/* Interactive Toy Giuliano Patricelli Green_Ball_Game 10/3/2018 References used: Pyravliki - Drew Godden Target Practice - Jackson Lanaus *I used these program references for my score system and ball physics as well as end game screen* Enjoy my retro style game! */ ////////////////////////////////////////////////////// // \\ //Avoid the boncing green balls by moving the mouse \\ // \\ ////////////////////////////////////////////////////// // Set global variables int finalScore; float lines=40; // Set player size int playerHeight = 20; int playerWidth = 20; // Set ball size int ballHeight = 40; int ballWidth = 40; // Set the speeds of each ball float speedX = 2.8; float speedY = 5; float speedX2 = -2.8; float speedY2 = 6; float speedX3 = 2.8; float speedY3 = 7; float speedX4 = -2.8; float speedY4 = 8; float speedX5 = 2.8; float speedY5 = 9; float speedX6 = -2.8; float speedY6 = 9; // Spawn the balls at random locations within the top right corner of the screen float ballLocX = random (280, 399); float ballLocY = random (1, 100); float ballLocX2 = random (300, 399); float ballLocY2 = random (1, 100); float ballLocX3 = random (300, 399); float ballLocY3 = random (1, 100); float ballLocX4 = random (300, 399); float ballLocY4 = random (1, 100); float ballLocX5 = random (300, 399); float ballLocY5 = random (1, 100); float ballLocX6 = random (300, 399); float ballLocY6 = random (1, 100); // Create the booleans for the end game screen boolean isEndGame = false; boolean gameOn = true; // Set the variables for the score counter and the set it to count by milliseconds int score = 0; int millisOnGameStart = millis(); void setup() { // Set the size size(400, 400); // Removed the cursor for a more natural gameplay feel noCursor(); // Set the frame rate to 60fps for smooth gameplay frameRate(60); } void draw() { // Place the functions of the background and "endGame" outside of the "gameOn" loop to make game over screen possable drawBackdrop(); // If the game is on then contine to run all the game functions if (gameOn == true) { // Call all the core gameplay functions scoreCounter(); drawPlayer(); ball1(); hitDetection1(); ballBorders1(); ballBorders2(); ballBorders3(); ballBorders4(); ballBorders5(); ballBorders6(); spawnBalls(); } } // Cause the balls to bounce and reverse directions if any of them make contact with the walls void ballBorders1() { if ((ballLocX>=width)||(ballLocX <= 0)) { speedX = speedX * -1; } if ((ballLocY>=height)||(ballLocY <= 0)) { speedY = speedY * -1; } } void ballBorders2() { if ((ballLocX2>=width)||(ballLocX2 <= 0)) { speedX2 = speedX2 * -1; } if ((ballLocY2>=height)||(ballLocY2 <= 0)) { speedY2 = speedY2 * -1; } } void ballBorders3() { if ((ballLocX3>=width)||(ballLocX3 <= 0)) { speedX3 = speedX3 * -1; } if ((ballLocY3>=height)||(ballLocY3 <= 0)) { speedY3 = speedY3 * -1; } } void ballBorders4() { if ((ballLocX4>=width)||(ballLocX4 <= 0)) { speedX4 = speedX4 * -1; } if ((ballLocY4>=height)||(ballLocY4 <= 0)) { speedY4 = speedY4 * -1; } } void ballBorders5() { if ((ballLocX5>=width)||(ballLocX5 <= 0)) { speedX5 = speedX5 * -1; } if ((ballLocY5>=height)||(ballLocY5 <= 0)) { speedY5 = speedY5 * -1; } } void ballBorders6() { if ((ballLocX6>=width)||(ballLocX6 <= 0)) { speedX6 = speedX6 * -1; } if ((ballLocY6>=height)||(ballLocY6 <= 0)) { speedY6 = speedY6 * -1; } } void drawPlayer() { // Draw the player square noStroke(); fill(0, 255, 77); rect(mouseX, mouseY, playerHeight, playerWidth); } // Draw all the balls and make them move by the speed set in the global variables void ball1() { fill(0, 255, 77); ellipse(ballLocX, ballLocY, ballHeight, ballWidth); ballLocX = ballLocX + speedX; ballLocY = ballLocY + speedY; } void ball2() { fill(0, 255, 77); ellipse(ballLocX2, ballLocY2, ballHeight, ballWidth); ballLocX2 = ballLocX2 + speedX2; ballLocY2 = ballLocY2 + speedY2; } void ball3() { fill(0, 255, 77); ellipse(ballLocX3, ballLocY3, ballHeight, ballWidth); ballLocX3 = ballLocX3 + speedX3; ballLocY3 = ballLocY3 + speedY3; } void ball4() { fill(0, 255, 77); ellipse(ballLocX4, ballLocY4, ballHeight, ballWidth); ballLocX4 = ballLocX4 + speedX4; ballLocY4 = ballLocY4 + speedY4; } void ball5() { fill(0, 255, 77); ellipse(ballLocX5, ballLocY5, ballHeight, ballWidth); ballLocX5 = ballLocX5 + speedX5; ballLocY5 = ballLocY5 + speedY5; } void ball6() { fill(0, 255, 77); ellipse(ballLocX6, ballLocY6, ballHeight, ballWidth); ballLocX6 = ballLocX6 + speedX6; ballLocY6 = ballLocY6 + speedY6; } // Create the counter of score in the top left of the screen void scoreCounter() { fill(0, 255, 255, 180); textSize(55); score = (millis() - millisOnGameStart)/100; text(score, 20, 50); } void spawnBalls() { // Spawn the balls after the score has reached a set number if ( score > 50 ) { ball2(); hitDetection2(); if ( score > 100 ) { ball3(); hitDetection3(); } if ( score > 150 ) { ball4(); hitDetection4(); } if ( score > 200 ) { ball5(); hitDetection5(); } if ( score > 250 ) { ball6(); hitDetection6(); } } } // Draw the background void drawBackdrop() { background(0, 70, 14); for (int i=0; i<400; i+=50) { strokeWeight(10); stroke(0, 177, 0,30); line(0, lines+i, 400, lines+i); } noStroke(); endGame(); } // the function that is called to show the end game screen void endGameScreen() { fill(0, 255, 255, 180); textSize(100); text(finalScore, 100, 200); textSize(55); text("Score", 110, 280); } // When the player is hit by the ball the game sets "endGame" to true which causes the endgame screen to be called void endGame() { if (isEndGame == true) { endGameScreen(); } } // This block of code determins if the player square has contacted the any of the balls and if so it captures your final score and sets the game to end and switch to endgame screen void hitDetection1() { if (mouseX +10 > (ballLocX -20) && mouseY +10 < (ballLocY +20) && mouseX -10 < (ballLocX +20) && mouseY -10 > (ballLocY -20) && isEndGame == false) { finalScore = score; isEndGame = true; gameOn = false; } } void hitDetection2() { if (mouseX + 10 > (ballLocX2 -20) && mouseY +10 < (ballLocY2 +20) && mouseX -10 < (ballLocX2 +20) && mouseY -10 > (ballLocY2 -20) && isEndGame == false) { finalScore = score; isEndGame = true; gameOn = false; } } void hitDetection3() { if (mouseX +10 > (ballLocX3 -20) && mouseY +10 < (ballLocY3 +20) && mouseX -10 < (ballLocX3 +20) && mouseY -10 > (ballLocY3 -20) && isEndGame == false) { finalScore = score; isEndGame = true; gameOn = false; } } void hitDetection4() { if (mouseX +10 > (ballLocX4 -20) && mouseY +10 < (ballLocY4 +20) && mouseX -10 < (ballLocX4 +20) && mouseY -10 > (ballLocY4 -20) && isEndGame == false) { finalScore = score; isEndGame = true; gameOn = false; } } void hitDetection5() { if (mouseX +10 > (ballLocX5 -20) && mouseY +10 < (ballLocY5 +20) && mouseX -10 < (ballLocX5 +20) && mouseY -10 > (ballLocY5 -20) && isEndGame == false) { finalScore = score; isEndGame = true; gameOn = false; } } void hitDetection6() { if (mouseX +10 > (ballLocX6 -20) && mouseY +10 < (ballLocY6 +20) && mouseX -10 < (ballLocX6 +20) && mouseY -10 > (ballLocY6 -20) && isEndGame == false) { finalScore = score; isEndGame = true; gameOn = false; } }