/* PROGRAM NAME: shooter_without_deadorange PROGRAM DESC: Move (WASD) and shoot (Mouse Click) to destroy cubes before time runs out and survive longer, indicated at the bottom. The longer you survive, the difficulty increases, indicated by cubes in the top left. Shots go slower, enemies smaller and more camoflauged, and time runs out quicker. If you die, press any key to restart the game. AUTHOR: Maxwell Hayes Woodley CREATED FOR: Assignment 2 - Interactive Toy [Intro to Media Computation] Teacher - Nicholas Hesler CHANGELOG: Not Available */ ////GAME VARIABLES//// float screen; // What screen the game is on [0 = Death Screen, 1 = Game] float currentTime; // The current time (in milliseconds) since the game started // Variables for background float backR; // R channel float backG; // G channel float backB; // B channel float backInc; // How much the G channel will increase with difficulty // Variables for difficulty management float maxTime; // The maximum amount of time the player can have float timeLeft; // How much time the player has left until they die float levelIncTime; // When to increase the difficulty float levelTimeAdd; // How much time to add between difficulty increases float level; // What difficulty level the player has reached float maxLevel; // The maximum difficulty level the player can reach float diffBoxX, diffBoxY; // X/Y Coordinates for difficulty box float diffBoxHeight, diffBoxWidth; // Height/Width of difficulty box ////PLAYER VARIABLES//// float playerSpeed; // How fast the player travels float playerSpeedInc; // How much faster the player travels with difficulty increase // Whether the player can travel in each direction boolean goUp; boolean goDown; boolean goRight; boolean goLeft; // Variables for head float playerX, playerY; // X/Y Coordinates for head float playerWidth, playerHeight; // Height/Width of head float headR; // R channel float headG; // G channel float headB; // B channel float headWeight; // The weight of the head's stroke // Variables for eyes float eyeLineSize; // The size of the eye lines float eyeOneX, eyeOneY; // X/Y Coordinates for eye 1 float eyeTwoX, eyeTwoY; // X/Y Coordinates for eye 2 float distOneX, distOneY; // How far away the mouse is from eye 1 float distTwoX, distTwoY; // How far away the mouse is from eye 2 float eyeMove; // How far the eyes should move relative to the mouse distance ////BULLET VARIABLES//// float bulletSpeed; // How fast the bullet travel float bulletSpeedDec; // How much slower the bullet will travel with difficulty float bulletWidth; // The width of the bullet float bulletHeight; // The height of the bullet float bulletR; // R channel float bulletG; // G channel float bulletB; // B channel boolean bulletFired; // Whether the bullet has been fired float bulletX, bulletY; // X/Y coordinates float bulletRise, bulletRun; // Rise/Run to target float bulletDistance; // Distance to target float bulletStepX, bulletStepY; // How far to move the bullet in the X/Y axis ////TARGET VARIABLES//// float targetSpeed; // How fast the targets move float targetSpeedInc; // How much faster the targets get with difficulty float targetTargetRadius; // How far away from the center the target can target float targetWidth, targetHeight; // The width/height of the targets float targetWidthDec, targetHeightDec; // How much smaller the width/height of the targets will get with difficulty float targetRespawnTime; // How long it will take for each target to respawn float targetRespawnTimeInc; // How much faster the targets respawn with difficulty float targetTimeReward; // How much time to reward to the player upon death float targetRewardDec; // How much less of a reward is given with difficulty float targetR; // R channel float targetG; // G channel float targetB; // B channel // Target alive/dead states boolean targetOneAlive; float targetOneRespawnTime; boolean targetTwoAlive; float targetTwoRespawnTime; boolean targetThreeAlive; float targetThreeRespawnTime; boolean targetFourAlive; float targetFourRespawnTime; boolean targetFiveAlive; float targetFiveRespawnTime; // Target X/Y coordinates float targetOneX, targetOneY; float targetTwoX, targetTwoY; float targetThreeX, targetThreeY; float targetFourX, targetFourY; float targetFiveX, targetFiveY; void setup() { // Set size of screen size(400, 400); // Set up the game setupGame(); // Set up all necessary values setupDifficulty(); setupPlayer(); setupTargets(); println("Move around (WASD) and shoot the cubes (Mouse Click) before time runs out to add more time and survive longer."); println("If you survive long enough, the difficulty increases, indicated in the top left."); println("If you die, press any key to restart."); } void draw() { // Get the current time currentTime = millis(); // Redraw the background background(backR, backG+backInc, backB); // If the game is on the game screen... if (screen==1) { // Update all difficulty-related functions updateDifficulty(); // Draw the cursor drawCursor(); // Draw the current level drawLevelGUI(); // Update player updateBullet(); moveplayer(); drawplayer(); // Update targets updateTargets(); } else { rectMode(CORNER); fill(126, 49, 36); noStroke(); rect(0, 0, width, height); } } void mousePressed() { // If the bullet has not been fired... if (bulletFired==false) { // Get the direction to fire the bullet in bulletRise = mouseY - playerY; bulletRun = mouseX - playerX; bulletDistance = sqrt((bulletRun*bulletRun)+(bulletRise*bulletRise)); bulletStepX = bulletRun/bulletDistance; bulletStepY = bulletRise/bulletDistance; bulletX = playerX; bulletY = playerY; bulletFired = true; } } void keyPressed() { // If the screen is the game screen... if (screen==1) { if (key=='w') { goUp = true; } else if (key=='s') { goDown = true; } else if (key=='a') { goLeft = true; } else if (key=='d') { goRight = true; } } else { // Set up the game setupGame(); // Set up all necessary values setupDifficulty(); setupPlayer(); setupTargets(); // Reset all outstanding values level=1; levelIncTime+=levelTimeAdd; screen=1; } } void keyReleased() { // If the screen is the game screen... if (screen==1) { // If W is pressed... if (key=='w') { // player should not go up goUp = false; } // If S is pressed... else if (key=='s') { // player should not go down goDown = false; } // If A is pressed... else if (key=='a') { // player should not go left goLeft = false; } // If D is pressed... else if (key=='d') { // player should not go right goRight = false; } } } //////USER-DEFINED FUNCTIONS////// void setupGame() { // Get rid of the cursor noCursor(); // Set the background colours backR = 98; backG = 204; backB = 185; backInc = 0; // Draw the background background(backR, backG+backInc, backB); // The game is on the title screen screen = 1; // The game is on level 1 level = 1; } void setupDifficulty() { // Set up the difficulty maxTime = (10)*60; timeLeft = maxTime; levelTimeAdd = (12)*1000; levelIncTime = levelTimeAdd; maxLevel = 5; } void setupPlayer() { // Set basic values playerSpeed = 3; playerSpeedInc = 1; // Set up head playerX = width/2; playerY = height/2; playerWidth = 50; playerHeight = 50; headR = 29; headG = 125; headB = 137; // Set up eyes eyeOneX = playerX - 10; eyeOneY = playerY - 8; eyeTwoX = playerX + 10; eyeTwoY = playerY - 8; eyeLineSize = 5; eyeMove = 0.01; // Set up bullet bulletFired = false; bulletSpeed = 16; bulletSpeedDec = 2.5; bulletWidth = 6; bulletHeight = 6; bulletR = headR*1.5; bulletG = headG*1.5; bulletB = headB*1.5; } void setupTargets() { // Set up targets targetWidth = 20; targetHeight = 20; targetWidthDec = 3; targetHeightDec = 3; targetSpeed = 5; targetTargetRadius = 10; targetRespawnTime = 3; targetTimeReward = 1.5; targetRewardDec = 0.15; targetR = 98; targetG = 255; targetB = 185; // Set up target 1 targetOneAlive = false; targetOneRespawnTime = currentTime; // Set up target 2 targetTwoAlive = false; targetTwoRespawnTime = currentTime; // Set up target 3 targetThreeAlive = false; targetThreeRespawnTime = currentTime; // Set up target 4 targetFourAlive = false; targetFourRespawnTime = currentTime; // Set up target 5 targetFiveAlive = false; targetFiveRespawnTime = currentTime; } void updateDifficulty() { // Update the time timeLeft--; drawBox(); // If the player has run out of time... if (timeLeft<=0) { // Kill the player screen = 0; } if (currentTime>=levelIncTime) { increaseDifficulty(); } } void drawBox() { // Draw box representing how much time the player has left rectMode(CORNERS); fill(255-(timeLeft*0.425), 50, 25); noStroke(); rect(2, 398, 0+(timeLeft*0.66333333333), 380); } void increaseDifficulty() { // If the max level has not been reached... if (level<maxLevel) { // Increase the difficulty level++; levelIncTime+=levelTimeAdd; backInc+=10; targetWidth-=targetWidthDec; targetHeight-=targetHeightDec; bulletSpeed-=bulletSpeedDec; targetTimeReward-=targetRewardDec; } } void drawCursor() { // Set up cursor ellipseMode(CENTER); noStroke(); // Draw the cursor fill(headR, headG, headB, 255/1.5); ellipse(mouseX, mouseY, 10, 10); // Draw the cursor trail fill(headR, headG, headB, 255/3); ellipse(pmouseX, pmouseY, 20, 20); } void drawLevelGUI() { rectMode(CORNER); fill(backR/1.5, backG/1.5, backB/1.5); noStroke(); rect(0, 0, width, 25); // For the current level... for (int i=0; i<level; i++) { // Draw a box to represent it rectMode(CENTER); fill(backR/3, backG/3, backB/3); noStroke(); rect((i*23)+12, 12, 20, 20); } } void moveplayer() { // If the screen is the game screen... if (screen==1) { // If player should go up and hasn't reached the top boundary... if (goUp&&(playerY-playerHeight/2>0)) { // Move player up playerY-=playerSpeed; } // If player should go down and hasn't reached the bottom boundary... if (goDown&&(playerY+playerHeight/2<height)) { // Move player down playerY+=playerSpeed; } // If player should go left and hasn't reached the left boundary... if (goLeft&&(playerX-playerWidth/2>0)) { // Move player left playerX-=playerSpeed; } // If player should go right and hasn't reached the right boundary... else if (goRight&&(playerX+playerWidth/2<width)) { // Move player right playerX+=playerSpeed; } } } void drawplayer() { // Draw head ellipseMode(CENTER); noStroke(); fill(headR, headG, headB); ellipse(playerX, playerY, playerWidth, playerHeight); // Draw eyes eyeOneX = playerX - 10; eyeOneY = playerY - 10; eyeTwoX = playerX + 10; eyeTwoY = playerY - 10; distOneX = mouseX-eyeOneX; distOneY = mouseY-eyeOneY; distTwoX = mouseX-eyeTwoY; distTwoY = mouseY-eyeTwoY; stroke(0); strokeWeight(1.5); line(eyeOneX-eyeLineSize+(distOneX*eyeMove), eyeOneY-eyeLineSize+(distOneY*eyeMove), eyeOneX+eyeLineSize+(distOneX*eyeMove), eyeOneY+eyeLineSize+(distOneY*eyeMove)); line(eyeOneX-eyeLineSize+(distOneX*eyeMove), eyeOneY+eyeLineSize+(distOneY*eyeMove), eyeOneX+eyeLineSize+(distOneX*eyeMove), eyeOneY-eyeLineSize+(distOneY*eyeMove)); line(eyeTwoX-eyeLineSize+(distTwoX*eyeMove), eyeTwoY-eyeLineSize+(distTwoY*eyeMove), eyeTwoX+eyeLineSize+(distTwoX*eyeMove), eyeTwoY+eyeLineSize+(distTwoY*eyeMove)); line(eyeTwoX-eyeLineSize+(distTwoX*eyeMove), eyeTwoY+eyeLineSize+(distTwoY*eyeMove), eyeTwoX+eyeLineSize+(distTwoX*eyeMove), eyeTwoY-eyeLineSize+(distTwoY*eyeMove)); } void updateBullet() { boolean bulletCollided; // Whether the bullet has collided with something // If the bullet has been fired if (bulletFired) { // Check whether it has collided with each target if (targetOneAlive) { bulletCollided = checkForTargetCollision(targetOneX, targetOneY); // If the bullet collided with target 1... if (bulletCollided==true) { // Kill the target targetOneAlive = false; targetOneRespawnTime = currentTime+(targetRespawnTime*1000); // Despawn the bullet bulletFired = false; } } if (targetTwoAlive) { bulletCollided = checkForTargetCollision(targetTwoX, targetTwoY); // If the bullet collided with target 2... if (bulletCollided==true) { // Kill the target targetTwoAlive = false; targetTwoRespawnTime = currentTime+(targetRespawnTime*1000); // Despawn the bullet bulletFired = false; } } if (targetThreeAlive) { bulletCollided = checkForTargetCollision(targetThreeX, targetThreeY); // If the bullet collided with target 3... if (bulletCollided==true) { // Kill the target targetThreeAlive = false; targetThreeRespawnTime = currentTime+(targetRespawnTime*1000); // Despawn the bullet bulletFired = false; } } if (targetFourAlive) { bulletCollided = checkForTargetCollision(targetFourX, targetFourY); // If the bullet collided with target 4... if (bulletCollided==true) { // Kill the target targetFourAlive = false; targetFourRespawnTime = currentTime+(targetRespawnTime*1000); // Despawn the bullet bulletFired = false; } } if (targetFiveAlive) { bulletCollided = checkForTargetCollision(targetFiveX, targetFiveY); // If the bullet collided with target 5... if (bulletCollided==true) { // Kill the target targetFiveAlive = false; targetFiveRespawnTime = currentTime+(targetRespawnTime*1000); // Despawn the bullet bulletFired = false; } } // If the bullet has not reached the edges of the screen... if ((bulletX<width&&bulletX>0)&&(bulletY<height&&bulletY>0)) { bulletX+=bulletStepX*bulletSpeed; bulletY+=bulletStepY*bulletSpeed; } else { // Despawn the bullet bulletFired = false; // Despawn the bullet bulletFired = false; } // Draw bullet ellipseMode(CENTER); stroke(bulletR, bulletG, bulletB); fill(bulletR, bulletG, bulletB); ellipse(bulletX, bulletY, bulletWidth, bulletHeight); // Draw bullet trail fill(bulletR, bulletG, bulletB, 255/3); ellipse(bulletX-bulletStepX, bulletY-bulletStepY, bulletWidth+2, bulletHeight+2); } } boolean checkForTargetCollision(float targetX, float targetY) { boolean collision = false; // Whether a collision is detected // If the bullet has collided with a target... if ( ((bulletX>targetX-(targetWidth/2))&&(bulletX<targetX+(targetWidth/2))) && ((bulletY>targetY-(targetHeight/2))&&(bulletY<targetY+(targetHeight/2))) ) { // A collision has been detected collision = true; // If the player can be given more time... if (timeLeft+(targetTimeReward*60)<=maxTime) { // Give the player more time timeLeft+=(targetTimeReward)*60; } } // Return whether a collision has been detected return collision; } void updateTargets() { // Update target 1 // If the target is alive... if (targetOneAlive) { drawTarget(targetOneX, targetOneY); } else { // If it is time for the target to respawn if (currentTime >= targetOneRespawnTime) { // Respawn the target targetOneAlive = true; // Generate a new original position targetOneX = random(0+targetWidth, width-targetWidth); targetOneY = random(30+targetHeight, height-targetHeight-5); } } // Update target 2 // If the target is alive... if (targetTwoAlive) { drawTarget(targetTwoX, targetTwoY); } else { // If it is time for the target to respawn if (currentTime >= targetTwoRespawnTime) { // Respawn the target targetTwoAlive = true; // Generate a new original position targetTwoX = random(0+targetWidth, width-targetWidth); targetTwoY = random(30+targetHeight, height-targetHeight-5); } } // Update target 3 // If the target is alive... if (targetThreeAlive) { drawTarget(targetThreeX, targetThreeY); } else { // If it is time for the target to respawn if (currentTime >= targetThreeRespawnTime) { // Respawn the target targetThreeAlive = true; // Generate a new original position targetThreeX = random(0+targetWidth, width-targetWidth); targetThreeY = random(30+targetHeight, height-targetHeight-5); } } // Update target 4 // If the target is alive... if (targetFourAlive) { drawTarget(targetFourX, targetFourY); } else { // If it is time for the target to respawn if (currentTime >= targetFourRespawnTime) { // Respawn the target targetFourAlive = true; // Generate a new original position targetFourX = random(0+targetWidth, width-targetWidth); targetFourY = random(30+targetHeight, height-targetHeight-5); } } // Update target 5 // If the target is alive... if (targetFiveAlive) { drawTarget(targetFiveX, targetFiveY); } else { // If it is time for the target to respawn if (currentTime >= targetFiveRespawnTime) { // Respawn the target targetFiveAlive = true; // Generate a new original position targetFiveX = random(0+targetWidth, width-targetWidth); targetFiveY = random(30+targetHeight, height-targetHeight-5); } } } void drawTarget(float targetX, float targetY) { rectMode(CENTER); fill(targetR, targetG, targetB); noStroke(); rect(targetX, targetY, targetWidth, targetHeight); }