The language used is called 'Processo' a weaker and simpler version of Processing used at my school to introduce Java syntax in a visual way. I'm still working on the code, but the overall purpose is similar to crossy road, but the cars are moving down the screen as the player avoid them. The player can only move horizontally, and the enemies only move vertically. Using if statements, once the enemy cars get below the screen that the user can see, the enemy car's y-variable is reset to above the screen. I want the speed of the cars to be the same, so to make sure they all aren't falling at the same time, the y-coordinate of the enemy cars are set to a random point above the screen every time the enemy car resets.
This is where the issue begins, if the y-coordinate is set to a set number, such as int i = 10; then the enemy car will reset like desired. But if the car's y-coordinate is set to random, like int i = random(-400, -100); then the car will not reset and keep moving down. How can I make the car reset or find a new solution that makes sure that the cars are moving at the same speed, but not at the same time?
size (300, 400);
double baddie1 = -100;
double baddie2 = -300;
double baddie3 = -600;
void player() {
noStroke();
fill(rgb(0, 255, 0));
rect(mouseX-50, 320, 80, 80);
}
void enemy1() {
fill(rgb(255, 0, 0));
rect(10, baddie1, 80, 80);
}
void enemy2() {
rect(110, baddie2, 80, 80);
}
void enemy3() {
rect(210, baddie3, 80, 80);
}
void draw() {
background(rgb(226, 225, 225));
if(baddie1 == 400) {
baddie1 = random(-600, -100);
} else {
baddie1 += 5;
}
if(baddie2 == 400) {
baddie2 = random(-600, -100);
} else {
baddie2 += 5;
}
if(baddie3 == 400) {
baddie3 = random(-600, -100);
} else {
baddie3 += 5;
}
player();
enemy1();
enemy2();
enemy3();
}
This is a Processing compatible version of the above:
double baddie1 = -100;
double baddie2 = -300;
double baddie3 = -600;
void setup(){
size (300, 400);
}
void player() {
noStroke();
fill(rgb(0, 255, 0));
rect(mouseX-50, 320, 80, 80);
}
void enemy1() {
fill(rgb(255, 0, 0));
rect(10, baddie1, 80, 80);
}
void enemy2() {
rect(110, baddie2, 80, 80);
}
void enemy3() {
rect(210, baddie3, 80, 80);
}
void draw() {
background(rgb(226, 225, 225));
if(baddie1 == 400) {
baddie1 = random(-600, -100);
} else {
baddie1 += 5;
}
if(baddie2 == 400) {
baddie2 = random(-600, -100);
} else {
baddie2 += 5;
}
if(baddie3 == 400) {
baddie3 = random(-600, -100);
} else {
baddie3 += 5;
}
player();
enemy1();
enemy2();
enemy3();
}
color rgb(int r, int g, int b){
return color(r, g, b);
}
void rect(double x, double y, double w, double h){
rect((float)x, (float)y, (float)w, (float)h);
}
The issue is far simpler than it seems: when you check for the baddies's y coordinate to know if they are far away enough to reset them, the random number issued won't always be a multiple of 5, and you check for (baddie1 == 400)
, so if this number increments from, let's say 397 to 402, you'll never see that one again.
You can fix this by using the >=
operator instead:
if (baddie1 >= 400) {
baddie1 = random(-600, -100);
} else {
baddie1 += 5;
}
if (baddie2 >= 400) {
baddie2 = random(-600, -100);
} else {
baddie2 += 5;
}
if (baddie3 >= 400) {
baddie3 = random(-600, -100);
} else {
baddie3 += 5;
}
Hope this helps. Have fun!