打一个贪吃蛇的java代码
以下是一个简单的贪吃蛇Java代码示例:
import java.util.ArrayList;
import java.util.Random;
public class SnakeGame {
public static final int WIDTH = 20; // 游戏窗口的宽度
public static final int HEIGHT = 20; // 游戏窗口的高度
public static final int CELL_SIZE = 25; // 每个方格的大小
private ArrayList<Cell> snake; // 蛇的身体
private Cell food; // 食物
private int dx, dy; // 蛇移动的方向
private int score; // 得分
public SnakeGame() {
snake = new ArrayList<Cell>();
snake.add(new Cell(WIDTH / 2, HEIGHT / 2)); // 初始化蛇的位置
food = new Cell(getRandomX(), getRandomY()); // 初始化食物的位置
dx = 1; // 蛇一开始向右移动
dy = 0;
score = 0;
}
// 获取一个随机的 X 坐标
private int getRandomX() {
Random random = new Random();
return random.nextInt(WIDTH);
}
// 获取一个随机的 Y 坐标
private int getRandomY() {
Random random = new Random();
return random.nextInt(HEIGHT);
}
// 移动蛇
public void move() {
// 计算下一个头部的位置
int x = snake.get(0).getX() + dx;
int y = snake.get(0).getY() + dy;
// 判断是否撞墙
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
System.out.println("Game Over!");
return;
}
// 判断是否吃到食物
if (x == food.getX() && y == food.getY()) {
snake.add(0, food);
food = new Cell(getRandomX(), getRandomY());
score++;
} else {
// 移动蛇
for (int i = snake.size() - 1; i > 0; i--) {
snake.get(i).setX(snake.get(i - 1).getX());
snake.get(i).setY(snake.get(i - 1).getY());
}
snake.get(0).setX(x);
snake.get(0).setY(y);
}
}
// 改变蛇的方向
public void changeDirection(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
// 获取得分
public int getScore() {
return score;
}
// 获取蛇的身体
public ArrayList<Cell> getSnake() {
return snake;
}
// 获取食物的位置
public Cell getFood() {
return food;
}
public static void main(String[] args) {
SnakeGame game = new SnakeGame();
GameWindow window = new GameWindow(game);
window.setVisible(true);
while (true) {
game.move();
window.repaint();
try {
Thread.sleep(100); // 控制蛇的移动速度
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Cell {
private int x, y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
private SnakeGame game;
public GamePanel(SnakeGame game) {
this.game = game;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 画出蛇的身体
ArrayList<Cell> snake = game.getSnake();
for (Cell cell : snake) {
g.setColor(Color.GREEN);
g.fillRect(cell.getX() * SnakeGame.CELL_SIZE, cell.getY() * SnakeGame.CELL_SIZE,
SnakeGame.CELL_SIZE, SnakeGame.CELL_SIZE);
}
// 画出食物
Cell food = game.getFood();
g.setColor(Color.RED);
g.fillRect(food.getX() * SnakeGame.CELL_SIZE, food.getY() * SnakeGame.CELL_SIZE,
SnakeGame.CELL_SIZE, SnakeGame.CELL_SIZE);
// 画出得分
g.setColor(Color.WHITE);
g.drawString("Score: " + game.getScore(), 10, 20);
}
}
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class GameWindow extends JFrame {
private SnakeGame game;
private GamePanel panel;
public GameWindow(SnakeGame game) {
this.game = game;
panel = new GamePanel(game);
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Snake Game");
setSize(SnakeGame.WIDTH * SnakeGame.CELL_SIZE, (SnakeGame.HEIGHT + 1) * SnakeGame.CELL_SIZE);
setLocationRelativeTo(null);
}
}
这个代码可以在控制台上运行,也可以运行在一个简单的窗口中。运行后,你可以使用方向键来控制蛇的移动,吃到食物会增加得分,撞墙会结束游戏。
原文地址: https://www.cveoy.top/t/topic/bypB 著作权归作者所有。请勿转载和采集!