I’m attempting to make a game where boxes fly in from the left of the screen and the player must move the cursor to dodge them. However, when I add the delay where I believe it should go, the game stops working.
Here is my code:
import java.util.*;
import processing.serial.*;
float x, y1;
float[] y = new float[30];
Random ran = new Random();
void setup() {
frameRate(60);
size(600, 600, P2D);
strokeWeight(4);
noCursor();
x = -3;
y1 = ran.nextFloat();
int i;
for (i = 0; i < 30; i = i + 1){
y[i] = ran.nextFloat()*600;
println(y[i]);
}
}
void draw() {
background(204);
line(mouseX, mouseY, pmouseX, pmouseY);
for (int i = 0; i < 30; i++){
rect(x, y[i], 15, 15);
delay(2000);
}
translate(x, y1);
x += 10;
}
If I add the delay outside of the for loop, it just generates all 15 boxes and then makes them move 10 pixels every 2 seconds.
I’m not sure if this solves my issue. I want make a box, delay, make another, delay, etc… I believe your solution would just delay their group movement, correct?
This is what I’ve changed my code to. I have one moving box and a score counter. I just can’t get any other boxes to generate like I want.
So far I have:
1 Moving Box
A Score Counter
A code that closes that game if my cursor hits the box
Here it is:
import java.util.*;
import processing.serial.*;
float x, y1;
float[] y = new float[30];
float[] upperBound = new float[30];
Random ran = new Random();
int pOrM, score = 0;
void setup() {
frameRate(30);
size(600, 600, P2D);
strokeWeight(4);
noCursor();
x = -3;
y1 = ran.nextFloat();
int i;
for (i = 0; i < 30; i = i + 1){
y[i] = ran.nextFloat()*570;
println(y[i]);
}
for (i = 0; i < 30; i++){
upperBound[i] = 1;
}
}
void draw() {
background(204);
fill(0);
text(score, 15, 30);
textSize(26);
line(mouseX, mouseY, pmouseX, pmouseY);
for (int i = 0, j = 0; i < upperBound[j] && j < 29; i++, j++){
rect(x, y[j], 30, 30);
if (x < mouseX && x + 30 > mouseX && y[j] < mouseY && y[j] + 30 > mouseY){
System.exit(0);
}
frameRate++;
if (x + 15 > 601){
if (x < 597){
score++;
}
}
}
translate(x, y1);
x += 10;
}
I just can’t get the next boxes to load. It seems as though my for loop only ever runs once.
Okay, I see what you are saying there. However, the code you have provided creates a box for a few seconds and then gives me an array out of bounds issue.
import java.util.*;
import processing.serial.*;
float x, y1;
float[] y = new float[30];
//float[] upperBound = new float[30];
float upperBound = 1;
Random ran = new Random();
int pOrM, score = 0;
void setup() {
frameRate(30);
size(600, 600, P2D);
strokeWeight(4);
noCursor();
x = -3;
y1 = ran.nextFloat();
int i;
for (i = 0; i < 30; i = i + 1){
y[i] = ran.nextFloat()*570;
println(y[i]);
}
/*for (i = 0; i < 30; i++){
upperBound[i] = 1;
}*/
}
void draw() {
background(204);
fill(0);
text(score, 15, 30);
textSize(26);
line(mouseX, mouseY, pmouseX, pmouseY);
for (int i = 0, j = 0; i < upperBound && j < 30; i++, j++){
rect(x, y[i], 30, 30);
if (x < mouseX && x + 30 > mouseX && y[i] < mouseY && y[i] + 30 > mouseY){
System.exit(0);
}
frameRate++;
if (x + 15 > 601){
if (x < 597){
score++;
x = -5;
}
}
if (frameCount %60 == 0){
upperBound++;
}
}
translate(x, y1);
x += 10;
}
This works perfectly fine for the first box. However, as soon as upperBound bumps up 1 within my if statement, all of the boxes appear rather than 1 more. How should I approach this?