你可以用java写一个贪吃蛇小游戏吗
时间: 2023-12-22 21:02:33 浏览: 29
当然可以!以下是一个简单的Java贪吃蛇小游戏的实现代码,供你参考:
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
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;
public class SnakeGame extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
private static final int BOX_WIDTH = 20;
private static final int BOX_HEIGHT = 20;
private static final int BOARD_WIDTH = 20;
private static final int BOARD_HEIGHT = 20;
private static final int INITIAL_SNAKE_SIZE = 3;
private static final int SLEEP_TIME = 100;
private static final Random random = new Random();
private ArrayList<Point> snake = new ArrayList<Point>();
private Point food;
private int direction = KeyEvent.VK_RIGHT;
public SnakeGame() {
setPreferredSize(new java.awt.Dimension(BOARD_WIDTH * BOX_WIDTH, BOARD_HEIGHT * BOX_HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
initGame();
}
private void initGame() {
snake.clear();
for (int i = 0; i < INITIAL_SNAKE_SIZE; i++) {
snake.add(new Point(i, 0));
}
placeFood();
}
private void placeFood() {
int x = random.nextInt(BOARD_WIDTH);
int y = random.nextInt(BOARD_HEIGHT);
food = new Point(x, y);
}
private void move() {
Point head = snake.get(0);
switch (direction) {
case KeyEvent.VK_LEFT:
head = new Point(head.x - 1, head.y);
break;
case KeyEvent.VK_RIGHT:
head = new Point(head.x + 1, head.y);
break;
case KeyEvent.VK_UP:
head = new Point(head.x, head.y - 1);
break;
case KeyEvent.VK_DOWN:
head = new Point(head.x, head.y + 1);
break;
}
snake.add(0, head);
if (head.equals(food)) {
placeFood();
} else {
snake.remove(snake.size() - 1);
}
}
private boolean checkCollision() {
Point head = snake.get(0);
if (head.x < 0 || head.x >= BOARD_WIDTH || head.y < 0 || head.y >= BOARD_HEIGHT) {
return true;
}
for (int i = 1; i < snake.size(); i++) {
if (snake.get(i).equals(head)) {
return true;
}
}
return false;
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN)
&& Math.abs(key - direction) != 2) {
direction = key;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(food.x * BOX_WIDTH, food.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.WHITE);
for (Point p : snake) {
g.fillRect(p.x * BOX_WIDTH, p.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
}
}
public void run() {
while (true) {
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
}
move();
if (checkCollision()) {
break;
}
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
SnakeGame game = new SnakeGame();
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
game.run();
}
}
```
该代码使用了Java Swing绘制图形界面,并使用键盘事件处理玩家输入。游戏中使用ArrayList存储蛇的身体,使用Point表示每个蛇身体的坐标。在游戏中,我们先绘制一个食物,然后循环进行以下步骤:
1. 检查玩家是否碰撞到墙壁或自己的身体,如果是,则结束游戏。
2. 根据玩家输入更新蛇的方向,然后移动蛇的身体。
3. 如果蛇头碰到食物,重新生成一个食物。
4. 重新绘制游戏界面。
在游戏中,我们可以使用Java的线程来控制游戏的运行,使得游戏能够不断地更新,同时也不会阻塞主线程。