很抱歉,由于贪吃蛇游戏的代码比较长,不能在此处完整地呈现。下面是一个简单的贪吃蛇游戏的Java代码示例,供您参考:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Snake extends JFrame implements KeyListener { private static final long serialVersionUID = 1L; private JPanel contentPane; private JLabel[][] map; private int[][] mapStatus; private int score = 0; private int snakeLength = 1; private int[] snakeX = new int[100]; private int[] snakeY = new int[100]; private int foodX, foodY; private Thread moveThread; public static void main(String[] args) { Snake frame = new Snake(); frame.setVisible(true); } public Snake() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new GridLayout(20, 20)); map = new JLabel[20][20]; mapStatus = new int[20][20]; for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { map[i][j] = new JLabel(); map[i][j].setOpaque(true); map[i][j].setBackground(Color.WHITE); contentPane.add(map[i][j]); mapStatus[i][j] = 0; } } snakeX[0] = 10; snakeY[0] = 10; map[10][10].setBackground(Color.BLACK); mapStatus[10][10] = 1; generateFood(); moveThread = new Thread(new MoveThread()); moveThread.start(); addKeyListener(this); requestFocus(); } private void generateFood() { foodX = (int) (Math.random() * 20); foodY = (int) (Math.random() * 20); if (mapStatus[foodX][foodY] == 1) { generateFood(); } else { map[foodX][foodY].setBackground(Color.RED); mapStatus[foodX][foodY] = 2; } } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_UP) { if (snakeLength == 1 || snakeX[0] != snakeX[1]) { snakeX[0] -= 1; } } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { if (snakeLength == 1 || snakeX[0] != snakeX[1]) { snakeX[0] += 1; } } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { if (snakeLength == 1 || snakeY[0] != snakeY[1]) { snakeY[0] -= 1; } } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { if (snakeLength == 1 || snakeY[0] != snakeY[1]) { snakeY[0] += 1; } } } private class MoveThread implements Runnable { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } move(); } } } private void move() { if (snakeX[0] == foodX && snakeY[0] == foodY) { score += 10; snakeLength++; generateFood(); } if (snakeX[0] < 0 || snakeX[0] >= 20 || snakeY[0] < 0 || snakeY[0] >= 20) { JOptionPane.showMessageDialog(this, "Game Over! Your score is " + score); System.exit(0); } if (mapStatus[snakeX[0]][snakeY[0]] == 1) { JOptionPane.showMessageDialog(this, "Game Over! Your score is " + score); System.exit(0); } for (int i = snakeLength - 1; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; map[snakeX[i]][snakeY[i]].setBackground(Color.BLACK); mapStatus[snakeX[i]][snakeY[i]] = 1; } map[snakeX[snakeLength - 1]][snakeY[snakeLength - 1]].setBackground(Color.WHITE); mapStatus[snakeX[snakeLength - 1]][snakeY[snakeLength - 1]] = 0; snakeX[0] += 1; map[snakeX[0]][snakeY[0]].setBackground(Color.BLACK); mapStatus[snakeX[0]][snakeY[0]] = 1; } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } }
该示例使用JPanel和JLabel创建游戏地图,并使用数组来跟踪贪吃蛇和食物的位置。键盘监听器监听玩家按键事件,并根据按键事件移动贪吃蛇。通过使用线程来控制贪吃蛇的移动,游戏可以以一定的速度运行。当贪吃蛇吃到食物时,玩家得分并且贪吃蛇的长度增加。如果贪吃蛇碰到游戏边界或碰到自己的身体,则游戏结束。
评论