下面是一个简单的贪吃蛇游戏的Java代码示例:\n\njava\nimport java.awt.*;\nimport java.awt.event.*;\nimport javax.swing.*;\n\npublic class SnakeGame extends JPanel implements ActionListener {\n private final int B_WIDTH = 300;\n private final int B_HEIGHT = 300;\n private final int DOT_SIZE = 10;\n private final int ALL_DOTS = 900;\n private final int RAND_POS = 29;\n private final int DELAY = 140;\n\n private final int x[] = new int[ALL_DOTS];\n private final int y[] = new int[ALL_DOTS];\n\n private int dots;\n private int apple_x;\n private int apple_y;\n\n private boolean leftDirection = false;\n private boolean rightDirection = true;\n private boolean upDirection = false;\n private boolean downDirection = false;\n private boolean inGame = true;\n\n private Timer timer;\n private Image ball;\n private Image apple;\n private Image head;\n\n public SnakeGame() {\n initBoard();\n }\n \n private void initBoard() {\n addKeyListener(new TAdapter());\n setBackground(Color.black);\n setFocusable(true);\n\n setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));\n loadImages();\n initGame();\n }\n\n private void loadImages() {\n ImageIcon iid = new ImageIcon("src/resources/dot.png");\n ball = iid.getImage();\n\n ImageIcon iia = new ImageIcon("src/resources/apple.png");\n apple = iia.getImage();\n\n ImageIcon iih = new ImageIcon("src/resources/head.png");\n head = iih.getImage();\n }\n\n private void initGame() {\n dots = 3;\n\n for (int z = 0; z < dots; z++) {\n x[z] = 50 - z * 10;\n y[z] = 50;\n }\n\n locateApple();\n\n timer = new Timer(DELAY, this);\n timer.start();\n }\n\n @Override\n public void paintComponent(Graphics g) {\n super.paintComponent(g);\n\n doDrawing(g);\n }\n\n private void doDrawing(Graphics g) {\n if (inGame) {\n g.drawImage(apple, apple_x, apple_y, this);\n\n for (int z = 0; z < dots; z++) {\n if (z == 0) {\n g.drawImage(head, x[z], y[z], this);\n } else {\n g.drawImage(ball, x[z], y[z], this);\n }\n }\n\n Toolkit.getDefaultToolkit().sync();\n\n } else {\n gameOver(g);\n }\n }\n\n private void gameOver(Graphics g) {\n String msg = "Game Over";\n Font small = new Font("Helvetica", Font.BOLD, 14);\n FontMetrics metr = getFontMetrics(small);\n\n g.setColor(Color.white);\n g.setFont(small);\n g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);\n }\n\n private void checkApple() {\n if ((x[0] == apple_x) && (y[0] == apple_y)) {\n dots++;\n locateApple();\n }\n }\n\n private void move() {\n for (int z = dots; z > 0; z--) {\n x[z] = x[(z - 1)];\n y[z] = y[(z - 1)];\n }\n\n if (leftDirection) {\n x[0] -= DOT_SIZE;\n }\n\n if (rightDirection) {\n x[0] += DOT_SIZE;\n }\n\n if (upDirection) {\n y[0] -= DOT_SIZE;\n }\n\n if (downDirection) {\n y[0] += DOT_SIZE;\n }\n }\n\n private void checkCollision() {\n for (int z = dots; z > 0; z--) {\n if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {\n inGame = false;\n }\n }\n\n if (y[0] >= B_HEIGHT) {\n inGame = false;\n }\n\n if (y[0] < 0) {\n inGame = false;\n }\n\n if (x[0] >= B_WIDTH) {\n inGame = false;\n }\n\n if (x[0] < 0) {\n inGame = false;\n }\n\n if (!inGame) {\n timer.stop();\n }\n }\n\n private void locateApple() {\n int r = (int) (Math.random() * RAND_POS);\n apple_x = ((r * DOT_SIZE));\n\n r = (int) (Math.random() * RAND_POS);\n apple_y = ((r * DOT_SIZE));\n }\n\n @Override\n public void actionPerformed(ActionEvent e) {\n if (inGame) {\n checkApple();\n checkCollision();\n move();\n }\n\n repaint();\n }\n\n private class TAdapter extends KeyAdapter {\n @Override\n public void keyPressed(KeyEvent e) {\n int key = e.getKeyCode();\n\n if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {\n leftDirection = true;\n upDirection = false;\n downDirection = false;\n }\n\n if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {\n rightDirection = true;\n upDirection = false;\n downDirection = false;\n }\n\n if ((key == KeyEvent.VK_UP) && (!downDirection)) {\n upDirection = true;\n rightDirection = false;\n leftDirection = false;\n }\n\n if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {\n downDirection = true;\n rightDirection = false;\n leftDirection = false;\n }\n }\n }\n\n public static void main(String[] args) {\n JFrame frame = new JFrame();\n frame.setTitle("Snake");\n frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n frame.setResizable(false);\n frame.add(new SnakeGame());\n frame.pack();\n frame.setLocationRelativeTo(null);\n frame.setVisible(true);\n }\n}\n\n\n该代码使用Java Swing库创建了一个简单的贪吃蛇游戏。它使用JPanel类作为游戏面板,通过重写paintComponent方法绘制游戏界面。ActionListener接口用于处理游戏逻辑,例如移动蛇、检查碰撞等。KeyListener接口用于处理玩家输入,例如控制蛇的移动方向。Timer类用于定时触发actionPerformed方法,以便更新游戏状态并重新绘制游戏界面。运行main方法将创建一个窗口并显示贪吃蛇游戏。


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

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