//Ball variables
float xPosBall = random (0);
float yPosBall = random (0, 500);
float xSizeBall = 150;
float ySizeBall = 150;
float xSpeed = random (3, 5);
float ySpeed = random (3, 5);
int colour = 255;
//Character Variables, Char for short
float xPosChar = 400;
float yPosChar = 400;
float charHeight = 100;
float charWidth = 100;
float charSpeed;
void setup () {
size (500, 500);
smooth ();
rectMode(CENTER);
ellipseMode(CENTER);
xSizeBall = xSizeBall/2;
ySizeBall = ySizeBall/2;
charWidth = charWidth/2;
charHeight = charHeight/2;
}
void draw() {
background (0);
drawBall();
bounceBall();
moveBall();
killBall();
drawCharacter();
collideCharacter();
}
void drawBall() {
//BALL STUFF
noStroke();
fill (colour);
ellipse (xPosBall, yPosBall, xSizeBall, ySizeBall);
}
void moveBall() {
//making the ball move in the x axis
xPosBall = xPosBall + xSpeed;
//making the ball move in the y axis
yPosBall = yPosBall + ySpeed;
}
void killBall() {
//red laser
fill(242, 29, 29);
rect(490, 250, 10, 500);
//ball heading to the right wall, LIVES SYSTEM OF THE TOY
if (xPosBall > width) {
xPosBall = 0;
colour = colour -100; //decreasing the colour
xSizeBall = xSizeBall - 25; //decreasing the size
ySizeBall = ySizeBall - 25; //decreasing the size
}
if (colour < 0) { //keeping the colour black after certain red lines
colour = 0;
}
if (xSizeBall < 0) { //keeping the size 0
xSizeBall = 0;
}
if (ySizeBall < 0) { //keeping the size 0
ySizeBall = 0;
}
}
void bounceBall() {
//ball bouncing off the left side
if (xPosBall < 0) {
xSpeed = -xSpeed;
}
//making the ball bounce from the top
if (yPosBall < 0) {
ySpeed = -ySpeed;
}
//making the ball bounce from the bottom
if (yPosBall > height) {
ySpeed = -ySpeed;
}
}
void drawCharacter() {
//Character body
fill(150);
rect(xPosChar, yPosChar, charWidth, charHeight);
//Character face
fill(0);
ellipse(xPosChar - 10, yPosChar - 10, 10, 10);
ellipse(xPosChar + 7, yPosChar -10, 10, 10);
ellipse(xPosChar + 0, yPosChar + 10, 20, 15);
//body waist
fill(150);
rect(xPosChar, yPosChar +30, charWidth - 10, charHeight - 20);
//left feet
fill(150);
rect(xPosChar - 10, yPosChar +50, charWidth - 40, charHeight - 30);
//right foot
fill(150);
rect(xPosChar - 10, yPosChar +50, charWidth - 40, charHeight - 30);
//left foot
fill(150);
rect(xPosChar +10, yPosChar +50, charWidth - 40, charHeight - 30);
//right arm
fill(150);
rect(xPosChar - 20, yPosChar +30, charWidth - 25, charHeight - 40);
//left arm
fill(150);
rect(xPosChar + 20, yPosChar +30, charWidth - 25, charHeight - 40);
}
//moving the character
void keyPressed() {
//moving up
if (keyCode==UP) {
yPosChar = yPosChar - charHeight;
}
//moving down
else if (keyCode==DOWN) {
yPosChar = yPosChar + charHeight;
}
}
void collideCharacter() {
// Collison with the character
if (yPosBall > yPosChar - 50 && yPosBall < yPosChar + 50 && xPosBall >= xPosChar - 50) { //collision sides of the character
xSpeed = -xSpeed; //reversing direction
ySpeed = -ySpeed;
}
else if (yPosBall < yPosChar + 50 && yPosBall > yPosChar - 50 && xPosBall <= xPosChar + 50) {
xSpeed = +xSpeed;
ySpeed = +ySpeed;
}
}