用java写一个动画小游戏、
时间: 2023-12-07 11:25:54 浏览: 153
好的,我可以提供一个简单的 Java 动画小游戏,叫做“弹跳球”(Bouncing Ball)。游戏规则如下:
1. 程序会在屏幕中央生成一个小球,小球会从上方掉落。
2. 玩家需要通过键盘控制一个板子,让小球不要掉到屏幕底部。
3. 如果小球掉到底部,则游戏结束。
4. 如果玩家控制的板子成功接住小球,则游戏继续进行,小球会从下方反弹并向上飞行。
5. 玩家需要尽可能多地接住小球,直到达到一定分数为止。
下面是 Java 代码实现:
```java
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
public class BouncingBallGame extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400; // 窗口宽度
private static final int HEIGHT = 400; // 窗口高度
private static final int BALL_RADIUS = 20; // 小球半径
private static final int PADDLE_WIDTH = 60; // 板子宽度
private static final int PADDLE_HEIGHT = 10; // 板子高度
private static final int PADDLE_Y = HEIGHT - PADDLE_HEIGHT - 10; // 板子纵坐标
private static final int BALL_SPEED = 3; // 小球速度
private static final int PADDLE_SPEED = 5; // 板子速度
private static final int SCORE_PER_BOUNCE = 10; // 每次接球得分
private static final int GAME_OVER_SCORE = 100; // 游戏结束分数
private int ballX = WIDTH / 2; // 小球初始横坐标
private int ballY = 0; // 小球初始纵坐标
private int ballDx = BALL_SPEED; // 小球横向移动速度
private int ballDy = BALL_SPEED; // 小球纵向移动速度
private int paddleX = WIDTH / 2 - PADDLE_WIDTH / 2; // 板子初始横坐标
private int score = 0; // 玩家得分
public BouncingBallGame() {
JFrame frame = new JFrame("弹跳球游戏");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.addKeyListener(this);
frame.add(this);
frame.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillOval(ballX - BALL_RADIUS, ballY - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2);
g.setColor(Color.RED);
g.fillRect(paddleX, PADDLE_Y, PADDLE_WIDTH, PADDLE_HEIGHT);
g.setColor(Color.BLACK);
g.drawString("得分:" + score, 10, 20);
}
private void moveBall() {
ballX += ballDx;
ballY += ballDy;
// 检测小球是否碰到左右边界
if (ballX - BALL_RADIUS < 0 || ballX + BALL_RADIUS > WIDTH) {
ballDx = -ballDx;
}
// 检测小球是否碰到顶部
if (ballY - BALL_RADIUS < 0) {
ballDy = BALL_SPEED;
}
// 检测小球是否碰到板子
if (ballY + BALL_RADIUS >= PADDLE_Y && ballX >= paddleX && ballX <= paddleX + PADDLE_WIDTH) {
ballDy = -BALL_SPEED;
score += SCORE_PER_BOUNCE;
}
// 检测小球是否掉到底部
if (ballY + BALL_RADIUS > HEIGHT) {
gameOver();
}
}
private void movePaddle(int direction) {
paddleX += direction * PADDLE_SPEED;
// 检测板子是否碰到左右边界
if (paddleX < 0) {
paddleX = 0;
} else if (paddleX + PADDLE_WIDTH > WIDTH) {
paddleX = WIDTH - PADDLE_WIDTH;
}
}
private void gameOver() {
System.out.println("游戏结束,得分:" + score);
System.exit(0);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
movePaddle(-1);
} else if (key == KeyEvent.VK_RIGHT) {
movePaddle(1);
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
BouncingBallGame game = new BouncingBallGame();
Random random = new Random();
while (true) {
game.moveBall();
game.repaint();
// 随机改变小球的速度和方向,增加游戏难度
if (random.nextInt(100) < 5) {
game.ballDx = -game.ballDx;
}
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 如果玩家得分达到一定分数,游戏结束
if (game.score >= GAME_OVER_SCORE) {
System.out.println("恭喜你,获得胜利!");
System.exit(0);
}
}
}
}
```
这个游戏使用 Java Swing 组件库来实现,需要用到键盘事件和线程控制等知识点。玩家需要通过左右箭头键来控制板子,让小球不要掉到底部,并尽量多地接住小球,得到更高的得分。小球的速度和方向会随机改变,增加游戏难度,当玩家得分达到一定分数时,游戏结束,玩家获得胜利。
阅读全文
相关推荐












