Food flickering and not being eaten in dodge game

old code

Currently, I am trying to make a game in which the mouse controls a block that will move around, gathering little balls. Every time a ball spawns, an enemy will appear, moving in a linear path.

My program attempts to implement that, however the enemies all spawn at once and are moving right only. The food, when eaten, does not disappear. Instead, when I move the mouse, the food flickers all over the place and the enemy moves all towards the right side. I'm not sure how to make the food appear once and stay there, then when the player eats it, it disappears and spawns another one somewher else, and spawns an enemy as well that moves in left-right or up-down and bounces off the walls.

I would like to make the food spawn in one place, then when I eat it, delete the food and spawn another one randomly.

I am trying to make something similar to this.

Thanks for your help.

EDIT: New code based off of the psuedocode in @AJC's answer

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();
        }
    }
}

There are several problems in your code

  • You are setting the food to a random position every game loop, so it will not stay in one place. You need to remember the position of the current food, and set a new position only when the player touches it
  • The enemies are controlled all by one variable, so they will all move in the same direction obviously
  • There are some other logic flaws, like the updates for the other objects get skipped when the player doesn't move
  • I also suggest organizing the code into sections, like separating the draw and update cycles
  • New pseudocode:

    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
    
    链接地址: http://www.djcxy.com/p/66574.html

    上一篇: Chrome扩展程序中的AdSense

    下一篇: 食物闪烁,而不是在躲闪游戏中吃东西