Random position of class objects

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: Objects / Processing.org

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
//