I Need Help! (Exam)

I’m making a simplified game, were the cirkel has to collect trash. (It’s simplified a lot) But there is a challenge and that is that the cirkel cannot be hit by the cars.

BUT here’s my problem.
I can’t spawn infinite cars in because i need a delay between. (3-5 seconds)

Can anyone help me before monday PLEASE!

I have Discord if you want to talk or text with me about it. ~VOA_Rick~#5436

All my work so far down below:

//Car Variables
int s, t;
int s1, t1;

boolean CarDelay;
long reloadStartTime;
int wait = 5000;

float [] i = {205, 355};
//Trash Variables

//Random Variables
float o, p;
int c;
float x1, y1;
static int x, y;
float xDelta, yDelta;
static int w, h;
static int ww, hh;   
static int gw, gh;
boolean bag;

final static byte  FPS = 90;

//Setup
void setup(){
  size(600, 900);
  smooth();
  frameRate(FPS);

  c = #C8C8C8;
  
  o = 525;
  p = 825;
  //---------
  s = 205;
  t = 0;
  s1 = 10;
  t1 = 10;
  
}

//Width/Height
static boolean north, south, west, east;

static final void initVars(int wdt, int hgt) {
  x = wdt>>1;     
  y = hgt>>1;
 
  w = wdt/15;    
  h = hgt/15;
 
  ww = w>>1;     
  hh = h>>1;
 
  gw = wdt - ww; 
  gh = hgt - hh;
}


void draw(){  
//Road Structures
  background(51);
  rect(-10, -10, 150, 910);
  fill(c);
  rect(450, -10, 150, 910);
  
  line(70, 0, 70, 900);
  line(525, 0, 525, 900);
  
  stroke(153);
  strokeWeight(5);
  line(300, 0, 300, 100);
  line(300, 200, 300, 300);
  line(300, 400, 300, 500);
  line(300, 600, 300, 700);
  line(300, 800, 300, 900);
  
//Bag
  fill(0);
  ellipse(o, p, 70, 40);
  fill(c);

//----------------------------
//Cars
  //s = s + s1;
  t = t + t1;
  
  rect(s, t, 40, 80);
  
  rect(s, t, 40, 80);
//----------------------------
  //Barriers
  if(o > width - 20) {
     o = 575;
  }

  if(o < 0 + 20) {
     o = 25;
  }

  if(p > height - 20) {
     p = 25;
  }
 
  if(p < 0 + 20) {
     p = 875;
  }
}


void keyPressed(){
  final int k = keyCode;
  
  if (k == ' ' | k == ENTER | k == RETURN)
  bag = !bag;
    
  if(key == 'a')
    o = o - 25;
    
  if(key == 'd')
    o = o + 25;
}

void displayObject() {
  if (bag)    ellipse(x, y, w, h);
  else              rect(x, y, w, h);
}

Hello,

you posted your code incorrectly

the naming of variables is bad (t,t1,t2,s…) - please use something like

  • car1_X, car1_Y, car1_color, car1_Speed and
  • car2_X, car2_Y, car2_color, car2_Speed

You might want to look into OOP :
https://www.processing.org/tutorials/objects/

I used a var called timer now to wait until we start the 1st car on the right lane.

When I respawn, I give them a RANDOM y position below 0 which means they take different amount of time until they reach the screen and become visible.

Chrisir

//Car Variables
int s, s2, t, t2=-83, t3=5;
int s1, t1;
color col1, col2; 

boolean CarDelay;
long reloadStartTime;
int wait = 5000;

float [] i = {205, 355};
//Trash Variables

//Random Variables
float o, p;
int c;
float x1, y1;
static int x, y;
float xDelta, yDelta;
static int w, h;
static int ww, hh;
static int gw, gh;
boolean bag;

final static byte FPS = 90;

int timer; 

//Setup
void setup() {
  size(600, 900);
  smooth();
  frameRate(FPS);

  c = #C8C8C8;

  o = 525;
  p = 825;
  //---------
  s = 205;
  s2 = 352;

  t = 0;
  s1 = 0;
  t1 = 10;

  col1=color(random(205), random(205), random(205));
  col2=color(random(111, 255), random(140, 255), random(140, 255));

  timer=millis();
}

//Width/Height
static boolean north, south, west, east;

static final void initVars(int wdt, int hgt) {
  x = wdt>>1;
  y = hgt>>1;

  w = wdt/15;
  h = hgt/15;

  ww = w>>1;
  hh = h>>1;

  gw = wdt - ww;
  gh = hgt - hh;
}

void draw() {

  background(51);

  //Road Structures
  fill(0);
  rect(-10, -10, 150, 910);
  fill(0);
  rect(450, -10, 150, 910);

  line(70, 0, 70, 900);
  line(525, 0, 525, 900);

  stroke(153);
  strokeWeight(5);
  line(300, 0, 300, 100);
  line(300, 200, 300, 300);
  line(300, 400, 300, 500);
  line(300, 600, 300, 700);
  line(300, 800, 300, 900);

  //Bag
  fill(0);
  ellipse(o, p, 70, 40);
  fill(0);

  //----------------------------
  //Cars
  if (millis()-timer>3600) {
    t2 = t2 + t3;
  }

  //s = s + s1;
  t = t + t1;

  // respawn
  if (t>height+83) { 
    t=int(random(-184, -23)); // pos y
    t1=int(random(0.9, 5.6)); // speed
    col1=color(random(205), random(205), random(205));
  }

  // respawn
  if (t2>height+83) { 
    t2=int(random(-184, -23)); // pos y
    t3=int(random(0.9, 5.6)); // speed
    col2=color(random(111, 255), random(140, 255), random(140, 255));
  }

  fill(col1);
  rect(s, t, 40, 80);
  fill(col2);
  rect(s2, t2, 40, 80);

  //----------------------------
  //Barriers
  if (o > width - 20) {
    o = 575;
  }

  if (o < 0 + 20) {
    o = 25;
  }

  if (p > height - 20) {
    p = 25;
  }

  if (p < 0 + 20) {
    p = 875;
  }
}

void keyPressed() {
  final int k = keyCode;

  if (k == ' ' | k == ENTER | k == RETURN)
    bag = !bag;

  if (key == 'a')
    o = o - 25;

  if (key == 'd')
    o = o + 25;
}

void displayObject() {
  if (bag)
    ellipse(x, y, w, h);
  else
    rect(x, y, w, h);
}
//
1 Like

Thx a lot man!
But would you mind helping me with a reset system?
I need the object to die if it hits the cars…

Thx though.

sure

here is a collision tutorial

on collision reset the car (random y value as I have shown) and kill the object

1 Like

Thx. I now have until Wednesday.
Would you mind creating that system for me?
Maybe in another document, or in the same document as before.
It would help me a lot, because i also need to add something else to the game.

Btw.
I did read all of the text you sent me but it didn’t made me mush smarter…

??

No, I am not writing this for you. That’s not how the forum works

Anyway instead of using this link with collision use

(for object1.x, object1.y, object2.x, object2.y insert your values for car position (t2 etc.) and cirkel)

if(dist(object1.x, object1.y, object2.x, object2.y) < 40) {
     // collision
    // respawn
    t2=int(random(-184, -23)); // pos y
    t3=int(random(0.9, 5.6)); // speed
    col2=color(random(111, 255), random(140, 255), random(140, 255));
}

Cheers

Chrisir