以下是一个Java贪吃蛇游戏的简单实现代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SnakeGame extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JButton[][] gameBoard;
    private int[][] gameBoardState;
    private int snakeLength;
    private int snakeX, snakeY;
    private int direction;
    private Timer timer;
    
    public SnakeGame() {
        setTitle("Snake Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(20, 20));
        
        gameBoard = new JButton[20][20];
        gameBoardState = new int[20][20];
        snakeLength = 1;
        snakeX = 10;
        snakeY = 10;
        direction = 0;
        
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                gameBoard[i][j] = new JButton();
                add(gameBoard[i][j]);
            }
        }
        
        gameBoardState[snakeX][snakeY] = 1;
        gameBoard[snakeX][snakeY].setBackground(Color.GREEN);
        timer = new Timer(100, this);
        timer.start();
        
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    if (direction != 2)
                        direction = 0;
                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                    if (direction != 3)
                        direction = 1;
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    if (direction != 0)
                        direction = 2;
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    if (direction != 1)
                        direction = 3;
                }
            }
        });
        
        setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        if (direction == 0)
            snakeY--;
        else if (direction == 1)
            snakeX--;
        else if (direction == 2)
            snakeY++;
        else if (direction == 3)
            snakeX++;
        
        if (snakeX < 0 || snakeX > 19 || snakeY < 0 || snakeY > 19) {
            timer.stop();
            JOptionPane.showMessageDialog(this, "You Lost!");
            return;
        }
        
        if (gameBoardState[snakeX][snakeY] == 1) {
            timer.stop();
            JOptionPane.showMessageDialog(this, "You Lost!");
            return;
        }
        
        gameBoardState[snakeX][snakeY] = 1;
        gameBoard[snakeX][snakeY].setBackground(Color.GREEN);
        
        if (snakeLength == 1) {
            int foodX = (int) (Math.random() * 20);
            int foodY = (int) (Math.random() * 20);
            gameBoard[foodX][foodY].setBackground(Color.RED);
            gameBoardState[foodX][foodY] = 2;
        }
        
        if (gameBoardState[snakeX][snakeY] == 2) {
            snakeLength++;
            int foodX = (int) (Math.random() * 20);
            int foodY = (int) (Math.random() * 20);
            gameBoard[foodX][foodY].setBackground(Color.RED);
            gameBoardState[foodX][foodY] = 2;
        } else {
            int tailX = 0, tailY = 0;
            boolean found = false;
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 20; j++) {
                    if (gameBoardState[i][j] == snakeLength) {
                        tailX = i;
                        tailY = j;
                        found = true;
                        break;
                    }
                }
                if (found)
                    break;
            }
            gameBoardState[tailX][tailY] = 0;
            gameBoard[tailX][tailY].setBackground(null);
        }
    }
    
    public static void main(String[] args) {
        new SnakeGame();
    }

}

这个游戏有一个20x20的游戏板,使用JButton实现。游戏板的每个单元格都有一个状态:0表示空白,1表示蛇,2表示食物。蛇的方向由用户通过键盘控制。每100毫秒,Timer将向前移动蛇的头部,如果它遇到了游戏板的边界或蛇的身体,则游戏结束。如果蛇吃了食物,则它会变长并且新的食物会随机出现在游戏板上。当蛇移动时,它的尾巴也会移动,但是如果它没有吃到食物,则尾巴的单元格将变为空白状态。游戏结束时,一个对话框将显示“您输了!”消息。

用Java贪吃蛇游戏的代码

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

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