软件构造第一次作业
开发一个具有界面的贪吃蛇小游戏,采用java语言。
1、要有简单的界面
2、要可操控
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.awt.event.KeyAdapter; 6 import java.awt.event.KeyEvent; 7 import java.util.ArrayList; 8 import java.util.Random; 9 10 public class SnakeGame extends JPanel implements ActionListener { 11 private final int WIDTH = 600; 12 private final int HEIGHT = 400; 13 private final int DOT_SIZE = 10; 14 private final int ALL_DOTS = (WIDTH * HEIGHT) / (DOT_SIZE * DOT_SIZE); 15 private final ArrayList<Point> snake; 16 private Point food; 17 private int direction; 18 private boolean running; 19 private Timer timer; 20 private int score; 21 22 public SnakeGame() { 23 snake = new ArrayList<>(); 24 snake.add(new Point(5, 5)); 25 direction = KeyEvent.VK_RIGHT; 26 running = true; 27 score = 0; 28 29 setPreferredSize(new Dimension(WIDTH, HEIGHT)); 30 setBackground(new Color(30, 30, 30)); // Dark background 31 setFocusable(true); 32 addKeyListener(new KeyAdapter() { 33 @Override 34 public void keyPressed(KeyEvent e) { 35 int newDirection = e.getKeyCode(); 36 if ((newDirection == KeyEvent.VK_LEFT && direction != KeyEvent.VK_RIGHT) || 37 (newDirection == KeyEvent.VK_RIGHT && direction != KeyEvent.VK_LEFT) || 38 (newDirection == KeyEvent.VK_UP && direction != KeyEvent.VK_DOWN) || 39 (newDirection == KeyEvent.VK_DOWN && direction != KeyEvent.VK_UP)) { 40 direction = newDirection; 41 } 42 } 43 }); 44 45 spawnFood(); 46 timer = new Timer(150, this); // Slower speed 47 timer.start(); 48 } 49 50 private void spawnFood() { 51 Random rand = new Random(); 52 int x = rand.nextInt(WIDTH / DOT_SIZE); 53 int y = rand.nextInt(HEIGHT / DOT_SIZE); 54 food = new Point(x, y); 55 } 56 57 @Override 58 protected void paintComponent(Graphics g) { 59 super.paintComponent(g); 60 if (running) { 61 g.setColor(Color.RED); 62 g.fillRect(food.x * DOT_SIZE, food.y * DOT_SIZE, DOT_SIZE, DOT_SIZE); 63 g.setColor(Color.GREEN); 64 for (Point p : snake) { 65 g.fillRect(p.x * DOT_SIZE, p.y * DOT_SIZE, DOT_SIZE, DOT_SIZE); 66 } 67 drawScore(g); 68 } else { 69 showGameOver(g); 70 } 71 } 72 73 private void drawScore(Graphics g) { 74 g.setColor(Color.WHITE); 75 g.setFont(new Font("Arial", Font.BOLD, 14)); 76 g.drawString("Score: " + score, 10, 20); 77 } 78 79 private void showGameOver(Graphics g) { 80 String msg = "Game Over"; 81 g.setColor(Color.WHITE); 82 g.setFont(new Font("Arial", Font.BOLD, 20)); 83 g.drawString(msg, WIDTH / 2 - g.getFontMetrics().stringWidth(msg) / 2, HEIGHT / 2); 84 g.setFont(new Font("Arial", Font.PLAIN, 14)); 85 g.drawString("Final Score: " + score, WIDTH / 2 - g.getFontMetrics().stringWidth("Final Score: " + score) / 2, HEIGHT / 2 + 20); 86 } 87 88 @Override 89 public void actionPerformed(ActionEvent e) { 90 if (running) { 91 move(); 92 checkCollision(); 93 checkFood(); 94 repaint(); 95 } 96 } 97 98 private void move() { 99 Point head = snake.get(0); 100 Point newHead = new Point(head); 101 102 switch (direction) { 103 case KeyEvent.VK_LEFT -> newHead.x--; 104 case KeyEvent.VK_RIGHT -> newHead.x++; 105 case KeyEvent.VK_UP -> newHead.y--; 106 case KeyEvent.VK_DOWN -> newHead.y++; 107 } 108 109 snake.add(0, newHead); 110 snake.remove(snake.size() - 1); 111 } 112 113 private void checkCollision() { 114 Point head = snake.get(0); 115 if (head.x < 0 || head.x >= WIDTH / DOT_SIZE || head.y < 0 || head.y >= HEIGHT / DOT_SIZE || snake.subList(1, snake.size()).contains(head)) { 116 running = false; 117 timer.stop(); 118 } 119 } 120 121 private void checkFood() { 122 if (snake.get(0).equals(food)) { 123 snake.add(new Point(food)); 124 score++; 125 spawnFood(); 126 } 127 } 128 129 public static void main(String[] args) { 130 JFrame frame = new JFrame("Snake Game"); 131 SnakeGame game = new SnakeGame(); 132 frame.add(game); 133 frame.pack(); 134 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 135 frame.setLocationRelativeTo(null); 136 frame.setVisible(true); 137 } 138 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?