xxxxxxxxxx
float xPos, yPos; //float is a decimal point number
float xSpeed, ySpeed;
int lives, score; //whole numbers
color ballColor; //AMERICA
void setup(){
size(500, 500);
background(150, 0, 150);
xSpeed = 6;
ySpeed = 3;
xPos = 100;
yPos = 250;
lives = 3;
ballColor = randomColor();
}
void draw(){
fill(150, 0, 150, 20); //r, g, b, alpha
rect(0, 0, width, height); //are screen width, height
//Paddle
fill(ballColor);
rect(width - 30, mouseY - 50, 30, 100, 10); //5th number rounds the corners
//ball
xPos += xSpeed; //xPos = xPos + xSpeed;
yPos += ySpeed;
noStroke();
fill(ballColor);
ellipse(xPos, yPos, 30, 30);
// UI, UX, UIX: user interface
text("score: " + score, 10, 20);
text("Lives: " + lives, 10, 40);
//hitbox : checking if paddle is hit
if(xPos > width - (15 + 30) && yPos < mouseY + 15 + 50 && yPos > mouseY - 15 - 50){
xSpeed = -xSpeed;
score = score + 1; //score++; score += 1;
}
// || mean or in computer, && means and
if(xPos < 0 + 15){
xSpeed = -xSpeed; // xSpeed *= -1, xSpeed = xSpeed * -1;
}
if(yPos > 500 - 15 || yPos < 0 + 15){
ySpeed = -ySpeed; // xSpeed *= -1, xSpeed = xSpeed * -1;
}
if(xPos > 500 - 15){
reset();
}
}
void reset(){
if(lives > 1){
lives = lives - 1; // lives -= 1; lives--;
xPos = 100;
yPos = 250;
}
else{
lives--;
exit();
}
}
color randomColor(){
color newCol = color(random(100, 255), random(100, 255), random(100, 255));
return newCol;
}