这是一个简单的贪吃蛇游戏代码,使用Java语言实现。

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

public class SnakeGame extends JFrame implements KeyListener {
    private final int WIDTH = 500;
    private final int HEIGHT = 500;
    private final int SNAKE_SIZE = 10;
    private final int APPLE_SIZE = 10;
    private final int MAX_APPLES = 3;
    private int x, y, dx, dy, appleCount;
    private int[] appleX = new int[MAX_APPLES];
    private int[] appleY = new int[MAX_APPLES];
    private boolean left, right, up, down, gameOver;
    private Timer timer;
    private Image buffer;
    private Graphics bufferGraphics;

    public SnakeGame() {
        setTitle("Snake Game");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        setLocationRelativeTo(null);
        addKeyListener(this);
        buffer = createImage(WIDTH, HEIGHT);
        bufferGraphics = buffer.getGraphics();
        initGame();
        timer = new Timer(50, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gameLoop();
            }
        });
        timer.start();
    }

    private void initGame() {
        x = WIDTH / 2;
        y = HEIGHT / 2;
        dx = SNAKE_SIZE;
        dy = 0;
        appleCount = 0;
        for (int i = 0; i < MAX_APPLES; i++) {
            appleX[i] = (int) (Math.random() * (WIDTH - APPLE_SIZE));
            appleY[i] = (int) (Math.random() * (HEIGHT - APPLE_SIZE));
        }
    }

    private void gameLoop() {
        if (!gameOver) {
            bufferGraphics.clearRect(0, 0, WIDTH, HEIGHT);
            bufferGraphics.setColor(Color.RED);
            for (int i = 0; i < MAX_APPLES; i++) {
                bufferGraphics.fillRect(appleX[i], appleY[i], APPLE_SIZE, APPLE_SIZE);
            }
            bufferGraphics.setColor(Color.BLACK);
            bufferGraphics.fillRect(x, y, SNAKE_SIZE, SNAKE_SIZE);
            x += dx;
            y += dy;
            if (x < 0 || x > WIDTH - SNAKE_SIZE || y < 0 || y > HEIGHT - SNAKE_SIZE) {
                gameOver = true;
            }
            for (int i = 0; i < MAX_APPLES; i++) {
                if (x < appleX[i] + APPLE_SIZE && x + SNAKE_SIZE > appleX[i] &&
                        y < appleY[i] + APPLE_SIZE && y + SNAKE_SIZE > appleY[i]) {
                    appleX[i] = (int) (Math.random() * (WIDTH - APPLE_SIZE));
                    appleY[i] = (int) (Math.random() * (HEIGHT - APPLE_SIZE));
                    appleCount++;
                    if (appleCount == MAX_APPLES) {
                        gameOver = true;
                    }
                }
            }
            repaint();
        }
    }

    public void paint(Graphics g) {
        g.drawImage(buffer, 0, 0, this);
    }

    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
            left = true;
            right = false;
            up = false;
            down = false;
            dx = -SNAKE_SIZE;
            dy = 0;
        } else if (key == KeyEvent.VK_RIGHT) {
            left = false;
            right = true;
            up = false;
            down = false;
            dx = SNAKE_SIZE;
            dy = 0;
        } else if (key == KeyEvent.VK_UP) {
            left = false;
            right = false;
            up = true;
            down = false;
            dx = 0;
            dy = -SNAKE_SIZE;
        } else if (key == KeyEvent.VK_DOWN) {
            left = false;
            right = false;
            up = false;
            down = true;
            dx = 0;
            dy = SNAKE_SIZE;
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public static void main(String[] args) {
        new SnakeGame();
    }
}
``
写一段 贪吃蛇 游戏代码Java

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

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