GameOver.java---BallGame

此博客展示了Java代码,通过import语句引入相关类,定义了名为GameOver的类继承自GameCanvas,用于实现游戏结束场景。包含构造方法和显示游戏结束的方法,在方法中绘制文字并处理异常,最后退出应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * <strong>FileName</strong>: GameOver.java
 * <HR>
 * <strong>Description</strong>: The scene of ending game.
 * <P><HR>
 *  @author
 *  <em>
 * This Game is developed by <B>Fansy</B>.<BR>
 *  F.S.Studio 1999~2005
 *  </em>
 *  @version 0.3.2
 */

public class GameOver extends GameCanvas
{
 private Graphics g;
 
 public GameOver()
 {
  super(false);
  g=getGraphics();
 }
 
 public void showGameOver()
 {
  Display.getDisplay(BallMidlet.instance).setCurrent(this);
  g.drawString("GameOver!",100,150,20);
  this.flushGraphics();
  try{Thread.sleep(4000);}
  catch(Exception e){}
  BallMidlet.instance.quitApp();
 }
}

import java.awt.*; import java.awt.event.*; import javax.swing.Timer; public class JumpBall { Frame frame=new Frame("弹球小游戏"); //定义地图的大小 private final int map_width=300; private final int map_high=500; //定义小球的大小 private final int ball_size=16; //定义小球的位置 private int ball_x=50; private int ball_y=20; //设置小球的速度 private int speed_x=10; private int speed_y=10; //定义球拍的尺寸 private final int band_length=80; private final int band_high=20; //定义球拍的位置 private int band_x=150; private final int band_y=450;//因为球拍是水平移动的,所以坐标y的值不变 //定义一个计时器 private Timer timer; //创建一个画布 myCanvas mc=new myCanvas(); //设置一个Boole的变量,用来判断游戏是否结束 private Boolean isOver=false; //定义一个自定义画布类,继承Canvas private class myCanvas extends Canvas{ @Override public void paint(Graphics g) { //如果游戏结束 if(isOver){ //设置画笔颜色 g.setColor(Color.RED); g.setFont(new Font("Times",Font.BOLD,30)); g.drawString("游戏结束",100,250); } else { //游戏中 //绘制小球 g.setColor(Color.RED); g.fillOval(ball_x,ball_y,ball_size,ball_size); //绘制球拍 g.setColor(Color.PINK); g.fillRect(band_x,band_y,band_length,band_high); } } } private void invit() { //设置画布尺寸 mc.setPreferredSize(new Dimension(map_width, map_high)); // KeyListener keyListener = new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int name = e.getKeyCode(); if (name == KeyEvent.VK_LEFT) { //如果按下的是<- if (band_x > 0) { band_x = band_x - 10; } } if (name == KeyEvent.VK_RIGHT) { //按下的是-> if (band_x < map_width - band_length) { band_x = band_x + 10; } } } }; frame.addKeyListener(keyListener); mc.addKeyListener(keyListener); //修正小球的速度 ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (ball_x <= 0 || ball_x >= map_width - ball_size) { //当球碰撞到地图两边 speed_x = -speed_x; } if (ball_y <= 0 || (ball_y >= band_y - ball_size && ball_x > band_x && ball_x < band_x + band_length)) { //当球碰到上边界和球拍的时候 speed_y = -speed_y; } if (ball_y > band_y && (ball_x < band_x || ball_x > band_x + band_length)) { //游戏结束 timer.stop(); isOver = true; //刷新画板 mc.repaint(); } ball_x = ball_x + speed_x; ball_y = ball_y + speed_y; mc.repaint(); } }; timer = new Timer(100, actionListener); timer.start(); frame.add(mc); frame.pack(); frame.setVisible(true); //点击红叉能关闭窗口 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new JumpBall().invit(); } } 在这个代码基础上帮我加上一个得分情况和失败的字幕
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值