处理,当形状消失时如何绘制形状

Processing, how to redraw shapes when they dissappear

大家好,我制作了这款游戏,用户可以避开从屏幕右侧飞来的外星人,然后他们从屏幕左侧经过。我需要外星人离开屏幕左侧后从右侧重新出现。我该怎么做呢? 这是我现有的代码:

编辑: 在 main class

下添加了 alien class
PImage background;
int x=0; //global variable background location

Alien alien1;
Alien alien2;
Alien alien3;
Defender user1;


void setup(){
 size(800,400);
 background = loadImage("spaceBackground.jpg");
 background.resize(width,height);

 alien1 = new Alien(800,100,5);
 alien2 = new Alien(800,200,5);
 alien3 = new Alien(800,300,5);

 user1 = new Defender(10,height/2);

}
void draw ()
{
 drawBackground();
 alien1.move();
 alien1.render();
 alien2.move();
 alien2.render();
 alien3.move();
 alien3.render();
 user1.render();
}  

void drawBackground()
{
 image(background, x, 0); //draw background twice adjacent
 image(background, x+background.width, 0);
 x -=4;
 if(x == -background.width)
 x=0; //wrap background
}

void keyPressed()
{

  if(key == CODED) {
     if (keyCode == UP) {
       user1.y = user1.y - 5;
     }
     else if (keyCode == DOWN)
     {
       user1.y = user1.y + 5;
     }
  }

}


final color Alien1 = color(0,255,0);
final color Alien2 = color(50,100,0);

class Alien
{
 int x,y;
 int speedX, speedY;

 Alien(int x, int y, int speedX)
 {
  this.x = x;
  this.y = y;
  this.speedX = speedX;
 }

 void move()
 {
   x=x-speedX;
   float stepY = random(-5,5);
   y = y + (int)stepY;
 }

 //draw an alien
 void render()
 {
 fill(Alien1);
 ellipse(x,y,30,30);
 fill(Alien2);
 ellipse(x,y,50,15);

 }



}


如果您上传 Alien class 那么我们可以给出更清晰的指示,但我们的想法是您应该在 move() 方法中添加以下逻辑。

void move()
 {
   x=x-speedX;
   float stepY = random(-5,5);
   y = y + (int)stepY;
   if(this.x < 0) {
      this.x = 800; // or width, startingPosition, ...
   }
 }

编辑:添加了外星人 class,因此我的解决方案适用于代码。