编辑:(已回答问题)
所以我有一个控制 x 和 y 并使其发生变化的线程。我的问题是,当蛇的 body 被放置在 GUI 中时,它会将它直接放在头顶而不是它后面的 12 个空间。我知道它为什么会这样,但我不确定如何解决这个问题。
据我了解,它先绘制 body ,然后绘制头部,但它使用相同的 x 和 y 空间......真正需要做的是它需要绘制头部,通过线程,然后绘制 body 部位,以便它在头部后面留出空间(因为头部会向前移动)......等等......我只是不知道该怎么做。
这是 Thread runner,我非常怀疑你是否真的需要它,但只是为了确定
class ThreadRunnable extends Thread implements Runnable {
private boolean allDone = false;
private boolean RIGHT;
private boolean LEFT;
private boolean UP;
private boolean DOWN;
public ThreadRunnable (boolean up, boolean right, boolean down, boolean left){
UP = up;
RIGHT = right;
DOWN = down;
LEFT = left;
}
public void run() {
if(UP == true) {
try {
while(UP) {
if (y <= yBase && y >= 0) {
y = y - 12;
}
else {
y = yBase;
}
Thread.sleep(40);
repaint();
if (allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
if(RIGHT == true) {
try {
while(RIGHT) {
if (x <= xBase && x >= 0) {
x = x+12;
}
else {
x = 0;
}
Thread.sleep(40);
repaint();
if(allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
if(DOWN == true) {
try {
while(DOWN) {
if (y <= yBase && y >= 0) {
y = y+12;
}
else {
y = 0;
}
Thread.sleep(40);
repaint();
if (allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
if(LEFT == true) {
try {
while(LEFT) {
if (x <= xBase && x >= 0) {
x = x-12;
}
else {
x = xBase;
}
Thread.sleep(40);
repaint();
if (allDone) {
return;
}
}
}
catch (InterruptedException exception) {
}
finally {
}
}
}
}
这是代码的“绘图”部分
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBorder(BorderFactory.createLineBorder(Color.WHITE));
this.setBackground(Color.black);
if (numOfFood == 1) {
g.setColor(Color.BLUE);
g.fillRect(foodX,foodY,12,12); //Paints the food
}
else {
foodX = random.nextInt(105)*12; //Random x for food position after snake eats
foodY = random.nextInt(59)*12; //Random y for food position after snake eats
numOfFood = 1;
}
Rectangle headRect = new Rectangle( x, y, 12, 12 ); //The head (not painted)
Rectangle foodRect = new Rectangle(foodX, foodY, 12, 12); //The food (not painted)
if ( body.size() >= 0) {
body.add(new BodyPart( x, y, 12, 12));
body = new ArrayList<BodyPart>( body.subList( body.size() - n, body.size() ) );
g.setColor(Color.RED);
for ( int i = body.size() - 1; i >= body.size() - (n-1); i-- ) {
g.fillRect( body.get( i ).getX(), body.get( i ).getY(), body.get( i ).getWidth(), body.get( i ).getHeight() ); //This is calling a class that will get these parts of the array list (x,y,width,height)
}
}
g.setColor(Color.RED);
g.fillRect(x,y,12,12); //Draws head
g.setColor(Color.WHITE);
g.fillRect(x+2,y+2,8,8); //This is a white square in the middle of the head to show that it is the head
if (headRect.intersects(foodRect)) {
numOfFood = 0;
n++;
}
}
编辑:下面回答的问题
所需要的只是移动
setBorder(BorderFactory.createLineBorder(Color.WHITE));
和
this.setBackground(Color.black);
到代码的较早部分而不是那里的绘图方法...
我也将我的 ArrayList 更改为一个普通的 Array,并让它将头部位置插入到 Array 的开头,然后让它在下一次打印(在头部移动之后)
g.setColor(Color.RED);
//Prints the body parts
if (n > 0) {
for (int i = 0;i < n; ++i) {
g.fillRect(body[i][0],body[i][1],12,12);
}
}
//Inserts the head location
for (int i = n-1;i >= 0; --i) {
body[i+1][0] = body[i][0];
body[i+1][1] = body[i][1];
if (i == 0) {
body[i][0] = x;
body[i][1] = y;
}
}
非常感谢大家的帮助
最佳答案
- 将头向前移动一个。
- 将 body 部分放在头部所在的位置。
- 删除最后一个 body 片段。
其他 body 部分都不需要移动。
关于java - 蛇游戏程序-制作蛇的 body ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7541660/