gpt4 book ai didi

java - 蛇游戏 : how to stop the game when the snake eat itself

转载 作者:搜寻专家 更新时间:2023-11-01 03:35:58 25 4
gpt4 key购买 nike

我正在制作一个贪吃蛇游戏,除了蛇吃自己的部分外,它运行良好。

到目前为止,这是我的代码:

障碍类

class Obstacle {
int x, y;

public Obstacle(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(x, y, 10, 10);
}
}

食品类

class Food {
int x, y;

public Food(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
}
}

body 等级

class Body {
int x, y;

public Body(int x, int y) {
this.x = x;
this.y = y;
}

public void draw(Graphics g) {
g.setColor(Color.green);
g.fillRect(x, y, 10, 10);
g.setColor(Color.black);
g.drawRect(x, y, 10, 10);
}
}

屏幕类

class Screen extends JPanel implements ActionListener, KeyListener {
int WIDTH = 800, HEIGHT = 800;
public static int time = 50;
Timer t;
int x = 400, y = 400;
boolean right = false, left = false, up = false, down = false;
public static int grade = 0;

LinkedList<Food> foods;
LinkedList<Body> snake;
LinkedList<Obstacle> block;

Body b;
Food f;
Obstacle k;

Random rand = new Random();
int size = 3;

public Screen() {

init();
}


public void init() {

block = new LinkedList<Obstacle>();
foods = new LinkedList<Food>();
snake = new LinkedList<Body>();

level();
t = new Timer(time, this);
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void tracking() {

if (x < 0 || x > 800 || y < 0 || y > 800) {
hit();
}
}

public void level() {
Object[] options = {"Level 1", "Level 2", "Level 3"};

int option = JOptionPane.showOptionDialog(null, "Please Select a Level", "Level Option",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);

switch (option) {
case JOptionPane.YES_OPTION:
System.out.println("Before: " + time);
time = 50;
System.out.println("After: " + time);
break;
case JOptionPane.NO_OPTION:
System.out.println("Before: " + time);
time = 30;
System.out.println("After: " + time);
break;
case JOptionPane.CANCEL_OPTION:
System.out.println("Before: " + time);
time = 10;
System.out.println("After: " + time);
break;
}
}

public void hit() {
t.stop();
if (JOptionPane.showConfirmDialog(null, "Snake: You hit the wall! \nRestart the Game?", "OUCH!!!",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
x = 400;
y = 400;
while (snake.size() != 3) {
snake.remove();
size = 3;
}
init();

} else {
System.exit(0);
}
}

public void move() {
if (right) x += 10;
if (left) x -= 10;
if (up) y -= 10;
if (down) y += 10;
}

public void snake() {
if (snake.size() == 0) {
b = new Body(x, y);
snake.add(b);
}
move();
b = new Body(x, y);
snake.add(b);

if (snake.size() > size) {
snake.remove(0);
}
}

public void food() {
if (foods.size() == 0) {
int ok = 0;
int rx = 0;
int ry = 0;
while (ok != 1) {
rx = (int) (rand.nextInt(700) + 1);
ry = (int) (rand.nextInt(700) + 1);

if ((rx % 10) == 0 && (ry % 10) == 0) {
ok = 1;
}
}
f = new Food(rx, ry);
foods.add(f);
}

if (snake.get(snake.size() - 1).x == foods.get(0).x && snake.get(snake.size() - 1).y == foods.get(0).y) {
foods.remove();
size++;
grade += 100;
}
}

public void block() {
if (block.size() == 0) {
int ok = 0;
int rx = 0;
int ry = 0;
while (ok != 15) {
rx = (int) (rand.nextInt(750) + 1);
ry = (int) (rand.nextInt(750) + 1);

if ((rx % 10) == 0 && (ry % 10) == 0) {
k = new Obstacle(rx, ry);
block.add(k);
int temp = 10;

int r = (int) (rand.nextInt(2) + 1);

for (int i = 0; i < 5; i++) {

if (r == 1) {
k = new Obstacle(rx + temp , ry);
} else if (r == 2) {
k = new Obstacle(rx , ry + temp);
}
block.add(k);

if ((k.x == foods.get(0).x && k.y == foods.get(0).y) ||
(k.x == 400 && k.y == 400) ||
((k.x >= 350 && k.x <= 450) && (k.y >= 350 && k.y <= 450))) {
block.remove(k);
}

temp += 10;
}
ok++;
}
}
}

for (int i = 0; i < block.size(); i++) {
if (snake.get(snake.size() - 1).x == block.get(i).x && snake.get(snake.size() - 1).y == block.get(i).y) {
hit();
}
}
}

public void update() {
snake();
food();
block();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(Color.BLACK);
for (int i = 1; i < WIDTH / 10; i++) {
g.drawLine(i * 10, 0, i * 10, HEIGHT);
}
for (int i = 1; i < HEIGHT / 10; i++) {
g.drawLine(0, i * 10, WIDTH, i * 10);
}

for (int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}

for (int i = 0; i < foods.size(); i++) {
foods.get(i).draw(g);
}

for (int i = 0; i < block.size(); i++) {
block.get(i).draw(g);
}
}

@Override
public void actionPerformed(ActionEvent e) {
update();
tracking();
repaint();
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT && left == false) {
up = false;
down = false;
right = true;

}
if (key == KeyEvent.VK_LEFT && right == false) {
up = false;
down = false;
left = true;
}
if (key == KeyEvent.VK_UP && down == false) {
left = false;
right = false;
up = true;
}
if (key == KeyEvent.VK_DOWN && up == false) {
left = false;
right = false;
down = true;
}
}

@Override
public void keyReleased(KeyEvent e) {

}
}

和主类 SnakeGame

public class SnakeGame extends JFrame {
public static void main(String[] args) {
JFrame f = new JFrame();
Screen s = new Screen();
f.add(s);
f.setSize(810, 810);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

所以一开始我决定比较蛇头吃尾部的时候,但是因为我的蛇是动的;尾部的 x 和 y 在头部的 x 和 y 之后,所以比较总是正确的

我也想到了当蛇改变方向,然后做一些代码来跟踪x y坐标和它的尾部x y坐标,但我不知道如何走

有没有其他方法可以让游戏在蛇吃自己的时候停止?

最佳答案

当您调用 move(); 时您获得了 x 的新值和 y ,蛇的“头”移动到的位置。我看到你的 LinkedList<Body> snake;有蛇 body 各部分的位置。

如果每次调用 move(); ,你将你的下一个“头部”位置与蛇的所有其他部分进行比较,你会发现它是否命中。

像这样:

        for (int i = 0; i < snake.size(); i++) {

if ((x == snake.get(i).x) && y == snake.get(i).y) {
hit();
}
}

游戏将从 hit(); 开始因为“头”的xy匹配您的全局 xy变量,所以你必须修复它(即等待 move(); 调用开始检查)。

编辑:

这是一个示例修改:

添加boolean started = false;给你的Screen class :

更改 snake()检查碰撞的方法:

public void snake() {

if (snake.size() == 0) {
b = new Body(x, y);
snake.add(b);

}
move();

if (started) {

for (int i = 0; i < snake.size(); i++) {

if ((x == snake.get(i).x) && y == snake.get(i).y) {
hit();
}
}

}
b = new Body(x, y);
snake.add(b);

if (snake.size() > size) {
snake.remove(0);
}

}

添加started = true;food() 的结尾方法(找不到更好的地方,但我认为你可以!):

public void food() {
if (foods.size() == 0) {
int ok = 0;
int rx = 0;
int ry = 0;
while (ok != 1) {
rx = (int) (rand.nextInt(700) + 1);
ry = (int) (rand.nextInt(700) + 1);

if ((rx % 10) == 0 && (ry % 10) == 0) {
ok = 1;
}
}
f = new Food(rx, ry);
foods.add(f);
}

if (snake.get(snake.size() - 1).x == foods.get(0).x
&& snake.get(snake.size() - 1).y == foods.get(0).y) {
foods.remove();
size++;
grade += 100;
started = true;
}
}

它应该可以工作!

Snake hitting itself

关于java - 蛇游戏 : how to stop the game when the snake eat itself,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31552958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com