import javax.swing.; import java.awt.; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Random; public class SnakeGame extends JFrame implements KeyListener { // 游戏界面大小 private final int WIDTH = 800; private final int HEIGHT = 800; // 网格大小 private static final int GRID_SIZE = 40; // 贪吃蛇移动速度 private static final int SNAKE_SPEED = GRID_SIZE; // 贪吃蛇 private ArrayList snake = new ArrayList<>(); private Point direction = new Point(1, 0); // 贪吃蛇移动方向 private Point food; // 食物 public SnakeGame() { // 设置窗口大小和标题 setSize(WIDTH, HEIGHT); setTitle('贪吃蛇小游戏'); // 设置窗口居中 setLocationRelativeTo(null); // 添加键盘监听器 addKeyListener(this); // 初始化贪吃蛇和食物 initSnake(); initFood(); // 显示窗口 setVisible(true); } // 初始化贪吃蛇 private void initSnake() { // 初始长度为3 snake.add(new Point(2, 0)); snake.add(new Point(1, 0)); snake.add(new Point(0, 0)); } // 初始化食物 private void initFood() { Random random = new Random(); int x = random.nextInt(WIDTH / GRID_SIZE); int y = random.nextInt(HEIGHT / GRID_SIZE); food = new Point(x, y); } // 绘制游戏界面 @Override public void paint(Graphics g) { // 清空画布 g.clearRect(0, 0, WIDTH, HEIGHT); // 绘制网格 g.setColor(Color.GRAY); for (int i = 0; i < WIDTH / GRID_SIZE; i++) { g.drawLine(i * GRID_SIZE, 0, i * GRID_SIZE, HEIGHT); } for (int i = 0; i < HEIGHT / GRID_SIZE; i++) { g.drawLine(0, i * GRID_SIZE, WIDTH, i * GRID_SIZE); } // 绘制贪吃蛇 g.setColor(Color.GREEN); for (Point p : snake) { g.fillRect(p.x * GRID_SIZE, p.y * GRID_SIZE, GRID_SIZE, GRID_SIZE); } // 绘制食物 g.setColor(Color.RED); g.fillOval(food.x * GRID_SIZE, food.y * GRID_SIZE, GRID_SIZE, GRID_SIZE); } // 贪吃蛇移动 private void moveSnake() { // 计算新的蛇头位置 Point head = new Point(snake.get(0).x + direction.x, snake.get(0).y + direction.y); // 判断是否撞墙 if (head.x < 0 || head.x >= WIDTH / GRID_SIZE || head.y < 0 || head.y >= HEIGHT / GRID_SIZE) { JOptionPane.showMessageDialog(this, 'Game Over!'); System.exit(0); } // 判断是否吃到食物 if (head.equals(food)) { snake.add(0, head); initFood(); } else { // 判断蛇头是否和蛇身重合 for (int i = 1; i < snake.size(); i++) { if (head.equals(snake.get(i))) { JOptionPane.showMessageDialog(this, 'Game Over!'); System.exit(0); } } snake.add(0, head); snake.remove(snake.size() - 1); } // 刷新游戏界面 repaint(); } // 键盘按下事件处理 @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: if (direction.y != 1) { direction.x = 0; direction.y = -1; } break; case KeyEvent.VK_DOWN: if (direction.y != -1) { direction.x = 0; direction.y = 1; } break; case KeyEvent.VK_LEFT: if (direction.x != 1) { direction.x = -1; direction.y = 0; } break; case KeyEvent.VK_RIGHT: if (direction.x != -1) { direction.x = 1; direction.y = 0; } break; } } // 必须实现的方法 @Override public void keyTyped(KeyEvent e) { } // 必须实现的方法 @Override public void keyReleased(KeyEvent e) { } public static void main(String[] args) {SnakeGame game = new SnakeGame(); // 游戏循环 while (true) { game.moveSnake(); try { Thread.sleep(1000 / SNAKE_SPEED); } catch (InterruptedException e) { e.printStackTrace(); } } }


原文地址: https://www.cveoy.top/t/topic/okX7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录