利用Java开发一个贪吃蛇游戏,基本功能要求如下: 1、初始蛇长度为1,蛇吃到蛋长度加1。 2、蛋随机出现。 3、蛇碰到四壁或自己则死亡 4、计分,比如吃一个蛋得100分 5、可以暂停。 6、死后,可以按某个键重新开始。
时间: 2023-12-17 22:05:13 浏览: 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 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 = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private Egg egg;
private ArrayList<Egg> eggs;
private Random r;
private int xCoor = 10, yCoor = 10;
private int size = 5;
private boolean right = true, left = false, up = false, down = false;
private int ticks = 0;
private Key key;
private int score = 0;
public SnakeGame() {
setFocusable(true);
key = new Key();
addKeyListener(this);
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < WIDTH / 10; i++) {
g.drawLine(i * 10, 0, i * 10, HEIGHT);
}
for (int i = 0; i < HEIGHT / 10; i++) {
g.drawLine(0, i * 10, WIDTH, i * 10);
}
for (int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
for (int i = 0; i < eggs.size(); i++) {
eggs.get(i).draw(g);
}
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
snake = new ArrayList<BodyPart>();
eggs = new ArrayList<Egg>();
r = new Random();
addEgg();
while (running) {
tick();
repaint();
}
}
public void tick() {
if (snake.size() == 0) {
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
}
ticks++;
if (ticks > 1500000) {
if (right)
xCoor++;
if (left)
xCoor--;
if (up)
yCoor--;
if (down)
yCoor++;
ticks = 0;
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
if (snake.size() > size) {
snake.remove(0);
}
}
if (eggs.size() == 0) {
addEgg();
}
for (int i = 0; i < eggs.size(); i++) {
if (xCoor == eggs.get(i).getxCoor() && yCoor == eggs.get(i).getyCoor()) {
size++;
score += 100;
eggs.remove(i);
i--;
}
}
for (int i = 0; i < snake.size(); i++) {
if (xCoor == snake.get(i).getxCoor() && yCoor == snake.get(i).getyCoor()) {
if (i != snake.size() - 1) {
stop();
}
}
}
if (xCoor < 0 || xCoor > 29 || yCoor < 0 || yCoor > 29) {
stop();
}
}
public void addEgg() {
int x = r.nextInt(30);
int y = r.nextInt(30);
egg = new Egg(x, y, 10);
eggs.add(egg);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
SnakeGame game = new SnakeGame();
frame.add(game);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Snake Game");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
right = true;
}
if (key == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
}
if (key == KeyEvent.VK_UP && !down) {
left = false;
right = false;
up = true;
}
if (key == KeyEvent.VK_DOWN && !up) {
left = false;
right = false;
down = true;
}
if (key == KeyEvent.VK_ENTER) {
if (!running) {
running = true;
new Thread(this).start();
}
}
if (key == KeyEvent.VK_SPACE) {
if (running) {
running = false;
} else {
running = true;
new Thread(this).start();
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
class BodyPart {
private int xCoor, yCoor, width, height;
public BodyPart(int xCoor, int yCoor, int size) {
this.xCoor = xCoor;
this.yCoor = yCoor;
width = size;
height = size;
}
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(xCoor * width, yCoor * height, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
class Egg {
private int xCoor, yCoor, width, height;
public Egg(int xCoor, int yCoor, int size) {
this.xCoor = xCoor;
this.yCoor = yCoor;
width = size;
height = size;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillOval(xCoor * width, yCoor * height, width, height);
}
public int getxCoor() {
return xCoor;
}
public int getyCoor() {
return yCoor;
}
}
class Key {
public boolean[] keys = new boolean[256];
public boolean up, down, left, right;
public void update() {
up = keys[KeyEvent.VK_UP];
down = keys[KeyEvent.VK_DOWN];
left = keys[KeyEvent.VK_LEFT];
right = keys[KeyEvent.VK_RIGHT];
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
}
```
这个示例中有一些基本功能实现,如蛇的移动、蛋的随机生成、蛇的长度增加、死亡判定、计分、暂停等。您可以根据自己的需求进行修改和完善。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![](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)
![](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)
![](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)