闪避游戏食物闪烁不吃

Food flickering and not being eaten in dodge game

old code

目前,我正在尝试制作一个游戏,其中鼠标控制一个会四处移动的方块,收集小球。每次生成一个球时,都会出现一个敌人,沿直线路径移动。

我的程序试图实现这一点,但是所有敌人都同时生成并且只向右移动。食物被吃掉后,不会消失。相反,当我移动鼠标时,食物会到处闪烁,敌人会全部向右移动。我不确定如何让食物出现一次并留在那里,然后当玩家吃掉它时,它会消失并在其他地方产生另一个,并产生一个左右移动或上下移动并弹跳的敌人离开墙壁。

我想让食物在一个地方生成,然后当我吃它的时候,删除食物并随机生成另一个。

我正在尝试 make something similar to this.

感谢您的帮助。

编辑:新代码基于@AJC 回答中的伪代码

import java.util.ArrayList;
import java.util.Random;

public class Game{
    public static void main(String[] args) {
        StdDraw.setXscale(0,100);
        StdDraw.setYscale(0,100);

        int foodX = 0;
        int foodY = 0;

        ArrayList enemyXPos = new ArrayList();
        ArrayList enemyYPos = new ArrayList();
        ArrayList enemySpeeds = new ArrayList();
        int score = 0;
        Random rand = new Random();

        while (true) {
            double playerX = StdDraw.mouseX();
            double playerY = StdDraw.mouseY();
            StdDraw.clear();

            int enemyX = 0, enemyY = 0;


            if (playerX == foodX && playerY == foodY) {
                score += 1;

                foodX = rand.nextInt(100);
                foodY = rand.nextInt(100);


                enemyX = rand.nextInt(100);
                enemyY = rand.nextInt(100);

                enemyXPos.add(enemyX);
                enemyYPos.add(enemyY);
                int enemySpeed = rand.nextInt(20);
                enemySpeeds.add(enemySpeed);
            }

            for (int j = 0; j < enemyXPos.size()-1; j++) {
                if ((int) enemyXPos.get(j) == playerX && (int) enemyYPos.get(j) == playerY) {
                    break;
                }

            }
            for (int j = 0; j < enemyXPos.size()-1; j++) {
                enemyXPos.add(j, (int) enemyXPos.get(j) + (int) enemySpeeds.get(j));
                enemyYPos.add(j, (int) enemyXPos.get(j) + (int) enemySpeeds.get(j));
            }


            StdDraw.setPenColor(StdDraw.BLUE);
            StdDraw.filledCircle(playerX, playerY, 5);

            StdDraw.setPenColor(StdDraw.GREEN);
            StdDraw.filledCircle(foodX, foodY, 2);

            for (int j = 0; j < enemyXPos.size()-1; j++) {
                StdDraw.setPenColor(StdDraw.RED);
                StdDraw.filledCircle(enemyX, enemyY, 2);
            }
            StdDraw.show();
        }
    }
}

你的代码有几个问题

  1. 你在每个游戏循环中将食物设置到一个随机位置,所以它不会留在一个地方。需要记住当前食物的位置,并设置一个新的位置只有当玩家触摸它
  2. 敌人都是由一个变量控制的,所以他们显然都会朝着同一个方向移动
  3. 还有其他一些逻辑缺陷,例如当玩家不移动时会跳过其他对象的更新
  4. 我还建议将代码组织成多个部分,例如将绘制和更新周期分开

新伪代码:

Variables:
 - Current food position
 - List of enemy positions
 - List of enemy velocities

Game loop:
    //Update stuff
    Get mouse location
    Set player location to mouse location

    If player is touching food then
        Increase score
        Set food position to random position
        Spawn new enemy:
            Add new random position to enemy position list
            Add new random velocity to enemy velocity list
    End if

    For each enemy:
        If enemy is touching player than
            End game
        End if
    End for

    For each enemy:
        Update enemy location:
            Add velocity of enemy to position of enemy
            (posX=posX+velX)
            (posY=posY+velY)
    End for

    //Draw stuff
    Draw player
    Draw food
    For each enemy:
        Draw that enemy
    End for
End game loop