亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
涞水县| 泗洪县| 牡丹江市| 马山县| 枞阳县| 阳西县| 河南省| 拜泉县| 行唐县| 龙州县| 汤阴县| 贡嘎县| 满城县| 文成县| 肇庆市| 宜兰县| 涟水县| 崇州市| 茌平县| 大理市| 吉木乃县| 通河县| 金山区| 郎溪县| 宿州市| 赣州市| 呼伦贝尔市| 安龙县| 十堰市| 凌海市| 华安县| 石景山区| 石柱| 沂源县| 星座| 岳阳县| 永济市| 隆安县| 静安区| 泸溪县| 安顺市|