用java编程贪吃蛇游戏代码
时间: 2023-10-29 21:11:40 浏览: 36
以下是一个简单的Java贪吃蛇游戏代码示例:
```
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
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 extends JPanel implements KeyListener, ActionListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 600;
private static final int HEIGHT = 600;
private static final int UNIT_SIZE = 25;
private static final int DELAY = 75;
private ArrayList<Point> snake;
private Point food;
private char direction;
private boolean isRunning;
private Timer timer;
private Random random;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
snake = new ArrayList<Point>();
snake.add(new Point(WIDTH / UNIT_SIZE / 2, HEIGHT / UNIT_SIZE / 2));
food = new Point();
generateFood();
direction = 'R';
isRunning = true;
timer = new Timer(DELAY, this);
timer.start();
random = new Random();
}
private void generateFood() {
int x = random.nextInt(WIDTH / UNIT_SIZE);
int y = random.nextInt(HEIGHT / UNIT_SIZE);
food.setLocation(x, y);
}
private void move() {
Point head = snake.get(0);
switch (direction) {
case 'U':
head = new Point(head.x, head.y - 1);
break;
case 'D':
head = new Point(head.x, head.y + 1);
break;
case 'L':
head = new Point(head.x - 1, head.y);
break;
case 'R':
head = new Point(head.x + 1, head.y);
break;
default:
break;
}
if (head.equals(food)) {
snake.add(0, food);
generateFood();
} else {
snake.remove(snake.size() - 1);
snake.add(0, head);
}
int x = head.x;
int y = head.y;
if (x < 0 || x >= (WIDTH / UNIT_SIZE)) {
isRunning = false;
return;
}
if (y < 0 || y >= (HEIGHT / UNIT_SIZE)) {
isRunning = false;
return;
}
for (int i = 1; i < snake.size(); i++) {
if (head.equals(snake.get(i))) {
isRunning = false;
return;
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(food.x * UNIT_SIZE, food.y * UNIT_SIZE, UNIT_SIZE, UNIT_SIZE);
g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x * UNIT_SIZE, p.y * UNIT_SIZE, UNIT_SIZE, UNIT_SIZE);
}
if (!isRunning) {
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("GAME OVER", (WIDTH / 2) - 120, HEIGHT / 2);
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && direction != 'R') {
direction = 'L';
}
if (key == KeyEvent.VK_RIGHT && direction != 'L') {
direction = 'R';
}
if (key == KeyEvent.VK_UP && direction != 'D') {
direction = 'U';
}
if (key == KeyEvent.VK_DOWN && direction != 'U') {
direction = 'D';
}
}
public void actionPerformed(ActionEvent e) {
if (isRunning) {
move();
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
```
要玩这个游戏,只需运行 `main` 方法即可。