java躲避雪花小游戏代码
时间: 2023-07-09 20:42:49 浏览: 50
以下是一个简单的 Java 躲避雪花小游戏代码,供您参考:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnowflakeGame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400;
private static final int HEIGHT = 600;
private static final int PLAYER_WIDTH = 50;
private static final int PLAYER_HEIGHT = 50;
private static final int PLAYER_SPEED = 5;
private static final int SNOWFLAKE_SIZE = 10;
private static final int SNOWFLAKE_SPEED = 3;
private static final int MAX_SNOWFLAKES = 50;
private static final Random random = new Random();
private boolean running;
private boolean gameOver;
private Player player;
private List<Snowflake> snowflakes;
private int score;
public SnowflakeGame() {
setPreferredSize(new java.awt.Dimension(WIDTH, HEIGHT));
setBackground(Color.WHITE);
player = new Player((WIDTH - PLAYER_WIDTH) / 2, HEIGHT - PLAYER_HEIGHT - 10, PLAYER_WIDTH, PLAYER_HEIGHT);
snowflakes = new ArrayList<Snowflake>();
score = 0;
addKeyListener(this);
setFocusable(true);
}
public void start() {
new Thread(this).start();
}
public void stop() {
running = false;
}
public void run() {
running = true;
while (running) {
update();
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void update() {
if (!gameOver) {
player.update();
for (Snowflake snowflake : snowflakes) {
snowflake.update();
if (player.intersects(snowflake)) {
gameOver = true;
break;
}
}
if (snowflakes.size() < MAX_SNOWFLAKES) {
snowflakes.add(new Snowflake(random.nextInt(WIDTH - SNOWFLAKE_SIZE), 0, SNOWFLAKE_SIZE, SNOWFLAKE_SIZE, SNOWFLAKE_SPEED));
}
for (int i = snowflakes.size() - 1; i >= 0; i--) {
Snowflake snowflake = snowflakes.get(i);
if (snowflake.getY() > HEIGHT) {
snowflakes.remove(i);
score++;
}
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
player.draw(g);
for (Snowflake snowflake : snowflakes) {
snowflake.draw(g);
}
if (gameOver) {
g.setColor(Color.RED);
g.drawString("Game Over!", WIDTH / 2 - 50, HEIGHT / 2);
g.drawString("Score: " + score, WIDTH / 2 - 35, HEIGHT / 2 + 20);
} else {
g.setColor(Color.BLACK);
g.drawString("Score: " + score, 10, 20);
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
player.setVelocityX(-PLAYER_SPEED);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
player.setVelocityX(PLAYER_SPEED);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT) {
player.setVelocityX(0);
}
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snowflake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
SnowflakeGame game = new SnowflakeGame();
frame.add(game);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
}
class Player {
private int x;
private int y;
private int width;
private int height;
private int velocityX;
public Player(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void setVelocityX(int velocityX) {
this.velocityX = velocityX;
}
public void update() {
x += velocityX;
if (x < 0) {
x = 0;
} else if (x + width > SnowflakeGame.WIDTH) {
x = SnowflakeGame.WIDTH - width;
}
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(x, y, width, height);
}
public boolean intersects(Snowflake snowflake) {
return x < snowflake.getX() + snowflake.getWidth() && x + width > snowflake.getX() && y < snowflake.getY() + snowflake.getHeight() && y + height > snowflake.getY();
}
}
class Snowflake {
private int x;
private int y;
private int width;
private int height;
private int velocityY;
public Snowflake(int x, int y, int width, int height, int velocityY) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.velocityY = velocityY;
}
public void update() {
y += velocityY;
}
public void draw(Graphics g) {
g.setColor(Color.GRAY);
g.fillRect(x, y, width, height);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
```
这个小游戏的实现方式是:玩家控制一个蓝色方块,躲避从屏幕上方落下的灰色小方块,得分是小方块从屏幕底部消失的数量。