写一个简单的贪吃蛇java代码
时间: 2024-01-07 13:03:35 浏览: 166
以下是一个简单的贪吃蛇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.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 = 500;
public static final int HEIGHT = 500;
private Thread thread;
private boolean running;
private boolean right = true;
private boolean left = false;
private boolean up = false;
private boolean down = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private int xCoor = 10;
private int yCoor = 10;
private int size = 5;
private int ticks = 0;
private Point apple;
private Random rand;
private int score = 0;
public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
setFocusable(true);
snake = new ArrayList<BodyPart>();
rand = new Random();
start();
}
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 tick() {
if (snake.size() == 0) {
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
}
ticks++;
if (ticks > 250000) {
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 (apple != null) {
if (xCoor == apple.x && yCoor == apple.y) {
size++;
score++;
apple.setLocation(rand.nextInt(49) * 10, rand.nextInt(49) * 10);
}
} else {
apple = new Point(rand.nextInt(49) * 10, rand.nextInt(49) * 10);
}
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 > 49 || yCoor < 0 || yCoor > 49) {
stop();
}
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
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);
}
g.setColor(Color.RED);
g.fillRect(apple.x, apple.y, 10, 10);
g.setColor(Color.WHITE);
g.drawString("Score: " + score, 10, 10);
}
public void run() {
while (running) {
tick();
repaint();
}
}
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;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
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);
}
}
class BodyPart {
private int xCoor;
private int yCoor;
private int width;
private int 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;
}
}
运行该代码将会生成一个简单的贪吃蛇游戏窗口。玩家可以通过方向键控制蛇的移动,吃到苹果会使蛇的长度增加,撞到墙或者自己的身体则会游戏结束。游戏窗口中间的红色方块代表苹果,左上角的白色文本代表得分。
阅读全文