java 贪吃蛇
1 先定义好方向的枚举
public enum Dir { L,R,U,D }
2 定义贪吃蛇活动的范围
public class Yard extends Frame { // 行 public static final int NodeSize = 15; // 列 public static final int NodeCount = 30; // 蛇活动的范围 public static final int AreaSize = NodeSize * NodeCount; public static int x = AreaSize / 2; public static int y = AreaSize / 2; Egg egg = new Egg(10, 10); Snake snake = new Snake(); Yard() { this.setSize(2 * AreaSize, 2 * AreaSize); this.setVisible(true); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); this.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { snake.keyPressed(e); } }); // 窗口重画 while (true) { try { // 50毫秒重画一次 Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } this.repaint(); } } @Override public void paint(Graphics graphics) { Color color = graphics.getColor(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, this.getWidth(), this.getHeight()); graphics.setColor(color); for (int i = 0; i <= NodeCount; i++) { graphics.drawLine(x, y + NodeSize * i, x + AreaSize, y + NodeSize * i); graphics.drawLine(x + NodeSize * i, y, x + NodeSize * i, x + AreaSize); } egg.paint(graphics); snake.paint(graphics); snake.eat(egg); } // 游戏画面双缓冲 Image offScreenImage; @Override public void update(Graphics graphics) { if (null == offScreenImage) { offScreenImage = this.createImage(this.getWidth(), this.getHeight()); } Graphics gps = offScreenImage.getGraphics(); paint(gps); graphics.drawImage(offScreenImage, 0, 0,null); } public static void main(String[] args) { new Yard(); } }
3 定义贪吃蛇
public class Snake { // 头 尾巴 Node head, tail; // 初始方向 Dir dir = Dir.L; Snake() { head = new Node(20,20); tail = head; } public void addToHead() { Node node = null; switch (dir) { case L: node = new Node(head.row, head.col - 1); break; case R: node = new Node(head.row, head.col + 1); break; case U: node = new Node(head.row - 1, head.col); break; case D: node = new Node(head.row + 1, head.col); break; } node.next = head; head.prev = node; head = node; } private void boundaryCheck() { if (head.row < 0) { head.row = Yard.NodeCount - 1; } if (head.col < 0) { head.col = Yard.NodeCount - 1; } if (head.row > Yard.NodeCount - 1) { head.row = 0; } if (head.col > Yard.NodeCount - 1) { head.col = 0; } } private void deleteTail() { if (null == tail) { return; } tail = tail.prev; tail.next.prev = null; tail.next = null; } public void paint(Graphics graphics) { Node node = head; while (node != null) { node.paint(graphics); node = node.next; } move(); } private void move() { addToHead(); deleteTail(); boundaryCheck(); } public void keyPressed(KeyEvent keyEvent) { int key = keyEvent.getKeyCode(); switch (key) { case KeyEvent.VK_LEFT: dir = Dir.L; break; case KeyEvent.VK_RIGHT: dir = Dir.R; break; case KeyEvent.VK_UP: dir = Dir.U; break; case KeyEvent.VK_DOWN: dir = Dir.D; break; } } public void eat(Egg egg) { if (head.row == egg.row && head.col == egg.col) { addToHead(); egg.reAppear(); } } }
4 定义蛇的节点
public class Node { // 行,列 int row, col; Node prev, next; public Node(int row, int col) { this.row = row; this.col = col; } public void paint(Graphics graphics) { int x = Yard.x + col * Yard.NodeSize; int y = Yard.y + row * Yard.NodeSize; Color color = graphics.getColor(); graphics.setColor(Color.BLACK); graphics.fillRect(x, y, Yard.NodeSize, Yard.NodeSize); graphics.setColor(color); } }
5 定义蛇吃的节点
public class Egg { int row, col; Random random = new Random(); public Egg(int row, int col) { this.row = row; this.col = col; } public void paint(Graphics graphics) { int x = Yard.x + col * Yard.NodeSize; int y = Yard.y + row * Yard.NodeSize; Color color = graphics.getColor(); graphics.setColor(Color.RED); graphics.fillOval(x, y, Yard.NodeSize, Yard.NodeSize); graphics.setColor(color); } public void reAppear() { this.row = random.nextInt(Yard.NodeCount); this.col = random.nextInt(Yard.NodeCount); } }
简单版不死贪吃蛇就完成了
不积跬步,无以至千里;不积小流,无以成江海
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2017-04-25 Docker