中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

Java實現貪吃蛇游戲

小云
97
2023-08-09 13:45:44
欄目: 編程語言

下面是一個簡單的Java實現貪吃蛇游戲的示例代碼:

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SnakeGame extends JFrame implements KeyListener {
private static final int WIDTH = 400;
private static final int HEIGHT = 400;
private static final int UNIT_SIZE = 20;
private static final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);
private static final int DELAY = 75;
private final List<Integer> snakeX = new ArrayList<>();
private final List<Integer> snakeY = new ArrayList<>();
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;
private final Random random;
public SnakeGame() {
random = new Random();
setTitle("Snake Game");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
addKeyListener(this);
startGame();
}
private void startGame() {
newApple();
running = true;
timer = new Timer(DELAY, e -> gameLoop());
timer.start();
snakeX.add(WIDTH / 2);
snakeY.add(HEIGHT / 2);
}
private void gameLoop() {
move();
checkCollision();
repaint();
}
private void move() {
for (int i = snakeX.size() - 1; i > 0; i--) {
snakeX.set(i, snakeX.get(i - 1));
snakeY.set(i, snakeY.get(i - 1));
}
switch (direction) {
case 'U':
snakeY.set(0, snakeY.get(0) - UNIT_SIZE);
break;
case 'D':
snakeY.set(0, snakeY.get(0) + UNIT_SIZE);
break;
case 'L':
snakeX.set(0, snakeX.get(0) - UNIT_SIZE);
break;
case 'R':
snakeX.set(0, snakeX.get(0) + UNIT_SIZE);
break;
}
}
private void checkCollision() {
// 檢查是否撞到自己
for (int i = snakeX.size() - 1; i > 0; i--) {
if (snakeX.get(0).equals(snakeX.get(i)) && snakeY.get(0).equals(snakeY.get(i))) {
gameOver();
}
}
// 檢查是否撞到邊界
if (snakeX.get(0) < 0 || snakeX.get(0) >= WIDTH || snakeY.get(0) < 0 || snakeY.get(0) >= HEIGHT) {
gameOver();
}
// 檢查是否吃到蘋果
if (snakeX.get(0).equals(appleX) && snakeY.get(0).equals(appleY)) {
snakeX.add(snakeX.get(snakeX.size() - 1));
snakeY.add(snakeY.get(snakeY.size() - 1));
newApple();
}
}
private void gameOver() {
running = false;
timer.stop();
}
private void newApple() {
appleX = random.nextInt((WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = random.nextInt((HEIGHT / UNIT_SIZE)) * UNIT_SIZE;
}
@Override
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
// 畫網格
for (int i = 0; i < WIDTH / UNIT_SIZE; i++) {
g.drawLine(i * UNIT_SIZE, 0, i * UNIT_SIZE, HEIGHT);
}
for (int i = 0; i < HEIGHT / UNIT_SIZE; i++) {
g.drawLine(0, i * UNIT_SIZE, WIDTH, i * UNIT_SIZE);
}
// 畫蛇
for (int i = 0; i < snakeX.size(); i++) {
g.setColor(Color.GREEN);
g.fillRect(snakeX.get(i), snakeY.get(i), UNIT_SIZE, UNIT_SIZE);
}
// 畫蘋果
g.setColor(Color.RED);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
if (direction

0
永州市| 德州市| 高阳县| 河北省| 丰都县| 柘城县| 特克斯县| 罗田县| 财经| 五指山市| 德化县| 通山县| 大丰市| 宽甸| 万安县| 杭锦旗| 舟曲县| 昌平区| 姚安县| 宜兰县| 鸡西市| 贺兰县| 翁牛特旗| 通城县| 西平县| 广灵县| 泰宁县| 长葛市| 济南市| 资中县| 松滋市| 浦东新区| 晋宁县| 白城市| 睢宁县| 上杭县| 阿尔山市| 夏津县| 石泉县| 沈丘县| 平山县|