用java写一个贪吃蛇
时间: 2023-12-24 13:03:13 浏览: 26
好的,下面是一个简单的 Java 贪吃蛇游戏示例代码,希望对你有所帮助:
```java
import java.awt.Color;
import java.awt.Dimension;
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.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
private Thread thread;
private boolean running;
private Point head;
private List<Point> tail;
private Point food;
private int direction;
private int score;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
init();
}
private void init() {
head = new Point(WIDTH / 2, HEIGHT / 2);
tail = new ArrayList<Point>();
tail.add(new Point(head.x - 10, head.y));
tail.add(new Point(head.x - 20, head.y));
tail.add(new Point(head.x - 30, head.y));
food = new Point(getRandomX(), getRandomY());
direction = KeyEvent.VK_RIGHT;
score = 0;
}
private int getRandomX() {
Random r = new Random();
return r.nextInt(WIDTH / 10) * 10;
}
private int getRandomY() {
Random r = new Random();
return r.nextInt(HEIGHT / 10) * 10;
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (running) {
update();
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void update() {
switch (direction) {
case KeyEvent.VK_UP:
head = new Point(head.x, head.y - 10);
break;
case KeyEvent.VK_DOWN:
head = new Point(head.x, head.y + 10);
break;
case KeyEvent.VK_LEFT:
head = new Point(head.x - 10, head.y);
break;
case KeyEvent.VK_RIGHT:
head = new Point(head.x + 10, head.y);
break;
}
if (head.x < 0 || head.x >= WIDTH || head.y < 0 || head.y >= HEIGHT) {
gameOver();
}
for (Point p : tail) {
if (p.equals(head)) {
gameOver();
}
}
tail.add(0, head);
if (head.equals(food)) {
score++;
food = new Point(getRandomX(), getRandomY());
} else {
tail.remove(tail.size() - 1);
}
}
private void gameOver() {
stop();
System.out.println("Game over!");
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 10, 20);
g.setColor(Color.RED);
g.fillRect(food.x, food.y, 10, 10);
g.setColor(Color.GREEN);
for (Point p : tail) {
g.fillRect(p.x, p.y, 10, 10);
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) {
direction = KeyEvent.VK_UP;
} else if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP) {
direction = KeyEvent.VK_DOWN;
} else if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT) {
direction = KeyEvent.VK_LEFT;
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT) {
direction = KeyEvent.VK_RIGHT;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("Snake Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.getContentPane().add(new SnakeGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
```
这是一个简单的贪吃蛇游戏,包括移动、食物、得分和游戏结束等基本功能。你可以在此基础上进行修改和完善,实现更多功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)