Rosie Gillman
Dependencies: mbed Gamepad2 ELEC2645_Project_el18rg
Dependents: ELEC2645_Project_el18rg
Rosemary Gillman 201265952
Objective
The goal of the game is to splat the bug as fast as you can using the swatter.
Controls
1 - Joystick - left/right to control the swatter 2 - Start button - starts the game 3 - Reset - resets the game 4 - Volume pot - adjusts the volume
Instructions
- Turn the gamepad on
- Wait for start screen and press start
- Move the joystick to control the swatter
- Splat the bug as fast as you can
- Press reset to play again
Gameplay
Start Screen 1
- Low pad tone plays for 0.5s
- Pad lights flash (200ms on/200ms off)
- Text saying "Bug Splat Leeds Edition" is displayed
- After 5 flashes the screen changes to Start Screen 2
Start Screen 2
- Pad lights stay on constantly
- Text reads "Splat the bug as fast as you can! Press start"
- When start is pressed the screen changes to Gamplay Screen
Gameplay Screen
- The timer begins
- Bug appears in the top right corner
- Swatter appears in the bottom left corner
- The bug bounces (with random velocity/direction) off the sides
- When the bug bounces off the wall a low pad tone is played each time for 0.1 seconds
- Swatter is controlled left to right with the joystick
- When the bug and swatter overlap the screen changes to the Ending Screen
Ending Screen
- Low pad tone plays for 0.5s
- The timer ends and its value is displayed
- Text reads "SPLAT (time) secs"
- Splats are drawn on the screen
Engine/Engine.cpp@22:0b207694a5f7, 2020-05-29 (annotated)
- Committer:
- el18rg
- Date:
- Fri May 29 21:31:43 2020 +0000
- Revision:
- 22:0b207694a5f7
- Parent:
- 19:bdfab290446a
Final check through
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el18rg | 2:b936aa854de2 | 1 | #include "Engine.h" |
el18rg | 2:b936aa854de2 | 2 | #include "mbed.h" |
el18rg | 2:b936aa854de2 | 3 | #include "Gamepad.h" |
el18rg | 2:b936aa854de2 | 4 | #include "N5110.h" |
el18rg | 16:33d8b58a1a65 | 5 | #include <ctime> |
el18rg | 2:b936aa854de2 | 6 | |
el18rg | 15:4ed54ff548cf | 7 | Engine::Engine() {} //Constuctor |
el18rg | 15:4ed54ff548cf | 8 | Engine::~Engine() {} //Destructor |
el18rg | 2:b936aa854de2 | 9 | |
el18rg | 15:4ed54ff548cf | 10 | void Engine::init(int swatter_width,int swatter_height,int speed) //initalises the game |
el18rg | 2:b936aa854de2 | 11 | { |
el18rg | 19:bdfab290446a | 12 | width = swatter_width; //set width |
el18rg | 19:bdfab290446a | 13 | height = swatter_height; //set hight |
el18rg | 19:bdfab290446a | 14 | _speed = speed; //sets the speed |
el18rg | 22:0b207694a5f7 | 15 | x = 2; //set x |
el18rg | 19:bdfab290446a | 16 | _swatter.init(x,height,width); //initialise the swatter with parameters |
el18rg | 19:bdfab290446a | 17 | _bug.init(_speed); //initilise the bug with speed |
el18rg | 2:b936aa854de2 | 18 | } |
el18rg | 2:b936aa854de2 | 19 | |
el18rg | 15:4ed54ff548cf | 20 | void Engine::read_input(Gamepad &pad) //reads the direction and magnitude inputs |
el18rg | 2:b936aa854de2 | 21 | { |
el18rg | 19:bdfab290446a | 22 | _d = pad.get_direction(); //gets the direction |
el18rg | 19:bdfab290446a | 23 | _mag = pad.get_mag(); //gets the magnitude |
el18rg | 2:b936aa854de2 | 24 | } |
el18rg | 2:b936aa854de2 | 25 | |
el18rg | 15:4ed54ff548cf | 26 | void Engine::draw(N5110 &lcd) //draws the bug and swatter |
el18rg | 2:b936aa854de2 | 27 | { |
el18rg | 19:bdfab290446a | 28 | _bug.draw(lcd); //draws the bug |
el18rg | 19:bdfab290446a | 29 | _swatter.draw(lcd); //draws the swatter |
el18rg | 2:b936aa854de2 | 30 | } |
el18rg | 2:b936aa854de2 | 31 | |
el18rg | 15:4ed54ff548cf | 32 | void Engine::update(N5110 & lcd, Gamepad & pad) //updates the game |
el18rg | 2:b936aa854de2 | 33 | { |
el18rg | 19:bdfab290446a | 34 | _swatter.update(_d,_mag); //updates the swatter with direction/magnitude |
el18rg | 19:bdfab290446a | 35 | _bug.update(); //updates the bug |
el18rg | 19:bdfab290446a | 36 | check_side_collision(pad); //checks side/bug collision |
el18rg | 19:bdfab290446a | 37 | check_swatter_collisions(lcd, pad); //checks swatter/bug collision |
el18rg | 2:b936aa854de2 | 38 | } |
el18rg | 15:4ed54ff548cf | 39 | void Engine::check_side_collision(Gamepad &pad) //checks if the bug collides with the sides and redirects it |
el18rg | 2:b936aa854de2 | 40 | { |
el18rg | 19:bdfab290446a | 41 | Vector2D bug_pos = _bug.get_pos(); //get the bug position |
el18rg | 19:bdfab290446a | 42 | Vector2D bug_velocity = _bug.get_velocity(); //get the bug velocity |
el18rg | 19:bdfab290446a | 43 | if (bug_pos.y <= 1) { //if the bug touches the top y-axis |
el18rg | 19:bdfab290446a | 44 | bug_pos.y = 1; //put the bug back inside the boundaries |
el18rg | 19:bdfab290446a | 45 | bug_velocity.y = -bug_velocity.y; //reverse the y velocity |
el18rg | 19:bdfab290446a | 46 | bug_velocity.x = rand() % 5 + 0; //bounce on the x-axis at a 0-5 random velocity |
el18rg | 19:bdfab290446a | 47 | pad.tone(750.0,0.1); //play a medium pad tone |
el18rg | 19:bdfab290446a | 48 | } else if (bug_pos.y + 10 >= (HEIGHT-1) ) { //if the bug touches the bottom y-axis |
el18rg | 19:bdfab290446a | 49 | bug_pos.y = (HEIGHT-1) - 10; //put the bug back inside the boundaries |
el18rg | 19:bdfab290446a | 50 | bug_velocity.y = -bug_velocity.y ; //reverse the y velocity |
el18rg | 19:bdfab290446a | 51 | bug_velocity.x = rand() % 5 + 0; //bounce on the x-axis at a 0-5 random velocity |
el18rg | 19:bdfab290446a | 52 | pad.tone(750.0,0.1); //play a medium pad tone |
el18rg | 4:5cc1b81999a9 | 53 | } |
el18rg | 19:bdfab290446a | 54 | if (bug_pos.x <= 1) { //if the bug touches the left x-axis |
el18rg | 19:bdfab290446a | 55 | bug_pos.x = 1; //put the bug back inside the boundaries |
el18rg | 19:bdfab290446a | 56 | bug_velocity.x = -bug_velocity.x; //reverse the x velocity |
el18rg | 19:bdfab290446a | 57 | bug_velocity.y = rand() % 5 + 0; //bounce on the y-axis at a 0-5 random velocity |
el18rg | 19:bdfab290446a | 58 | pad.tone(750.0,0.1); //play a medium pad tone |
el18rg | 19:bdfab290446a | 59 | } else if (bug_pos.x + 10 >= (WIDTH-1) ) { //if the bug touches the right x-axis |
el18rg | 19:bdfab290446a | 60 | bug_pos.x = (WIDTH-1) - 10; //put the bug back inside the boundaries |
el18rg | 19:bdfab290446a | 61 | bug_velocity.x = -bug_velocity.x ; //reverse the x velocity |
el18rg | 19:bdfab290446a | 62 | bug_velocity.y = rand() % 5 + 0; //bounce on the y-axis at a 0-5 random velocity |
el18rg | 19:bdfab290446a | 63 | pad.tone(750.0,0.1); //play a medium pad tone |
el18rg | 2:b936aa854de2 | 64 | } |
el18rg | 2:b936aa854de2 | 65 | |
el18rg | 19:bdfab290446a | 66 | _bug.set_velocity(bug_velocity); //set the bug velocity |
el18rg | 19:bdfab290446a | 67 | _bug.set_pos(bug_pos); //set the bug position |
el18rg | 2:b936aa854de2 | 68 | } |
el18rg | 15:4ed54ff548cf | 69 | void Engine::check_swatter_collisions(N5110 & lcd, Gamepad & pad) //checks if the swatter and bug have collided |
el18rg | 2:b936aa854de2 | 70 | { |
el18rg | 19:bdfab290446a | 71 | Vector2D bug_pos = _bug.get_pos(); //get bug position |
el18rg | 19:bdfab290446a | 72 | Vector2D p_pos = _swatter.get_pos(); //get swatter position |
el18rg | 19:bdfab290446a | 73 | Vector2D bug_velocity = _bug.get_velocity(); //get bug velocity |
el18rg | 13:09bc615e6665 | 74 | |
el18rg | 19:bdfab290446a | 75 | if ((bug_pos.x <= (p_pos.x +5)) && (bug_pos.x +5 >= p_pos.x)) { //check for x-axis collision |
el18rg | 3:2f4a7787beb3 | 76 | collisionX=true; |
el18rg | 11:93da75c1849d | 77 | } else { |
el18rg | 3:2f4a7787beb3 | 78 | collisionX=false; |
el18rg | 3:2f4a7787beb3 | 79 | } |
el18rg | 19:bdfab290446a | 80 | if ((bug_pos.y <= (p_pos.y+5)) && (bug_pos.y+5 >= p_pos.y )) { //check for y-axis collision |
el18rg | 3:2f4a7787beb3 | 81 | collisionY=true; |
el18rg | 11:93da75c1849d | 82 | } else { |
el18rg | 3:2f4a7787beb3 | 83 | collisionY=false; |
el18rg | 2:b936aa854de2 | 84 | } |
el18rg | 19:bdfab290446a | 85 | if (collisionX && collisionY) { //if there's a x and y-axis collision |
el18rg | 19:bdfab290446a | 86 | ending(lcd, pad); //run ending |
el18rg | 11:93da75c1849d | 87 | } |
el18rg | 2:b936aa854de2 | 88 | } |
el18rg | 15:4ed54ff548cf | 89 | void Engine::timer() //starts the timer off |
el18rg | 13:09bc615e6665 | 90 | { |
el18rg | 13:09bc615e6665 | 91 | start = clock(); |
el18rg | 13:09bc615e6665 | 92 | } |
el18rg | 15:4ed54ff548cf | 93 | void Engine::ending(N5110 & lcd, Gamepad & pad) //runs end of the game sequence |
el18rg | 7:b5ef03efe784 | 94 | { |
el18rg | 19:bdfab290446a | 95 | lcd.clear(); //clear lcd |
el18rg | 19:bdfab290446a | 96 | pad.leds_on(); //leds on |
el18rg | 19:bdfab290446a | 97 | pad.tone(500.0,0.5); //play low pad tone |
el18rg | 19:bdfab290446a | 98 | duration = clock() - start; //find duration of timer |
el18rg | 19:bdfab290446a | 99 | final = ((float)duration/CLOCKS_PER_SEC)/2; //change duration into seconds (final) |
el18rg | 19:bdfab290446a | 100 | char buffer[1]; //create buffer |
el18rg | 19:bdfab290446a | 101 | sprintf(buffer,"%.2f",final); //write value to buffer |
el18rg | 19:bdfab290446a | 102 | for (double i = 0; i < 100000; i += 0.1) { //for loop to keep the game stopped |
el18rg | 19:bdfab290446a | 103 | lcd.refresh(); //refresh lcd |
el18rg | 19:bdfab290446a | 104 | lcd.printString(buffer,20,1); //print time |
el18rg | 19:bdfab290446a | 105 | lcd.printString("secs",35,2); //print text |
el18rg | 19:bdfab290446a | 106 | _splat.draw(lcd); //draw splats |
el18rg | 7:b5ef03efe784 | 107 | } |
el18rg | 7:b5ef03efe784 | 108 | } |