用java编写一个贪吃蛇
时间: 2023-08-22 16:07:55 浏览: 75
编写一个贪吃蛇游戏可以使用Java语言和Java Swing库,下面是一个简单的贪吃蛇游戏实现的示例代码:
```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.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 ActionListener, KeyListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
public static final int DOT_SIZE = 10;
public static final int ALL_DOTS = (WIDTH * HEIGHT) / (DOT_SIZE * DOT_SIZE);
public static final int RAND_POS = (WIDTH / DOT_SIZE) * (HEIGHT / DOT_SIZE);
private ArrayList<Point> snake;
private Point food;
private Timer timer;
private boolean inGame;
private int score;
private int[] x = new int[ALL_DOTS];
private int[] y = new int[ALL_DOTS];
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
public SnakeGame() {
addKeyListener(this);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.black);
setFocusable(true);
initGame();
}
private void initGame() {
snake = new ArrayList<Point>();
score = 0;
inGame = true;
for (int i = 0; i < 3; i++) {
x[i] = 50 - i * DOT_SIZE;
y[i] = 50;
snake.add(new Point(x[i], y[i]));
}
generateFood();
timer = new Timer(100, this);
timer.start();
}
private void generateFood() {
Random rand = new Random();
int r = rand.nextInt(RAND_POS);
int foodX = ((r * DOT_SIZE));
r = rand.nextInt(RAND_POS);
int foodY = ((r * DOT_SIZE));
food = new Point(foodX, foodY);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (inGame) {
g.setColor(Color.green);
g.fillOval(food.x, food.y, DOT_SIZE, DOT_SIZE);
for (int i = 0; i < snake.size(); i++) {
g.setColor(Color.white);
g.fillOval(snake.get(i).x, snake.get(i).y, DOT_SIZE, DOT_SIZE);
}
g.setColor(Color.white);
g.drawString("Score: " + score, 10, 20);
} else {
g.setColor(Color.white);
g.drawString("Game Over", WIDTH / 2 - 30, HEIGHT / 2);
}
}
private void move() {
for (int i = snake.size() - 1; i > 0; i--) {
snake.set(i, snake.get(i - 1));
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
snake.set(0, new Point(x[0], y[0]));
}
private void checkCollision() {
for (int i = snake.size() - 1; i > 0; i--) {
if (snake.get(0).equals(snake.get(i))) {
inGame = false;
}
}
if (snake.get(0).x < 0 || snake.get(0).x > WIDTH - DOT_SIZE || snake.get(0).y < 0 || snake.get(0).y > HEIGHT - DOT_SIZE) {
inGame = false;
}
if (inGame && snake.get(0).equals(food)) {
score++;
generateFood();
snake.add(new Point(x[snake.size() - 1], y[snake.size() - 1]));
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkCollision();
move();
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT && !rightDirection) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if (key == KeyEvent.VK_RIGHT && !leftDirection) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if (key == KeyEvent.VK_UP && !downDirection) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if (key == KeyEvent.VK_DOWN && !upDirection) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这个示例代码实现了一个简单的贪吃蛇游戏,其中使用了Java Swing库实现了游戏界面和游戏逻辑,通过键盘控制贪吃蛇的移动并实现吃食物得分的功能。你可以根据自己的需求进行修改和扩展。
阅读全文