1

How do I create a random car barrier for my game? I have this top-down car game that I need to make. This game aims to make the car move up and down (on the y-axis) to avoid the barriers, while the barriers are moving toward the car (on the x-axis). Is it possible to create a random generation of the barriers when they spawn, making the game enjoyable? Right now, I only have a set of barriers that last for 12 seconds before I get easily through them. Can I also keep the spacing of the barriers the same? So the car can fit.

code

color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);

float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 700;



float lanes;

void lane1(float x, float y){
  fill(white);
  rect(x + 100, y,30,100);
  rect(x + 250, y + 100,30,100);
  rect(x + 250, y + 200, 30, 100);
  rect(x + 400, y + 100, 30, 100);
  rect(x + 550, y + 100, 30, 100);
  rect(x + 550, y, 30, 100);
  rect(x + 700, y + 100, 30, 100);
  rect(x + 700, y + 200, 30, 100);
}

void background(){
  background(green);
}
  
void car(){
  fill(red);
  rect(carX, carY, 60, 40);
}
void setup(){
  surface.setTitle("dodge");
  size(900, 500);
}

void draw(){
  background();
  noStroke();
  fill(grey);
  rect(roadx - 400, road1y - 30, 1500, 100);
  stroke(yellow);
  strokeWeight(5);
  rect(roadx - 400, road2y - 30, 1500, 100);
  noStroke();
  rect(roadx - 400, road3y - 27, 1500, 100);
  car();  
  lane1(laneX, 100);
  laneX -= 1;
  
  lanes = 1;

}
    

void keyPressed(KeyEvent event){
  if (key == 'w'){
    if(carY != 130){
      carY = carY - 100;
    }
  }
  if (key == 's'){
    if(carY != 330){
      carY = carY + 100;
    }
  }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Haadi Khan
  • 21
  • 1
  • That is of course possible. You need to create / generate barriers dynamically. Create a structure to save your barriers (e.g. an ArrayList or a simple array) that fits a screen filling of barriers (6, with your current lane length of 900 and barrier distance of 150). Then, every 150 'ticks' (pixels moved) remove the barriers that just left the screen and generate new barriers to flow in from the right. I'll try to modify your code for a simple implementation of that. – Eskapone May 11 '22 at 09:24

1 Answers1

1

I've modified your lane rendering and added dynamic creation of barriers. Every 150 pixels moved, the barriers are updated (leftmost barriers are removed, new ones are added at the rightmost position and the cycle starts over).

The barriers are stored as an integer between 0 and 6, as there are 7 possibilities for the barrier positions:

int   | 0  1  2  3  4  5  6
------+--------------------
lane0 |    #           #  #
lane1 |       #     #  #
lane2 |          #  #     #

Hope this helps.

Modified code:

import java.util.*;

private static final int SCREEN_X = 900;
private static final int SCREEN_Y = 500;
private static final int LANE_HEIGHT = 100;

private static final int BARRIER_DISTANCE = 150;
private static final int BARRIER_HEIGHT = 100;
private static final int BARRIER_WIDTH = 30;


color green = color(0,195,0);
color red = color(195,0,0);
color grey = color(100,100,100);
color yellow = color(200,200,0);
color white = color(255,255,255);

float roadx = 70, road1y = 130;
float road2y = 230, road3y = 330;
float carY = road2y;
float carX = roadx;
float carUPspeed = 1;
float laneX = 50;

float lanes;

List<Integer> barriers;

void lane1(float x){
  fill(white);
  for (Integer b : barriers)
  {
    switch(b)
    {
      case 0: // ___
        break;
      case 1: // X__
        rect(x, LANE_HEIGHT, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 2: // _X_
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 3: // __X
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 4: // _XX
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 5: // XX_
        rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 2, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
      case 6: // X_X
        rect(x, LANE_HEIGHT * 1, BARRIER_WIDTH, BARRIER_HEIGHT);
        rect(x, LANE_HEIGHT * 3, BARRIER_WIDTH, BARRIER_HEIGHT);
        break;
    }
    x += BARRIER_DISTANCE;
  }
}

void background(){
  background(green);
}
  
void car(){
  fill(red);
  rect(carX, carY, 60, 40);
}
void setup(){
  surface.setTitle("dodge");
  size(900, 500);
  
  // Initially generate 7 barriers (one more than the screen can fit)
  barriers = new ArrayList<Integer>();
  
  for (int i = 0; i <= (SCREEN_X / BARRIER_DISTANCE); i++)
  {
    barriers.add(int(random(0, 6)));
  }
  
  // Set the first to barriers to 'no barrier' to the player initially has some time
  barriers.set(0, 0);
  barriers.set(1, 0);
}

void draw(){
  background();
  noStroke();
  fill(grey);
  rect(roadx - 400, road1y - 30, 1500, LANE_HEIGHT);
  stroke(yellow);
  strokeWeight(5);
  rect(roadx - 400, road2y - 30, 1500, LANE_HEIGHT);
  noStroke();
  rect(roadx - 400, road3y - 27, 1500, LANE_HEIGHT);
  car();
  lane1(laneX);
  laneX -= 1;
  
  // laneX cycles from 50 to -100 (because of draw position)
  // So, every 150 pixels moved
  if (laneX == -100)
  {
    // Reset laneX
    laneX = 50;
    // Remove leftmost barrier
    barriers.remove(0);
    // Add new barriers, incoming from the right
    barriers.add(int(random(0, 6)));
  }
  
  lanes = 1;
}
    

void keyPressed(KeyEvent event){
  if (key == 'w'){
    if(carY != 130){
      carY = carY - 100;
    }
  }
  if (key == 's'){
    if(carY != 330){
      carY = carY + 100;
    }
  }
}
Eskapone
  • 321
  • 1
  • 7