制作冰火人小游戏代码Java
时间: 2023-07-31 15:12:22 浏览: 113
以下是一个简单的冰火人小游戏Java代码示例,包含基本的游戏逻辑和GUI界面:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class IceFireGame extends JPanel implements KeyListener {
private final int WIDTH = 500; // 窗口宽度
private final int HEIGHT = 500; // 窗口高度
private final int PLAYER_SIZE = 30; // 玩家大小
private final int PLAYER_SPEED = 5; // 玩家速度
private final int ICE_SIZE = 20; // 冰块大小
private final int FIRE_SIZE = 20; // 火球大小
private final int FIRE_SPEED = 10; // 火球速度
private int playerX = WIDTH / 2 - PLAYER_SIZE / 2; // 玩家初始位置
private int playerY = HEIGHT / 2 - PLAYER_SIZE / 2;
private boolean upPressed, downPressed, leftPressed, rightPressed; // 方向键状态
private boolean gameOver = false; // 游戏结束标志
private int score = 0; // 得分
private int iceX, iceY; // 冰块位置
private int fireX, fireY; // 火球位置
public IceFireGame() {
JFrame frame = new JFrame("Ice Fire Game");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.addKeyListener(this);
frame.add(this);
frame.setVisible(true);
startGame();
}
private void startGame() {
iceX = (int) (Math.random() * (WIDTH - ICE_SIZE)); // 随机生成冰块位置
iceY = (int) (Math.random() * (HEIGHT - ICE_SIZE));
fireX = (int) (Math.random() * (WIDTH - FIRE_SIZE)); // 随机生成火球位置
fireY = (int) (Math.random() * (HEIGHT - FIRE_SIZE));
while (!gameOver) { // 游戏循环
try {
Thread.sleep(50); // 控制游戏速度
} catch (InterruptedException e) {
e.printStackTrace();
}
movePlayer();
moveFire();
checkCollision();
repaint(); // 刷新画面
}
}
private void movePlayer() {
if (upPressed && playerY > 0) {
playerY -= PLAYER_SPEED;
}
if (downPressed && playerY < HEIGHT - PLAYER_SIZE) {
playerY += PLAYER_SPEED;
}
if (leftPressed && playerX > 0) {
playerX -= PLAYER_SPEED;
}
if (rightPressed && playerX < WIDTH - PLAYER_SIZE) {
playerX += PLAYER_SPEED;
}
}
private void moveFire() {
if (fireY < HEIGHT) {
fireY += FIRE_SPEED;
} else {
fireX = (int) (Math.random() * (WIDTH - FIRE_SIZE)); // 火球到达底部,重新随机生成位置
fireY = 0;
}
}
private void checkCollision() {
if (playerX + PLAYER_SIZE > iceX && playerX < iceX + ICE_SIZE && playerY + PLAYER_SIZE > iceY
&& playerY < iceY + ICE_SIZE) { // 玩家与冰块碰撞
score++;
iceX = (int) (Math.random() * (WIDTH - ICE_SIZE)); // 冰块消失,重新随机生成位置
iceY = (int) (Math.random() * (HEIGHT - ICE_SIZE));
}
if (playerX + PLAYER_SIZE > fireX && playerX < fireX + FIRE_SIZE && playerY + PLAYER_SIZE > fireY
&& playerY < fireY + FIRE_SIZE) { // 玩家与火球碰撞
gameOver = true;
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLUE);
g.fillRect(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
g.setColor(Color.CYAN);
g.fillRect(iceX, iceY, ICE_SIZE, ICE_SIZE);
g.setColor(Color.RED);
g.fillRect(fireX, fireY, FIRE_SIZE, FIRE_SIZE);
g.setColor(Color.BLACK);
g.drawString("Score: " + score, 10, 20);
if (gameOver) {
g.drawString("Game Over!", WIDTH / 2 - 30, HEIGHT / 2);
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
new IceFireGame();
}
}
```
该游戏使用 Java Swing 框架实现,包含一个 IceFireGame 类,继承自 JPanel,并实现了 KeyListener 接口用于监听方向键的状态变化。在构造函数中,创建了一个 JFrame 窗口,并将当前 JPanel 添加到其中,同时注册了 KeyListener 监听器。startGame() 方法中包含了游戏的主循环,每次循环控制玩家、火球的移动和碰撞检测,并通过 repaint() 方法刷新画面。paintComponent() 方法用于绘制游戏界面,包括玩家、冰块、火球和得分等元素。在 keyPressed() 和 keyReleased() 方法中,根据不同的按键状态更新相应的标志位。最后,在 main() 方法中创建了一个 IceFireGame 实例,启动游戏。