///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ,--, //
//,---.'| ,----.. //
//| | : ,---, .--.--. ,---,.,-.----. ,---, / / \ ,---, ,----.. ,---,. //
//: : | ' .' \ / / '. ,' .' |\ / \ .' .' `\ / . : .' .' `\ / / \ ,' .' | //
//| ' : / ; '. | : /`. / ,---.' |; : \ ,---.' \ . / ;. \,---.' \ | : : ,---.' | //
//; ; ' : : \ ; | |--` | | .'| | .\ : | | .`\ |. ; / ` ;| | .`\ |. | ;. / | | .' //
//' | |__ : | /\ \| : ;_ : : |-,. : |: | : : | ' |; | ; \ ; |: : | ' |. ; /--` : : |-, //
//| | :.'|| : ' ;. :\ \ `. : | ;/|| | \ : | ' ' ; :| : | ; | '| ' ' ; :; | ; __ : | ;/| //
//' : ;| | ;/ \ \`----. \| : .'| : . / ' | ; . |. | ' ' ' :' | ; . || : |.' .'| : .' //
//| | ./ ' : | \ \ ,'__ \ \ || | |-,; | | \ | | : | '' ; \; / || | : | '. | '_.' :| | |-, //
//; : ; | | ' '--' / /`--' /' : ;/|| | ;\ \ ' : | / ; \ \ ', / ' : | / ; ' ; : \ |' : ;/| //
//| ,/ | : : '--'. / | | \: ' | \.' | | '` ,/ ; : / | | '` ,/ ' | '/ .'| | \ //
//'---' | | ,' `--'---' | : .': : :-' ; : .' \ \ .' ; : .' | : / | : .' //
// `--'' | | ,' | |.' | ,.' `---` | ,.' \ \ .' | | ,' //
// `----' `---' '---' '---' `---` `----' //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LASER DODGE BY NICHOLAS PHAN
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Created on October 6, 2015
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Click and drag to launch the player square out of the trajectory of the lasers. Dont leave the boundaries of the screen or get
//hit, otherwise you'll have to restart. The game increases difficulty over time, causing the lasers to become faster. Survive as
//long as possible and aim to beat your high score.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CONTROLS
//Click and drag the mouse to launch the player.
//Press Enter to restart the game at any point.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//VERSION HISTORY
//v1.1 -- Player moves with wasd. One laser to dodge. Screen boundary stop the player.
//v1.2 -- Space gives invincibility for 3s.
//v1.3 -- Player follows mouse. Additional laser added.
//v1.4 -- Player is launched by mouse. Added difficulty increasing and timer
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Background
int backgroundRed=140;
int backgroundBlue=235;
int backgroundGreen=230;
float backgroundSize =800;
//player
float playerPositionX;
float playerPositionY;
float playerSizeX;
float playerSizeY;
//Move indicators
float playerMoveGuideX;
float playerMoveGuideY;
float clickRectSize =0;
//Boundaries
float topBoundary = 25;
float bottomBoundary = 775;
float leftBoundary = 25;
float rightBoundary = 775;
//lasers
float launchValueX;
float launchValueY;
int shooterColourChangeY=0;
int shooterColourChangeX=0;
float launchDistanceX =10;
float launchDistanceY =10;
float shootTimerH;
float shootTimerV;
float easing = 0.01;
float slowEasing = 0.003;
float horizontalShooterX;
float verticalShooterY;
//Timers
int startTime=millis();
int currentTime=0;
//Game
boolean gameState = true;
void setup() {
size(800, 800);
//Set player to midpoint
playerPositionX = width/2;
playerPositionY = height/2;
//Set Lasers to midpoint
horizontalShooterX = width/2;
verticalShooterY = height/2;
rectMode(CENTER);
ellipseMode(CENTER);
noStroke();
//Set player size
playerSizeX=50;
playerSizeY=50;
}
void draw() {
//Allows for a game over screen to be implemented more easily
loadGame();
}
/////////////////////////////////////////////////////////////////////////////////////////
//DIFFICULTY
/////////////////////////////////////////////////////////////////////////////////////////
//Loads the background, timers, move indicators, the player, and lasers. Also checks all
//boundaries.
/////////////////////////////////////////////////////////////////////////////////////////
void loadGame()
{
if (gameState == true)
{
//Loads the all aspects of the playable game while true
updateBackground();
timer();
drawMoveIndicator();
updateSquare();
checkBorders();
updateHorizontalShooter();
updateVerticalShooter();
difficultyIncreaser();
} else {
//Calls the Game Over screen
gameOver();
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//DIFFICULTY
/////////////////////////////////////////////////////////////////////////////////////////
//Increases the difficulty depending on the time. Difficulty changes by the amount of
//ease on the laser shooters. The greater the ease value, the more quickly they track the
//player's Position. The difficulty is indicated by how dark the background and increas
//at 15s, 25s, and 35s.
/////////////////////////////////////////////////////////////////////////////////////////
void difficultyIncreaser()
{
//If the current time is below 15s, set the background's colour
if (currentTime < 15000)
{
backgroundRed=140;
backgroundGreen=230;
backgroundBlue=235;
}
//If the current time is above 15s, darken the background's colour and increase easing value
if (currentTime > 15000)
{
easing = 0.02;
slowEasing = 0.005;
backgroundRed=66;
backgroundGreen=152;
backgroundBlue=157;
}
//If the current time is above 25s, darken the background's colour and increase easing value
if (currentTime > 25000)
{
easing = 0.04;
slowEasing = 0.007;
backgroundRed=0;
backgroundGreen=74;
backgroundBlue=79;
}
//If the current time is above 35s, darken the background's colour and increase easing value
if (currentTime > 35000)
{
easing = 0.09;
slowEasing = 0.01;
backgroundRed=0;
backgroundGreen=0;
backgroundBlue=0;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//TIMER
/////////////////////////////////////////////////////////////////////////////////////////
//Sets a counter that counts up in seconds to appear in the bottom left corner of the
//screen. Other function use this value as well (Such as the Game Over screen).
/////////////////////////////////////////////////////////////////////////////////////////
void timer()
{
fill(255, 50);
textSize(400);
currentTime=millis()-startTime;
text(currentTime/1000, 50, 750);
}
/////////////////////////////////////////////////////////////////////////////////////////
//MOVE HORIZONTAL SHOOTER
/////////////////////////////////////////////////////////////////////////////////////////
//Uses the players X position as a target and eases its own X position to that of the
//player's. Includes 3 timers. One determines when it should slow dow to prepare to fire
//its laser, the second calls the laser function shortly after the first, and the third
//determines when it returns to normal speed.
/////////////////////////////////////////////////////////////////////////////////////////
void updateHorizontalShooter()
{
//Determines when to call the laser function
if (( millis() - shootTimerH) >4600)
{
//Calls Laser function
shootLaserH();
}
//Determines when to end shooting its laser
if ((millis()-shootTimerH)>6000)
{
shootTimerH = millis();
shooterColourChangeX = 0;
}
//Determines when to slow down
if ( ( millis() - shootTimerH) <4000)
{
shooterColourChangeX = 0;
//eases Laser position slowly
float targetX = playerPositionX;
float dx = targetX - horizontalShooterX ;
horizontalShooterX += dx * easing;
} else {
//eases Laser position
shooterColourChangeX = 255;
float targetX = playerPositionX;
float dx = targetX - horizontalShooterX ;
horizontalShooterX += dx * slowEasing;
}
//Draws the horizontal laser shooter
fill(shooterColourChangeX, 0, 0);
rect(horizontalShooterX, 0, 100, 50);
}
/////////////////////////////////////////////////////////////////////////////////////////
//LASERX
/////////////////////////////////////////////////////////////////////////////////////////
//Draws the laser and calls to check if the player is inside it.
/////////////////////////////////////////////////////////////////////////////////////////
void shootLaserH()
{
//Draws pulsating glow on laser
fill(255, 100);
rect(horizontalShooterX, 400, 45+(sin(frameCount*0.2)*5 +5), 800);
//Draws laser
fill(255);
rect(horizontalShooterX, 400, 40, 800);
//Calls a function to check if the player is in the laser
checkLaserH();
}
/////////////////////////////////////////////////////////////////////////////////////////
//CHECK LASERX
/////////////////////////////////////////////////////////////////////////////////////////
//Checks if the player is inside the laser.
/////////////////////////////////////////////////////////////////////////////////////////
void checkLaserH()
{
//true if left point, mid point or right point of the player is within the laser
if ((playerPositionX-25 <horizontalShooterX+20 && playerPositionX-25 >horizontalShooterX-20) ||(playerPositionX <horizontalShooterX+20 && playerPositionX >horizontalShooterX-20)||(playerPositionX+25 <horizontalShooterX+20 && playerPositionX+25 >horizontalShooterX-20))
{
//Sets Gamestate to false
gameState = false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//MOVE VERTICAL SHOOTER
/////////////////////////////////////////////////////////////////////////////////////////
//Uses the player's Y position as a target and eases its own Y position to that of the
//player's. Includes 3 timers. One determines when it should slow dow to prepare to fire
//its laser, the second calls the laser function shortly after the first, and the third
//determines when it returns to normal speed.
/////////////////////////////////////////////////////////////////////////////////////////
void updateVerticalShooter()
{
///Determines when to call the laser
//True after each interval of 3.5s
if (( millis() - shootTimerV) >3500)
{
//Calls the laser function
shootLaserV();
}
//Determines when to end shooting its laser
//True after every interval of 5 seconds
if ((millis()-shootTimerV)>5000)
{
shootTimerV = millis();
shooterColourChangeY = 0;
}
//Determines when to slow down
//True after each interval of 3s
if ( ( millis() - shootTimerV) <3000)
{
shooterColourChangeY = 0;
//Eases Position slowly
float targetY = playerPositionY;
float dy = targetY - verticalShooterY ;
verticalShooterY += dy * easing;
} else {
//Eases Position
shooterColourChangeY = 255;
float targetY = playerPositionY;
float dy = targetY - verticalShooterY ;
verticalShooterY += dy * slowEasing;
}
//Draws laser Shooter
fill(shooterColourChangeY, 0, 0);
rect(0, verticalShooterY, 50, 100);
}
/////////////////////////////////////////////////////////////////////////////////////////
//LASERY
/////////////////////////////////////////////////////////////////////////////////////////
//Draws the laser and calls to check if the player is inside it.
/////////////////////////////////////////////////////////////////////////////////////////
void shootLaserV()
{
//Draws pulsating glow on Laser
fill(255, 100);
rect(400, verticalShooterY, 800, 45+(sin(frameCount*0.2)*2.5 +5));
//Draws laser
fill(255);
rect(400, verticalShooterY, 800, 40);
//call function to check if player is inside laser
checkLaserV();
}
/////////////////////////////////////////////////////////////////////////////////////////
//CHECK LASERY
/////////////////////////////////////////////////////////////////////////////////////////
//Checks if the player is inside the laser.
/////////////////////////////////////////////////////////////////////////////////////////
void checkLaserV()
{
//true if top point, mid point or bottom point of the player is within the laser
if ((playerPositionY-25 <verticalShooterY+20 && playerPositionY-25 >verticalShooterY-20) ||(playerPositionY <verticalShooterY+20 && playerPositionY >verticalShooterY-20)||(playerPositionY+25 <verticalShooterY+20 && playerPositionY+25 >verticalShooterY-20))
{
//Sets Gamestate to False
gameState = false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//CHECK BORDERS
/////////////////////////////////////////////////////////////////////////////////////////
//Checks if the player is outside the boundarys of the game screen and sets gamestate to
//false if so.
/////////////////////////////////////////////////////////////////////////////////////////
void checkBorders()
{
//True if the player position is outside the top, bottom, left, or right boundary
if (playerPositionX < leftBoundary || playerPositionX > rightBoundary || playerPositionY > bottomBoundary || playerPositionY < topBoundary)
{
//Sets Gamestate to False
gameState = false;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//GAMEOVER SCREEN
/////////////////////////////////////////////////////////////////////////////////////////
//Called when Gamestate = false. Draws the gameover screen and amount of seconds survived.
/////////////////////////////////////////////////////////////////////////////////////////
void gameOver()
{
fill(0);
rect(400, 400, backgroundSize, backgroundSize);
fill(255);
//Prints 'press enter to restart' in the middle of the screen
textSize(30);
text("Press Enter to restart", 250, 420) ;
//Prints amount of time survived at bottom of screen
text("You have survived for " + currentTime/1000+" seconds", 50, 750);
}
/////////////////////////////////////////////////////////////////////////////////////////
//UPDATE BACKGROUND
/////////////////////////////////////////////////////////////////////////////////////////
//Draws the background and causes a red outline around the screen if the player is in
//danger of moving outside of the screen's boundarys.
/////////////////////////////////////////////////////////////////////////////////////////
void updateBackground()
{
//Draws the background
background(170, 80, 80);
fill(backgroundRed, backgroundGreen, backgroundBlue);
rect(400, 400, backgroundSize, backgroundSize);
//Draws a striped pattern on the background
for ( int i =0; i<800;i+=20)
{
fill(random(240, 255),30);
rectMode(CORNER);
rect(0,i,800,10);
rectMode(CENTER);
}
//draws a red outline around the screen when true
//true when the player guide is outside the screens boundaries
if (mousePressed==true && (playerMoveGuideX > 774 ||playerMoveGuideX <26 || playerMoveGuideY >774 ||playerMoveGuideY< 26))
{
backgroundSize = 780;
} else {
backgroundSize =800;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//UPDATE SQUARE
/////////////////////////////////////////////////////////////////////////////////////////
//Draws the player
/////////////////////////////////////////////////////////////////////////////////////////
void updateSquare()
{
//draws the player
fill(255, 140, 140);
rect(playerPositionX, playerPositionY, playerSizeX, playerSizeY);
}
/////////////////////////////////////////////////////////////////////////////////////////
//DRAW MOVE INDICATOR
/////////////////////////////////////////////////////////////////////////////////////////
//Creates a trail from the player's position to the point where the mouse is clicked and
//indicates where the player will be moved at upon release
/////////////////////////////////////////////////////////////////////////////////////////
void drawMoveIndicator()
{
//determines the location of the player guide indicating the location the plkayer will end up
playerMoveGuideX = playerPositionX+(playerPositionX-mouseX);
playerMoveGuideY = playerPositionY+(playerPositionY-mouseY);
if (mousePressed==true)
{
//draws Player to Cursor trail
fill(255, 50);
triangle(mouseX, mouseY, playerPositionX+20, playerPositionY-20, playerPositionX-20, playerPositionY-20);
triangle(mouseX, mouseY, playerPositionX+20, playerPositionY+20, playerPositionX+20, playerPositionY-20);
triangle(mouseX, mouseY, playerPositionX+20, playerPositionY+20, playerPositionX-20, playerPositionY+20);
triangle(mouseX, mouseY, playerPositionX-20, playerPositionY+20, playerPositionX-20, playerPositionY-20);
//draws the player guide
fill(255, 140, 140, 100);
rect(playerMoveGuideX, playerMoveGuideY, clickRectSize, clickRectSize);
//draws a glow on the player to indicate that the mouse is being pressed
//One rect expands outwards while the other pulsates
rect(playerPositionX, playerPositionY, clickRectSize+20, clickRectSize+20);
if (clickRectSize < 30)
{
clickRectSize+=10;
} else {
clickRectSize=35;
clickRectSize+= (sin(frameCount*0.1)*5.0)+5;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//MOUSE RELEASED
/////////////////////////////////////////////////////////////////////////////////////////
//Sets launch values and calls the launch function.
/////////////////////////////////////////////////////////////////////////////////////////
void mouseReleased()
{
launchValueX = mouseX;
launchValueY= mouseY;
clickRectSize =0;
launch();
}
/////////////////////////////////////////////////////////////////////////////////////////
//LAUNCH
/////////////////////////////////////////////////////////////////////////////////////////
//uses mouseX and mouseY to determine the same differences in the opposite direction to
//launch the player.
/////////////////////////////////////////////////////////////////////////////////////////
void launch()
{
launchDistanceX = (playerPositionX-launchValueX);
launchDistanceY = (playerPositionY-launchValueY);
playerPositionY+=(launchDistanceY);
playerPositionX+=(launchDistanceX);
}
/////////////////////////////////////////////////////////////////////////////////////////
//RESET GAME
/////////////////////////////////////////////////////////////////////////////////////////
//Resets entire game when enter is pressed.
/////////////////////////////////////////////////////////////////////////////////////////
void keyPressed()
{
if (key== ENTER)
{
resetSquare();
resetShooterH();
resetShooterV();
resetTime();
gameState =true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
//RESET SQUARE
/////////////////////////////////////////////////////////////////////////////////////////
//resets square to center when function is called.
/////////////////////////////////////////////////////////////////////////////////////////
void resetSquare()
{
playerPositionX = width/2;
playerPositionY = height/2;
}
/////////////////////////////////////////////////////////////////////////////////////////
//RESET HORIZONTAL SHOOTER
/////////////////////////////////////////////////////////////////////////////////////////
//resets shooter to center when function is called.
/////////////////////////////////////////////////////////////////////////////////////////
void resetShooterH()
{
horizontalShooterX = width/2;
shootTimerH = millis();
}
/////////////////////////////////////////////////////////////////////////////////////////
//RESET VERTICAL SHOOTERS
/////////////////////////////////////////////////////////////////////////////////////////
//resets shooter to center when function is called.
/////////////////////////////////////////////////////////////////////////////////////////
void resetShooterV()
{
verticalShooterY = width/2;
shootTimerV = millis();
}
/////////////////////////////////////////////////////////////////////////////////////////
//RESET TIMER
/////////////////////////////////////////////////////////////////////////////////////////
//resets shooter to center when function is called.
/////////////////////////////////////////////////////////////////////////////////////////
void resetTime()
{
currentTime=0;
startTime=millis();
}