Java躲避雪花小游戏代码
时间: 2023-07-03 17:32:34 浏览: 85
509.JAVA飞机大战和躲雪花游戏.zip
好的,以下是一个简单的Java躲避雪花小游戏代码,希望能够帮助到您:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
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 DodgeSnow extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private static final int PLAYER_SIZE = 20;
private static final int SNOW_SIZE = 10;
private static final int MAX_SNOW = 100;
private static final int PLAYER_SPEED = 5;
private int playerX = WIDTH / 2;
private int playerY = HEIGHT / 2;
private List<Rectangle> snowflakes = new ArrayList<Rectangle>();
private Random rand = new Random();
public DodgeSnow() {
JFrame frame = new JFrame("Dodge Snow");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.addKeyListener(this);
frame.add(this);
frame.setVisible(true);
generateSnowflakes();
gameLoop();
}
private void generateSnowflakes() {
for (int i = 0; i < MAX_SNOW; i++) {
int x = rand.nextInt(WIDTH);
int y = rand.nextInt(HEIGHT);
snowflakes.add(new Rectangle(x, y, SNOW_SIZE, SNOW_SIZE));
}
}
private void gameLoop() {
while (true) {
movePlayer();
moveSnowflakes();
checkCollisions();
repaint();
sleep(50);
}
}
private void movePlayer() {
if (isKeyPressed(KeyEvent.VK_UP)) {
playerY -= PLAYER_SPEED;
}
if (isKeyPressed(KeyEvent.VK_DOWN)) {
playerY += PLAYER_SPEED;
}
if (isKeyPressed(KeyEvent.VK_LEFT)) {
playerX -= PLAYER_SPEED;
}
if (isKeyPressed(KeyEvent.VK_RIGHT)) {
playerX += PLAYER_SPEED;
}
playerX = Math.max(0, playerX);
playerX = Math.min(WIDTH - PLAYER_SIZE, playerX);
playerY = Math.max(0, playerY);
playerY = Math.min(HEIGHT - PLAYER_SIZE, playerY);
}
private void moveSnowflakes() {
for (Rectangle snowflake : snowflakes) {
snowflake.y += rand.nextInt(5) + 1;
if (snowflake.y > HEIGHT) {
snowflake.y = 0;
snowflake.x = rand.nextInt(WIDTH);
}
}
}
private void checkCollisions() {
Rectangle playerRect = new Rectangle(playerX, playerY, PLAYER_SIZE, PLAYER_SIZE);
for (Rectangle snowflake : snowflakes) {
if (playerRect.intersects(snowflake)) {
playerX = WIDTH / 2;
playerY = HEIGHT / 2;
break;
}
}
}
private boolean isKeyPressed(int keyCode) {
return keys[keyCode];
}
private void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@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.WHITE);
for (Rectangle snowflake : snowflakes) {
g.fillRect(snowflake.x, snowflake.y, SNOW_SIZE, SNOW_SIZE);
}
}
private boolean[] keys = new boolean[256];
@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
@Override
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
new DodgeSnow();
}
}
```
该游戏的规则是,玩家需要通过控制一个小方块来躲避从顶部飞落下来的雪花,如果玩家碰到了任何一个雪花,游戏就会结束。玩家可以使用方向键来控制小方块的移动。
阅读全文