贪吃蛇的游戏代码java
时间: 2023-10-03 14:07:19 浏览: 90
以下是一个基本的贪吃蛇游戏示例代码,使用Java编写:
```
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class SnakeGame extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private static final int BOX_WIDTH = 10;
private static final int BOX_HEIGHT = 10;
private static final int GRID_WIDTH = 50;
private static final int GRID_HEIGHT = 50;
private static final int INITIAL_SNAKE_LENGTH = 5;
private static final int INITIAL_DELAY = 100;
private static final int SCORE_INCREMENT = 10;
private static final Random RAND = new Random();
private ArrayList<Point> snake;
private Point food;
private int score;
private int direction;
private Timer timer;
public SnakeGame() {
setPreferredSize(new Dimension(GRID_WIDTH * BOX_WIDTH, GRID_HEIGHT * BOX_HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(new KeyAdapter() {
@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;
}
}
});
initGame();
}
private void initGame() {
snake = new ArrayList<Point>();
for (int i = 0; i < INITIAL_SNAKE_LENGTH; i++) {
snake.add(new Point(GRID_WIDTH / 2 + i, GRID_HEIGHT / 2));
}
food = new Point(RAND.nextInt(GRID_WIDTH), RAND.nextInt(GRID_HEIGHT));
score = 0;
direction = KeyEvent.VK_LEFT;
timer = new Timer(INITIAL_DELAY, this);
timer.start();
}
private void move() {
Point head = snake.get(0);
Point newHead = (Point) head.clone();
switch (direction) {
case KeyEvent.VK_LEFT:
newHead.x--;
break;
case KeyEvent.VK_RIGHT:
newHead.x++;
break;
case KeyEvent.VK_UP:
newHead.y--;
break;
case KeyEvent.VK_DOWN:
newHead.y++;
break;
}
if (newHead.equals(food)) {
snake.add(0, newHead);
score += SCORE_INCREMENT;
food.setLocation(RAND.nextInt(GRID_WIDTH), RAND.nextInt(GRID_HEIGHT));
timer.setDelay(Math.max(timer.getDelay() - 5, 30));
} else if (newHead.x < 0 || newHead.x >= GRID_WIDTH || newHead.y < 0 || newHead.y >= GRID_HEIGHT
|| snake.contains(newHead)) {
timer.stop();
} else {
snake.remove(snake.size() - 1);
snake.add(0, newHead);
}
}
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x * BOX_WIDTH, p.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
}
g.setColor(Color.RED);
g.fillRect(food.x * BOX_WIDTH, food.y * BOX_HEIGHT, BOX_WIDTH, BOX_HEIGHT);
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 10, 20);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
此代码实现了基本的贪吃蛇游戏逻辑,包括:
- 贪吃蛇的移动、吃食物、撞墙和撞到自己的检测;
- 食物随机生成;
- 得分和速度的计算和更新。
运行此代码将显示一个游戏窗口,玩家可以使用箭头键控制蛇的移动。游戏难度将随着得分的增加而提高。
阅读全文