java编写一个贪吃蛇游戏,具有计分功能
时间: 2024-02-12 10:08:52 浏览: 70
java 编写的贪吃蛇游戏
4星 · 用户满意度95%
好的,下面是一个简单的 Java 贪吃蛇游戏,具有计分功能:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeGame implements ActionListener, KeyListener {
public static final int WIDTH = 800, HEIGHT = 800;
public static final int UNIT_SIZE = 25;
public static final int GAME_UNITS = (WIDTH * HEIGHT) / UNIT_SIZE;
public static final int DELAY = 75;
private final int[] x = new int[GAME_UNITS];
private final int[] y = new int[GAME_UNITS];
private int bodyParts = 6;
private int applesEaten = 0;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
private Random random;
public SnakeGame() {
JFrame frame = new JFrame("Snake Game");
JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
};
panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
panel.setBackground(Color.BLACK);
panel.setFocusable(true);
panel.addKeyListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
startGame();
}
private void startGame() {
x[0] = 0;
y[0] = 0;
random = new Random();
appleX = random.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE;
appleY = random.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE;
timer = new Timer(DELAY, this);
timer.start();
running = true;
}
private void draw(Graphics g) {
if (running) {
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.YELLOW);
}
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
g.setColor(Color.WHITE);
g.drawString("Score: " + applesEaten, 10, 20);
} else {
gameOver(g);
}
}
private void move() {
for (int i = bodyParts; i > 0; i--) {
x[i] = x[i - 1];
y[i] = y[i - 1];
}
switch (direction) {
case 'U':
y[0] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}
private void checkApple() {
if (x[0] == appleX && y[0] == appleY) {
bodyParts++;
applesEaten++;
appleX = random.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE;
appleY = random.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE;
}
}
private void checkCollisions() {
for (int i = bodyParts; i > 0; i--) {
if (x[0] == x[i] && y[0] == y[i]) {
running = false;
break;
}
}
if (x[0] < 0 || x[0] > WIDTH - UNIT_SIZE || y[0] < 0 || y[0] > HEIGHT - UNIT_SIZE) {
running = false;
}
if (!running) {
timer.stop();
}
}
private void gameOver(Graphics g) {
g.setColor(Color.WHITE);
g.drawString("Game Over", WIDTH / 2 - 30, HEIGHT / 2 - 10);
g.drawString("Score: " + applesEaten, WIDTH / 2 - 30, HEIGHT / 2 + 10);
}
@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
panel.repaint();
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if (direction != 'U') {
direction = 'D';
}
break;
case KeyEvent.VK_LEFT:
if (direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if (direction != 'L') {
direction = 'R';
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new SnakeGame();
}
}
```
这个游戏使用了 Java Swing 组件来创建窗口和绘制游戏。在游戏过程中,使用方向键控制蛇的移动方向,当蛇头碰到边界或自身时游戏结束。每当蛇吃掉一个苹果时,计分器会加一。
阅读全文