/* Interactive Toy: Shape Shooter Student: Carsten Booth Instructor: Nicolas Hesler 2017/09/26 References used: Jason Pushkar's Interactive Toy from 2016, for the laser mechanics. Random Squares Code Orienteering file, for randomized square pattern. Learning Processing by Daniel Shiffman & In-class demonstration with Nicolas Helser were the reference for the button code. */ //if this boolean is true, the primary game functions will run, if it is false, they will stop and the menu button will show boolean buttonPressed = false; //this is used to track the player's score int score = 0; //these variables are used to control the randomized pattern before the button is pressed float squarePatternX; float squarePatternY; float spacing = 20; float squareSize = 20; float menuRed = 100; float menuBlue = 50; float menuBlack = 0; //these variables are used to control shooting the spaceships laser boolean shooting = false; int laserLength = 0; void setup() { size(400, 400); background(0); frameRate(60); noStroke(); println("Click the red button to start the game, mouse controls spaceship, click to shoot red squares before they read the bottom screen edge"); } //////////////////////////////////////////////start of draw void draw() { background(0); gameStartButton(); } //////////////////////////////////////////////end of draw ////////////////////////////////////////////// // GAME START BUTTON ////////////////////////////////////////////// //this button starts the game by calling all the game functions when pressed void gameStartButton() { //if the button is not pressed yet, show the button if (!buttonPressed) { //call square pattern function squarePattern(); //draw button rectMode(CENTER); fill(250, 50, 50, 150); rect(width/2, height/2, 40, 40); } //if the mouse enters the correct place on the screen and the user clicks, buttonPressed is true if (mouseX > width/2-20 && mouseX <= width/2+20 && mouseY > height/2-20 && mouseY <= height/2+20 && mousePressed) { buttonPressed=true; } //if the button is pressed, start primary game functions if (buttonPressed) { //game functions shipLaser(); drawStars(); drawSpaceFighter(); spawnEnemies(); gameReset(); cursorSettings(); laserCollision(); } } ////////////////////////////////////////////// // CURSOR SETTINGS ////////////////////////////////////////////// //this function hides the cursor when the game starts, and shows it when the game resets. //I referenced the processing.org reference page for this void cursorSettings() { if (buttonPressed) { noCursor(); } if (!buttonPressed) { cursor(ARROW); } } ////////////////////////////////////////////// // SPAWN ENEMIES ////////////////////////////////////////////// //variables for enemy spawn locations // x positions for square spawns float squareSpawnX1 = random(5, 395); // y positions for square spawns float squareSpawnY1 = random(-50, -20); //square left and right edges, which are defined based on position of red square X position float squareLeftEdge = squareSpawnX1-10; float squareRightEdge = squareSpawnX1+10; //square top and bottom edges, which are defined by position of red square Y position float squareTopEdge = squareSpawnY1-10; float squareBottomEdge = squareSpawnY1+10; //square speed float squareSpeed = 1; void spawnEnemies() { //draw an enemy square noStroke(); squareSpawnY1 = squareSpawnY1+squareSpeed; fill(250, 50, 50); rect(squareSpawnX1, squareSpawnY1, 20, 20); noStroke(); } ////////////////////////////////////////////// // COLLISIONS & SCORE COUNTING ////////////////////////////////////////////// //laser colliding with red square void laserCollision() { //if the end coordinates of the laser are equal to the bottom edge of the square, and between the left and right edges, reset the square and the laser positions if (mouseY>squareSpawnY1 && mouseY+laserLength <= squareSpawnY1+10 && mouseX>squareLeftEdge && mouseX<squareRightEdge) { //if the end of the laser has hit the square, stop shooting(thus resetting the laser length) shooting = false; //reset square position and edge values squareSpawnY1 = random(-50, -20); squareSpawnX1= random(5, 395); squareLeftEdge = squareSpawnX1-10; squareRightEdge = squareSpawnX1+10; squareSpeed = 1; //if the criteria for this function running are all met, add +1 to score counter, which prints in console score++; println("Score:"+(score)); } } ////////////////////////////////////////////// // GAME RESET ////////////////////////////////////////////// //if the red square collides with the button of the screen, stop primary game functions, and restart button and square pattern functions void gameReset() { if (squareSpawnY1>=height || mouseX-10>squareLeftEdge && mouseX-10<squareRightEdge && mouseY+10<squareBottomEdge && mouseY+10>squareTopEdge) { buttonPressed=false; //if game is reset, reset score and print Game Over! in console println("Game Over!"); score=0; //reset the enemy red square to above the screen so the game does not constantly reset squareSpawnY1 = -50; } } ////////////////////////////////////////////// // SQUARE PATTERN ////////////////////////////////////////////// //this function fills the screen with squares of randomized colours and cycles until the game start button is pressed, and when the game is reset. void squarePattern() { //size20 squares pattern loop squarePatternX=10; while (squarePatternX<width) { squarePatternY=10; while (squarePatternY<height) { fill(random(menuBlack, menuRed), 0, random(menuBlack, menuBlue)); rect(squarePatternX, squarePatternY, squareSize, squareSize); squarePatternY+=squareSize; } squarePatternX+=squareSize; } } ////////////////////////////////////////////// // SPACE FIGHTER ////////////////////////////////////////////// //draws player space fighter in blue void drawSpaceFighter() { fill(50, 150, 250); //the space fighter triangle coordinates will follow the mouse coordinates with slight offsets. triangle(mouseX-10, mouseY+10, mouseX, mouseY-20, mouseX+10, mouseY+10); } ////////////////////////////////////////////// // LASER ////////////////////////////////////////////// //player shooting laser functions void shipLaser() { //if mouse is clicked, then shoot laser if (mousePressed==true) { shooting=true; } //if shooting has been set to true, create a laser based on the code-block below if (shooting == true) { //draw laser line //make laser yellow stroke(250, 250, 0); //laser will flicker in size strokeWeight(random(1, 2)); line(mouseX, mouseY, mouseX, mouseY+laserLength); //increase laser length, changing the value of the number here will change laser speed or direction laserLength = laserLength -25; } //if laser goes off screen, set shooting to false if (laserLength <= -400) { shooting = false; } //if laser goes off screen, reset laser length if (shooting == false) { laserLength =0; } } ////////////////////////////////////////////// // STARS ////////////////////////////////////////////// //variables for drawStars function //generate random star location between 5, 395; this value is used for the stars X positions float starScatter1 = random(5, 395); float starScatter2 = random(5, 395); float starScatter3 = random(5, 395); float starScatter4 = random(5, 395); float starScatter5 = random(5, 395); float starScatter6 = random(5, 395); float starScatter7 = random(5, 395); float starScatter8 = random(5, 395); float starScatter9 = random(5, 395); //start star off-screen or at 0, this value is used for the stars Y positions float starFall1 = random(-50, 0); float starFall2 = random(-50, 0); float starFall3 = random(-50, 0); float starFall4 = random(-50, 0); float starFall5 = random(-50, 0); float starFall6 = random(-50, 0); float starFall7 = random(-50, 0); float starFall8 = random(-50, 0); float starFall9 = random(-50, 0); //star speeds will vary by adding a number between 1 and 5 to starFall at each reset of star float starSpeed1 = random(1, 5); float starSpeed2 = random(1, 5); float starSpeed3 = random(1, 5); float starSpeed4 = random(1, 5); float starSpeed5 = random(1, 5); float starSpeed6 = random(1, 5); float starSpeed7 = random(1, 5); float starSpeed8 = random(1, 5); float starSpeed9 = random(1, 5); //draw stars at top of screen in random X location and reset when they reach maximum height(bottom of screen) void drawStars() { //draw a star1 strokeWeight(1); stroke(255); starFall1 = starFall1+starSpeed1; point(starScatter1, starFall1); noStroke(); if (starFall1>=height) { starFall1 = random(-50, 0); starScatter1= random(5, 395); starSpeed1 = random(1, 5); } //draw a star2 stroke(255); strokeWeight(1); starFall2 = starFall2+starSpeed2; point(starScatter2, starFall2); noStroke(); if (starFall2>=height) { starFall2 = random(-50, 0); starScatter2= random(5, 395); starSpeed2 = random(1, 5); } //draw a star3 stroke(255); strokeWeight(1); starFall3 = starFall3+starSpeed3; point(starScatter3, starFall3); noStroke(); if (starFall3>=height) { starFall3 = random(-50, 0); starScatter3= random(5, 395); starSpeed3 = random(1, 5); } //draw a star4 stroke(255); strokeWeight(1); starFall4 = starFall4+starSpeed4; point(starScatter4, starFall4); noStroke(); if (starFall4>=height) { starFall4 = random(-50, 0); starScatter4= random(5, 395); starSpeed4 = random(1, 5); } //draw a star5 stroke(255); strokeWeight(1); starFall5 = starFall5+starSpeed5; point(starScatter5, starFall5); noStroke(); if (starFall5>=height) { starFall5 = random(-50, 0); starScatter5= random(5, 395); starSpeed5 = random(1, 5); } //draw a star6 stroke(255); strokeWeight(1); starFall6 = starFall6+starSpeed6; point(starScatter6, starFall6); noStroke(); if (starFall6>=height) { starFall6 = random(-50, 0); starScatter6= random(5, 395); starSpeed6 = random(1, 5); } //draw a star7 stroke(255); strokeWeight(1); starFall7 = starFall7+starSpeed7; point(starScatter7, starFall7); noStroke(); if (starFall7>=height) { starFall7 = random(-50, 0); starScatter7= random(5, 395); starSpeed7 = random(1, 5); } //draw a star8 stroke(255); //this star will flicker strokeWeight(random(1, 2)); starFall8 = starFall8+starSpeed8; point(starScatter8, starFall8); noStroke(); if (starFall8>=height) { starFall8 = random(-50, 0); starScatter8= random(5, 395); starSpeed8 = random(1, 5); } //draw a star9 stroke(255); //this star will flicker strokeWeight(random(1, 2)); starFall9 = starFall9+starSpeed9; point(starScatter9, starFall9); noStroke(); if (starFall9>=height) { starFall9 = random(-50, 0); starScatter9= random(5, 395); starSpeed9 = random(1, 5); } }