Hi can anyone help me with my game?

i am trying to make this image appear at random heights continuously

class Chaser {
int x;
int y;
float ran;
int rani;
PImage carimageY, carimageN, carimageW;

Chaser()
{
x = Lane();
y = height-50;
carimageY=loadImage(“data/car1.png”);
carimageY.resize(80, 80);

}

void render()
{
image(carimageY, x, y);
}

boolean collided(){
return false;
}

int Lane() {
int[] lanes = {-26,22,52,92,124,172,200,236,275,325,362,400,430,470,500,540,574,616,650,690};
ran =random(18);
rani = round(ran);

return lanes[rani];

}

void move(){
y=y-3;
}

void collide()
{
if(y<0){
y = height-200;
x=Lane();
}
if(collided()==true){
x=Lane();
}
}

void update()
{
move();
collide();
render();
}
}

Hello,

and welcome to the community!

Great to have you here!

void collide()
{
if(y<0){
y = height + random(200);  // that makes the car reappear below the screen
x=Lane();
}

Chrisir

1 Like

Thanks. I entered the code but I’m getting an error

show your entire code and describe your error.

what does it say and line number?

1 Like

Here is a pic of the error I’m getting

Hello,

Please format your code as a courtesy to everyone:
https://discourse.processing.org/faq#format-your-code

class Chaser {
  int x;
  int y;
  float ran;
  int rani;
  PImage carimageY, carimageN, carimageW;

  Chaser()
  {
    x = Lane();
    y = height-50;    
    carimageY=loadImage("data/car1.png");
    carimageY.resize(80, 80);
  }

  void render()
  {
    image(carimageY, x, y);
  }

  boolean collided() {
    return false;
  }

  int Lane() {
    int[] lanes = {-26, 22, 52, 92, 124, 172, 200, 236, 275, 325, 362, 400, 430, 470, 500, 540, 574, 616, 650, 690};
    ran =random(18);
    rani = round(ran);

    return lanes[rani];
  }

  void move() {
    y=y-3;
  }

  void collide()
  {
    if (y<0) {
      y = height + random (200);
      x=Lane();
    }
    if (collided()==true) {
      x=Lane();
    }
  }

  void update()
  {
    move();
    collide();
    render();
  }
}
  y = int(height + random (200));
1 Like

Hello,

If you hover over a red error or orange warning it will show the error in the bar below code (above console).

You can set preferences in the Processing PDE for errors and warnings and lots of other stuff:
file > preferences >

In this case:

This is a reference to help you resolve this:
https://processing.org/reference/intconvert_.html

:)

Thanks the code works now but the cars still doing the same thing only three rows of cars are appearing. I would like for the cars to appear on the screen until the game is stopped. I called the two other rows using an extended class but i don’t want to have to keep extending the class to get new rows of cars.

to get help, post an entire, runnable code

1 Like