Random position of class objects

Hi, I managed to make my player ( the car ) collide with the obstacles that are falling down. I tried to make a class so I would be able to make the position, speed, and size of each obstacle random. But it doesn’t work and I don’t see why.

here is my code for the collision:
if (dist(obstacleXPos, obstacleYPos, mouseX, y) < 60) {
background (0);
noLoop();
textSize(80);
fill(#3279D8);
text(“You lose!”, 180, height/2);
}

This is the class I made:
class ObstacleMovement {

int x;
int y;
int size;
int obstacleXPos;

int speed;
ObstacleMovement( int newX, int newY, int newSize, int newobstacleXPos, int newobstacleY, int newSpeed ) {
x = newX;
y = newY;
size = newSize;
obstacleXPos = newobstacleXPos;
speed = newSpeed;
}

void move() {
obstacleYPos = obstacleYPos + speed;
if (obstacleYPos > height) {
obstacleYPos = -20;
}
}

void display() {

fill (random(255), random(255), 255);
rect(obstacleXPos, obstacleYPos, 65, 80); // obstacle rectangle
//fill(255);
//ellipse(obstacleXPos+400, obstacleYPos, 72, 72); // Obstacle circle

}

void run() {
move();
display();
}

}

and this is how I create a new obstacle with the class:
ObstacleMovement obstacle1 = new ObstacleMovement(100, 200, 80, 100, 10, 3);

If someone knows how to help me I would be very grateful

here you don’t use the object obstacle1 that you derived from the class

in the class: what is the difference of x,y and obstacleXPos,obstacleYPos

Please show your entire, run-able code.

Chrisir

ObstacleMovement obstacle1 = new ObstacleMovement(100, 200, 80, 100, 10, 3);
ObstacleMovement obstacle2 = new ObstacleMovement(100, 200, 30, 350, 10, 5);
ObstacleMovement obstacle3 = new ObstacleMovement(100, 200, 30, 600, 10, 5);
Car carmain = new Car();
//SlowObjects slowobject1 = new SlowObjects(100, 300, 30, 240, 10, 9);
StripeMovement stripe1 = new StripeMovement(1, 1, 1, 1);

float stripeYPos;
float b;
float obstacleYPos;
float obstacleXPos;
float obstaclePos;
float obstacle1Pos;

float obstacleXxPos;
float obstacleYyPos;

float size;
float speed;

int x = 0;
int y = 450;

void setup() {
size (720, 600);
obstacleYPos = -80;

obstacleXPos = 115;
obstaclePos = 350;
obstacle1Pos = 600;
obstacleXxPos = 240;
obstacleYyPos = -80;
}

void keyPressed() {
if ( key == ‘d’ ) {
x = x + 50;
}
if ( key == ‘a’)
x = x - 50;
}

void draw () {
background (#484646);
smooth();
strokeWeight(2);

line(240, 0, 240, 600);
line(480, 0, 480, 600);

stripe1.run();
obstacle1.run();
obstacle2.run();
obstacle3.run();
carmain.run();
//slowobject1.run1();

if (dist(obstacleXPos, obstacleYPos, mouseX, y) < 60) {
background (0);
noLoop();
textSize(80);
fill(#3279D8);
text(“You lose!”, 180, height/2);
}

if (dist(obstaclePos, obstacleYPos, mouseX, y) < 60) {
background (0);
noLoop();
textSize(80);
fill(#3279D8);
text(“You lose!”, 180, height/2);
}
if (dist(obstacle1Pos, obstacleYPos, mouseX, y) < 60) {
background (0);
noLoop();
textSize(80);
fill(#3279D8);
text(“You lose!”, 180, height/2);
}

}`

1 Like

CODE not running, no car class, no stripe class…


Remarks

this is before setup() - seems unnecessary since you have a class now!!

this is in setup() - same thing!!!


**Remark II **

if (dist(obstacleXPos, obstacleYPos, mouseX, y) < 60) {

this is in draw, it must refer to the class like below

Please do the tutorial for classes: https://www.processing.org/tutorials/objects/

Chrisir


ObstacleMovement obstacle1 = new ObstacleMovement(100, 200, 10, 3);
ObstacleMovement obstacle2 = new ObstacleMovement(350, 200, 10, 5);
ObstacleMovement obstacle3 = new ObstacleMovement(600, 200, 10, 5);

// float b;

int x = 0;
int y = 450;

void setup() {
  size (720, 600);
}

void keyPressed() {
  if ( key == 'd' ) {
    x = x + 50;
  } else if ( key == 'a')
    x = x - 50;
}

void draw () {
  background (#484646);

  // CAR
  fill(255, 0, 0); 
  rect (mouseX, y, 15, 15); 

  smooth();
  strokeWeight(2);

  line(240, 0, 240, 600);
  line(480, 0, 480, 600);

  obstacle1.run();
  obstacle2.run();
  obstacle3.run();

  if (dist(obstacle1.x, obstacle1.y, mouseX, y) < 60) {
    background (0);
    noLoop();
    textSize(80);
    fill(#3279D8);
    text("You lose!", 180, height/2);
  }

  if (dist(obstacle2.x, obstacle2.y, mouseX, y) < 60) {
    background (0);
    noLoop();
    textSize(80);
    fill(#3279D8);
    text("You lose!", 180, height/2);
  }
  if (dist(obstacle3.x, obstacle3.y, mouseX, y) < 60) {
    background (0);
    noLoop();
    textSize(80);
    fill(#3279D8);
    text("You lose!", 180, height/2);
  }
}
// ===========================================================================

class ObstacleMovement {

  int x;
  int y;
  int size;
  color col1 = color ( random(255), random(255), 255);
  float type=random(1); 

  int speed;
  ObstacleMovement( int newX, int newY, 
    int newSize, 
    int newSpeed ) {
    x = newX;
    y = newY;
    size = newSize;
    speed = newSpeed;
  }

  void move() {
    y = y + speed;
    if (y > height) {
      y = -20;
    }
  }

  void display() {
    if (type>0.5) {
      fill (col1);
      rect(x, y, 
        65, 80); // obstacle rectangle
    } else {
      fill(0, 255, 0);
      ellipse(x, y, 72, 72); // Obstacle circle
    }
  }

  void run() {
    move();
    display();
  }
}//class
//