食物闪烁,而不是在躲闪游戏中吃东西
旧代码
目前,我正在尝试制作一款游戏,其中鼠标控制一个可以移动的区块,收集小球。 每当一个球产生时,敌人就会出现,并沿直线移动。
我的程序试图实现这一点,但是所有敌人都立刻产生并且只向右移动。 食物在食用时不会消失。 相反,当我移动鼠标时,食物在整个地方闪烁,敌人全部移向右侧。 我不确定如何让食物出现一次并留在那里,然后当玩家吃掉它时,它会消失并产生另外一种食物,并产生一个敌人以及左右或上下弹跳的敌人离开墙壁。
我想让食物在一个地方产卵,然后当我吃它时,删除食物并随机产生另一种食物。
我正在尝试制作类似于此的内容。
谢谢你的帮助。
编辑:基于@ AJC答案中的psuedocode的新代码
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();
}
}
}
你的代码有几个问题
新的伪代码:
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/66573.html