请用Java写一个贪吃蛇游戏
以下是一个简单的贪吃蛇游戏的Java代码示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SnakeGame extends JFrame implements KeyListener {
private final int WIDTH = 800;
private final int HEIGHT = 600;
private final int DOT_SIZE = 20;
private final int ALL_DOTS = (WIDTH * HEIGHT) / (DOT_SIZE * DOT_SIZE);
private final int RAND_POS = 30;
private final int DELAY = 140;
private List<Point> snake;
private Point apple;
private boolean inGame;
private Timer timer;
private int direction;
public SnakeGame() {
setTitle("Snake Game");
setSize(WIDTH, HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
addKeyListener(this);
snake = new ArrayList<>();
inGame = true;
direction = KeyEvent.VK_RIGHT;
timer = new Timer(DELAY, e -> gameLoop());
initGame();
}
private void initGame() {
snake.clear();
snake.add(new Point(100, 100));
snake.add(new Point(80, 100));
snake.add(new Point(60, 100));
generateApple();
timer.start();
}
private void generateApple() {
Random random = new Random();
int x = random.nextInt(RAND_POS) * DOT_SIZE;
int y = random.nextInt(RAND_POS) * DOT_SIZE;
apple = new Point(x, y);
}
private void gameLoop() {
if (inGame) {
move();
checkCollision();
repaint();
}
}
private void move() {
Point head = new Point(snake.get(0));
if (direction == KeyEvent.VK_RIGHT) {
head.x += DOT_SIZE;
} else if (direction == KeyEvent.VK_LEFT) {
head.x -= DOT_SIZE;
} else if (direction == KeyEvent.VK_UP) {
head.y -= DOT_SIZE;
} else if (direction == KeyEvent.VK_DOWN) {
head.y += DOT_SIZE;
}
snake.add(0, head);
snake.remove(snake.size() - 1);
}
private void checkCollision() {
Point head = snake.get(0);
if (head.x >= WIDTH || head.x < 0 || head.y >= HEIGHT || head.y < 0) {
inGame = false;
}
for (int i = 1; i < snake.size(); i++) {
if (head.equals(snake.get(i))) {
inGame = false;
break;
}
}
if (head.equals(apple)) {
snake.add(new Point(-1, -1));
generateApple();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.setColor(Color.RED);
g.fillRect(apple.x, apple.y, DOT_SIZE, DOT_SIZE);
g.setColor(Color.GREEN);
for (Point point : snake) {
g.fillRect(point.x, point.y, DOT_SIZE, DOT_SIZE);
}
} else {
g.setColor(Color.RED);
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Game Over", WIDTH / 2 - 120, HEIGHT / 2);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT) {
direction = KeyEvent.VK_LEFT;
} else if (key == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT) {
direction = KeyEvent.VK_RIGHT;
} else if (key == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) {
direction = KeyEvent.VK_UP;
} else if (key == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP) {
direction = KeyEvent.VK_DOWN;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public static void main(String[] args) {
new SnakeGame();
}
}
这个代码创建了一个继承自JFrame的SnakeGame类,实现了一个简单的贪吃蛇游戏。在游戏中,玩家控制蛇移动,吃掉苹果来得分,如果蛇撞墙或撞到自己,则游戏结束
原文地址: http://www.cveoy.top/t/topic/iPfA 著作权归作者所有。请勿转载和采集!