gpt4 book ai didi

java - 我无法让蛇的 body 部位沿着头部的路径移动

转载 作者:行者123 更新时间:2023-12-01 20:13:26 26 4
gpt4 key购买 nike

Board.Java(程序逻辑部分)

package snake;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Board extends JPanel implements KeyListener, Runnable{
private final int WIDTH = 500, HEIGHT = 500,TWIDTH = WIDTH/25, THEIGHT = HEIGHT/25, RAND_POS = 29;
private int foodLocX, foodLocY, snakeHeadX = 240, snakeHeadY = 240;;
private boolean running = true, up = false,down = false,left = false,right = true;
private JFrame frame;
private Thread t;
private ArrayList<BodyPart> body;
public Board() {
frame = new JFrame("Snake");
initBoard();
foodLocX = snakeHeadX + 8*TWIDTH;
foodLocY = snakeHeadY;
body = new ArrayList<BodyPart>();
body.add(new BodyPart(snakeHeadX,snakeHeadY, TWIDTH));
t = new Thread(this);
}

public void initBoard() {
frame.setPreferredSize(new Dimension(518,540));
this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
frame.addKeyListener(this);
frame.pack();
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private void locateApple() {

int r = (int) (Math.random() * RAND_POS);
foodLocX = ((r * TWIDTH));

r = (int) (Math.random() * RAND_POS);
foodLocY = ((r * THEIGHT));

while(foodLocX >= WIDTH || foodLocY >= HEIGHT || foodLocX < 0 || foodLocY < 0) {
r = (int) (Math.random() * RAND_POS);
foodLocX = ((r * TWIDTH));

r = (int) (Math.random() * RAND_POS);
foodLocY = ((r * THEIGHT));
}
}

public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.GREEN);
for(int i = 0; i<WIDTH;i+=TWIDTH) {
for(int ii = 0; ii<HEIGHT;ii+=THEIGHT) {
g.fillRect(i, ii, TWIDTH, THEIGHT);
}
}

if(foodLocX == body.get(0).getX() && foodLocY == body.get(0).getY()) {
int x = body.get(body.size()-1).getX();
int y = body.get(body.size()-1).getY();

if(right) {
body.add(new BodyPart(x+20,y,TWIDTH));
}if(left) {
body.add(new BodyPart(x-20,y,TWIDTH));
}if(down) {
body.add(new BodyPart(x,y+20,TWIDTH));
}if(up) {
body.add(new BodyPart(x,y-20,TWIDTH));
}
locateApple();
}
g.setColor(Color.RED);
g.fillRect(foodLocX, foodLocY, TWIDTH, THEIGHT);
g.setColor(Color.BLACK);
for(int i = 0; i< body.size();i++) {
body.get(i).draw(g);
}
repaint();
}

@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP && !down) {
up = true;
down = false;
left = false;
right = false;
try {
Thread.sleep(55,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getKeyCode() == KeyEvent.VK_DOWN && !up) {
up = false;
down = true;
left = false;
right = false;
try {
Thread.sleep(55,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getKeyCode() == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
left = false;
right = true;
try {
Thread.sleep(55,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(e.getKeyCode() == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
right = false;
try {
Thread.sleep(100,666);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}
@Override
public void keyReleased(KeyEvent arg0){};
@Override
public void keyTyped(KeyEvent arg0){}

//public void addBodyPart() {
//body.add(previousHead); // Part that I left off on
//}

public void moveSnake() {
if(right) {
for(int i = 0; i<body.size();i++) {
body.get(i).setX(body.get(i).getX()+20);
}
}if(left) {
for(int i = 0; i<body.size();i++) {
body.get(i).setX(body.get(i).getX()-20);
}
}if(down) {
for(int i = 0; i<body.size();i++) {
body.get(i).setY(body.get(i).getY()+20);
}
}if(up) {
for(int i = 0; i<body.size();i++) {
body.get(i).setY(body.get(i).getY()-20);
}
}
}
@Override
public void run() {
while(running) {

if(body.get(0).getX()<0 || body.get(0).getX()>480 || body.get(0).getY()<0 || body.get(0).getY()>480){
System.out.println("Game Over!\nYour snake was " + body.size() + " blocks long");
endGame();
}

moveSnake();

repaint();
try {
Thread.sleep(75,666);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void startGame() {
t.start();
}

public void endGame() {
t.stop();
}
}

Main.java(可运行程序)

package snake;

public class Main {

public static void main(String[] args) {
Board b = new Board();
b.startGame();
}

}

BodyPart.java

package snake;

import java.awt.Color;
import java.awt.Graphics;

public class BodyPart {

private int x,y,tilesize;
public BodyPart(int x, int y, int tilesize) {
this.x = x;
this.y = y;
this.tilesize = tilesize;
}

public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(x,y,tilesize,tilesize);
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}
}

这让我很困扰,因为它将 body 部位添加到数组列表中,但是一旦我移动,这些部位就不会跟随 body 。我迷路了,不知道该怎么办。任何帮助将不胜感激。非常感谢大家。

-AbysssCoder

最佳答案

因为在 moveSnake() 方法中,您正在迭代 body 并将 body 的每个部分移动相同的量(您拿起整条蛇并移动它,而不是拥有它“滑行”)。

您不需要移动整个蛇 - 您需要使用箭头键来移动“头”或“父级”。然后,您需要将蛇的其余部分编码为“子级”,其中蛇的每个新部分将其新位置设置为其父级的当前位置。

例如,如果您的蛇只有 2 个单位长,则箭头键将确定第一个单位(仅“头”)的位置。然后对于每个 bodypart : yoursnake (psuedocode),您可以将新位置设置为 bodyPart.getParentBodyPart.getLocation() 或类似逻辑的内容。

这很丑,因为我只是把它和你拥有的东西放在一起 - 你绝对可以清理它。但它会起作用:

public void moveSnake() {
if(right) {
// iterate from the tail forward so we don't overwrite positions that we need
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setX(body.get(i).getX()+20);
} else {
// we dont know what direct we are moving, so we need x and y
body.get(i).setX(body.get(i-1).getX());
body.get(i).setY(body.get(i-1).getY());
}

}
}if(left) {
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setX(body.get(i).getX()-20);
} else {
body.get(i).setX(body.get(i-1).getX());
body.get(i).setY(body.get(i-1).getY());
}

}
}if(down) {
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setY(body.get(i).getY()+20);
} else {
body.get(i).setY(body.get(i-1).getY());
body.get(i).setX(body.get(i-1).getX());
}

}
}if(up) {
for(int i = body.size()-1; i>=0;i--) {
if (i == 0) {
body.get(i).setY(body.get(i).getY()-20);
} else {
body.get(i).setY(body.get(i-1).getY());
body.get(i).setX(body.get(i-1).getX());
}

}
}
}

关于java - 我无法让蛇的 body 部位沿着头部的路径移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58961813/

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