xxxxxxxxxx
float hue;
float xPos, xSpeed;
PVector bPos; //(x, y, z)
int score = 0; // Initialize score to 0
void setup(){
size(800, 500);
background(0);
colorMode(HSB, 100); //HUE, SAT, BRIGHT value/100
xSpeed = 5;
xPos = 0;
bPos = randomPos(); // Initialize bPos
rectMode(CENTER);
}
void draw(){
xPos += xSpeed; // Corrected the operator from +== to +=
hue += 0.1;
stroke(hue % 100, 100, 100);
strokeWeight(5);
fill(0);
rect(width/2, height/2, width, height);
fill(40);
text(nf(30 - millis()/1000, 0, 2), width/2, height/2);
line(xPos, 0, xPos, height);
fill(0);
rect(bPos.x, bPos.y, 50, 50);
fill(hue % 100, 100, 100);
textSize(50);
textAlign(CENTER, CENTER);
text(score, bPos.x, bPos.y); // Display score
if (3000 - millis() <=0){
exit();
}
if (xPos > width || xPos < 0){
xSpeed = -xSpeed; //xSpeed = xSpeed * -1; xSpeed *= 1;
}
}
void keyPressed(){
if(key == ' '){
xSpeed *= -1;
if(abs(bPos.x - xPos) > 25) {
score++;
xSpeed *= 1.1; // Increase speed by 10%
bPos = randomPos();
}
}
}
PVector randomPos(){
return new PVector(random(100, width-100), random (100, height-100), 0);
}