/****************************************************************** * File Name: dodgeTheThings * Author: Maddi McDougall * Date: Oct 7, 2015 * Description: The user can move the player square up and down (by * pressing W to jump or by letting gravity take its course). The * goal is to dodge the stars shooting across the screen. If the * player collides with one the toy is reset. If the player collides * with the bottom of the screen they bounce up. Have fun! ******************************************************************/ //*User Defined Variables:*****************************************************************// /************************************** * Player Variables: * Player size, player's x and y * position, player velocity, jump * magnitude, gravity (only affects * player), variable for player jumping * (moving up) ***************************************/ int playerSize = 12; int playerPositionX = 30; float playerPositionY = 30; float playerVelocity = 0; float playerJumpAcceleration = 0.2; float gravity = 0.4; boolean playerMovingUp = false; /************************************** * Player-Star Collision Variable: * Used to determine if the player has * collided with a star. ***************************************/ boolean playerStarCollision = false; /************************************** * Star Variables: * Star size, initial star speed, star * acceleration ***************************************/ int starSize = 6; float starVelocity = 2; float starAcceleration = 0.005; /************************************** * Variables for Star 1: * Starting colour, X, and Y positions ***************************************/ int star01ColourR = 255; int star01ColourG = 0; int star01ColourB = 0; float star01PositionX = 400; int star01PositionY = 0; /************************************** * Variables for Star 2: * Starting colour, X, and Y positions ***************************************/ int star02ColourR = 255; int star02ColourG = 255; int star02ColourB = 0; float star02PositionX = 500; int star02PositionY = 0; /************************************** * Variables for Star 3: * Starting colour, X, and Y positions ***************************************/ int star03ColourR = 255; int star03ColourG = 255; int star03ColourB = 0; float star03PositionX = 600; int star03PositionY = 0; /************************************** * Variables for Star 4: * Starting colour, X, and Y positions ***************************************/ int star04ColourR = 0; int star04ColourG = 255; int star04ColourB = 0; float star04PositionX = 700; int star04PositionY = 0; //*Non-User Defined Functions:*************************************************************// /******************************************************** * Setup: * Sets screen size, no outline, frame rate set to 30fps ********************************************************/ void setup() { size(400, 200); noStroke(); frameRate(30); } /******************************************************** * Draw: * Creates a background and then calls the function to * draw the more detailed background overtop of it. * * Calls the functions to move the player, check if the * player has collided with the screen boundaries, and * draw the player. * * Calls the functions to move the stars, check if they * need to be reset, and display the stars. * Updates the stars' velocity. * * Calls the function to check if the player has collided * with a star, and then if that function determines it * to be true, the next function resets everything. ********************************************************/ void draw() { background(10); drawBackground(); movePlayer(playerMovingUp); checkPlayerScreenCollisions(); drawPlayer(playerPositionY); moveStar(); checkStarReset(); displayStars(); starVelocity = starVelocity + starAcceleration; checkPlayerStarCollisions(); resetIfCollisionDetected(); } //*User Defined Functions:*****************************************************************// /******************************************************** * Check Star Reset: * Checks if any of the stars have gone outside of the * screen's boundaries on the left side. * If they do, it resets them to the right side so they * can travel across the screen again. ********************************************************/ void checkStarReset() { if (star01PositionX < -starSize*5) { resetStar(1); assignRandomStarColour(1); assignRandomYPosition(1); } if (star02PositionX < -starSize*5) { resetStar(2); assignRandomStarColour(2); assignRandomYPosition(2); } if (star03PositionX < -starSize*5) { resetStar(3); assignRandomStarColour(3); assignRandomYPosition(3); } if (star04PositionX < -starSize*5) { resetStar(4); assignRandomStarColour(4); assignRandomYPosition(4); } } /******************************************************** * Assign Random Star Colour: * Generates a random (primarily blue) colour. * Checks the starNumber passed in and changes that * star's colour accordingly. This gives the appearance * of many stars instead of four perpetually looping ones. ********************************************************/ void assignRandomStarColour(int starNumber) { //The random perameters are set so that the colour generated is //in the blue colour range. int starColourR = int(random(60, 125)); int starColourG = int(random(80, 255)); int starColourB = int(random(220, 255)); //Looks for which star needs to be changed, changes it accordingly if (starNumber == 1) { star01ColourR = starColourR; star01ColourG = starColourG; star01ColourB = starColourB; } else if (starNumber == 2) { star02ColourR = starColourR; star02ColourG = starColourG; star02ColourB = starColourB; } else if (starNumber == 3) { star03ColourR = starColourR; star03ColourG = starColourG; star03ColourB = starColourB; } else if (starNumber == 4) { star04ColourR = starColourR; star04ColourG = starColourG; star04ColourB = starColourB; } } /******************************************************** * Move Star: * Adds the star's speed to it's position. All stars move * at the same speed and to the left. ********************************************************/ void moveStar() { star01PositionX = star01PositionX - starVelocity; star02PositionX = star02PositionX - starVelocity; star03PositionX = star03PositionX - starVelocity; star04PositionX = star04PositionX - starVelocity; } /******************************************************** * Assign Random Y Position: * Checks the starNumber passed in and changes that * star's Y position to a random value between 0 and the * height minus the star's size. This keeps the star from * being positioned below the screen (because the fire's * rectangles are drawn from the top corner). ********************************************************/ void assignRandomYPosition(int starNumber) { if (starNumber == 1) { star01PositionY = int(random(0, height-starSize)); } else if (starNumber == 2) { star02PositionY = int(random(0, height-starSize)); } else if (starNumber == 3) { star03PositionY = int(random(0, height-starSize)); } else if (starNumber == 4) { star04PositionY = int(random(0, height-starSize)); } } /******************************************************** * Reset Star: * Checks the starNumber passed in and changes that * star's X position to the right side (width) of the * screen. This causes the stars to loop from one right * to left indefinitely. ********************************************************/ void resetStar(int starNumber) { if (starNumber == 1) { star01PositionX = width; } else if (starNumber == 2) { star02PositionX = width; } else if (starNumber == 3) { star03PositionX = width; } else if (starNumber == 4) { star04PositionX = width; } } /******************************************************** * Displays Stars: * Calls the drawStars functions and passes the * appropriate arguments into them. ********************************************************/ void displayStars() { drawStar01(star01ColourR, star01ColourG, star01ColourB, star01PositionX, star01PositionY); drawStar02(star02ColourR, star02ColourG, star02ColourB, star02PositionX, star02PositionY); drawStar03(star03ColourR, star03ColourG, star03ColourB, star03PositionX, star03PositionY); drawStar04(star04ColourR, star04ColourG, star04ColourB, star04PositionX, star04PositionY); } /******************************************************** * Draw Star 1: * Creates a series of squares for the 'star'. Every * value is passed in (except for the alpha fill value) * from the DisplayStars function. ********************************************************/ void drawStar01(int r, int g, int b, float x, int y) { /*RGB vaules are generated by the assignRandomStarColour function * x is determined by the moveStar and resetStar functions * y is determined by the assignRandomYPosition function * Star size is determined in the global variables at the top */ fill(r, g, b); rect(x, y, starSize, starSize); fill(r, g, b, 127); rect(x+starSize, y, starSize, starSize); fill(r, g, b, 63); rect(x+2*(starSize), y, starSize, starSize); fill(r, g, b, 31); rect(x+3*(starSize), y, starSize, starSize); fill(r, g, b, 15); rect(x+4*(starSize), y, starSize, starSize); } /******************************************************** * Draw Star 2: * Creates a series of squares for the 'star'. Every * value is passed in (except for the alpha fill value) * from the DisplayStars function. ********************************************************/ void drawStar02(int r, int g, int b, float x, int y) { /*RGB vaules are generated by the assignRandomStarColour function * x is determined by the moveStar and resetStar functions * y is determined by the assignRandomYPosition function * Star size is determined in the global variables at the top */ fill(r, g, b); rect(x, y, starSize, starSize); fill(r, g, b, 127); rect(x+starSize, y, starSize, starSize); fill(r, g, b, 63); rect(x+2*(starSize), y, starSize, starSize); fill(r, g, b, 31); rect(x+3*(starSize), y, starSize, starSize); fill(r, g, b, 15); rect(x+4*(starSize), y, starSize, starSize); } /******************************************************** * Draw Star 3: * Creates a series of squares for the 'star'. Every * value is passed in (except for the alpha fill value) * from the DisplayStars function. ********************************************************/ void drawStar03(int r, int g, int b, float x, int y) { /*RGB vaules are generated by the assignRandomStarColour function * x is determined by the moveStar and resetStar functions * y is determined by the assignRandomYPosition function * Star size is determined in the global variables at the top */ fill(r, g, b); rect(x, y, starSize, starSize); fill(r, g, b, 127); rect(x+starSize, y, starSize, starSize); fill(r, g, b, 63); rect(x+2*(starSize), y, starSize, starSize); fill(r, g, b, 31); rect(x+3*(starSize), y, starSize, starSize); fill(r, g, b, 15); rect(x+4*(starSize), y, starSize, starSize); } /******************************************************** * Draw Star 4: * Creates a series of squares for the 'star'. Every * value is passed in (except for the alpha fill value) * from the DisplayStars function. ********************************************************/ void drawStar04(int r, int g, int b, float x, int y) { /*RGB vaules are generated by the assignRandomStarColour function * x is determined by the moveStar and resetStar functions * y is determined by the assignRandomYPosition function * Star size is determined in the global variables at the top */ fill(r, g, b); rect(x, y, starSize, starSize); fill(r, g, b, 127); rect(x+starSize, y, starSize, starSize); fill(r, g, b, 63); rect(x+2*(starSize), y, starSize, starSize); fill(r, g, b, 31); rect(x+3*(starSize), y, starSize, starSize); fill(r, g, b, 15); rect(x+4*(starSize), y, starSize, starSize); } /******************************************************** * Draw Background: * Uses a loop to create a gradient effect. The values * for r, g, and b are hard-coded based on a linear eq'n * that I determined after picking colours in Photoshop. * Uses several loops to draw triangles (mountains) * at the bottom of the screen. * Adds a line behind the player to show that they can * only move up and down. ********************************************************/ void drawBackground() { //Sky gradient for (int y = 0; y <= height; y++) { float r = 1.7; float g = 0.24; float b = 1.78; fill(9+y*r, 7+y*g, 14+y*b); rect(0, y*5, width, 5); } //Mountains for (int x = -10; x <= width; x=x+90) { fill(9, 7, 14); triangle(x, height, x+30, height-40, x+65, height); } //Mountains for (int x2 = 0; x2 <= width; x2=x2+105) { fill(9, 7, 14); triangle(x2, height, x2+40, height-60, x2+80, height); } //Mountains for (int x3 = -5; x3 <= width; x3=x3+59) { fill(9, 7, 14); triangle(x3, height, x3+30, height-25, x3+60, height); } //Line fill(255, 255, 255, 20); rect(playerPositionX+(playerSize/2)-1, 0, 2, height); } /******************************************************** * Key Pressed: * Checks to see if the appropriate key is pressed. * Toggles the playerMovingUp accordingly. ********************************************************/ void keyPressed() { if (key == 'w' || key == 'W') { playerMovingUp = true; } else { playerMovingUp = false; } } /******************************************************** * Key Released: * Checks to see if the appropriate key is released. * Toggles the playerMovingUp to false. ********************************************************/ void keyReleased() { if (key == 'w' || key == 'W') { playerMovingUp = false; } } /******************************************************** * Move Player: * If the player is moving up, adds the jump acceleration * to the player's velocity. Updates the position based * on this new value. * Otherwise, gravity is added to the player's velocity * and their position is updated accordingly. ********************************************************/ void movePlayer(boolean movingUp) { if (movingUp) { playerVelocity = playerVelocity - 2 + playerJumpAcceleration; playerPositionY = playerPositionY + playerVelocity; } else { playerVelocity = playerVelocity + gravity; playerPositionY = playerPositionY + playerVelocity; } } /******************************************************** * Draw Player: * Displays the player. Takes in the player's current * calculated y position. Size is based on playerSize. ********************************************************/ void drawPlayer(float y) { //Body fill(255); rect(playerPositionX, y, playerSize, playerSize); //Eyes fill(0); rect(playerPositionX + (playerSize*1/6), y + 6, (playerSize*1/6), (playerSize*2/6)); rect(playerPositionX + (playerSize*4/6), y + 6, (playerSize*1/6), (playerSize*2/6)); } /******************************************************** * Check Player-Screen Collisions: * If the player reaches the top of the screen their * velocity and position are reset (so they don't bounce * off of the ceiling). * If the player reaches the bottom their position is * reset (so they don't pass through the screen) and * their velocity is inverted so they 'bounce' off of the * ground. ********************************************************/ void checkPlayerScreenCollisions() { if (playerPositionY < 0) { playerVelocity = 0; playerPositionY = 0; } else if (playerPositionY+playerSize > height) { playerPositionY=height-playerSize; playerVelocity = -playerVelocity; } } /******************************************************** * Check Player-Star Collisions: * Determines if the player's shape has collided with one * of the stars'. Collisions with all stars have the same * result. The collision calculation only takes the first * square in the star's shape (because the other squares * act as a trail). ********************************************************/ void checkPlayerStarCollisions() { if (playerPositionX + playerSize >= star01PositionX && playerPositionX <= star01PositionX + starSize && playerPositionY + playerSize >= star01PositionY && playerPositionY <= star01PositionY + starSize) { playerStarCollision = true; } else if (playerPositionX + playerSize >= star02PositionX && playerPositionX <= star02PositionX + starSize && playerPositionY + playerSize >= star02PositionY && playerPositionY <= star02PositionY + starSize) { playerStarCollision = true; } else if (playerPositionX + playerSize >= star03PositionX && playerPositionX <= star03PositionX + starSize && playerPositionY + playerSize >= star03PositionY && playerPositionY <= star03PositionY + starSize) { playerStarCollision = true; } else if (playerPositionX + playerSize >= star04PositionX && playerPositionX <= star04PositionX + starSize && playerPositionY + playerSize >= star04PositionY && playerPositionY <= star04PositionY + starSize) { playerStarCollision = true; } else { playerStarCollision = false; } } /******************************************************** * Reset If Collisions Detected: * Checks if playerStarCollision is true, and resets * the player and stars to the initialized values. (To * create a "new game".) After that it toggles * playerStarCollision to false to start the new game. ********************************************************/ void resetIfCollisionDetected() { if (playerStarCollision) { //Reset player variables playerPositionY = 30; playerVelocity = 0; playerMovingUp = false; //Reset star velocity and acceleration starVelocity = 2; starAcceleration = 0.005; //Reset star 1 to initial values star01ColourR = 255; star01ColourG = 0; star01ColourB = 0; star01PositionX = 400; star01PositionY = 0; //Reset star 2 to initial values star02ColourR = 255; star02ColourG = 255; star02ColourB = 0; star02PositionX = 500; star02PositionY = 0; //Reset star 3 to initial values star03ColourR = 255; star03ColourG = 255; star03ColourB = 0; star03PositionX = 600; star03PositionY = 0; //Reset star 4 to initial values star04ColourR = 0; star04ColourG = 255; star04ColourB = 0; star04PositionX = 700; star04PositionY = 0; //Reset colission status playerStarCollision = false; } }