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@17:a50b273253ef, 2020-05-29 (annotated)
- Committer:
- el18rg
- Date:
- Fri May 29 18:31:28 2020 +0000
- Revision:
- 17:a50b273253ef
- Parent:
- 16:33d8b58a1a65
- Child:
- 18:879daedaff42
Timer working
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 | 11:93da75c1849d | 12 | width = swatter_width; |
el18rg | 11:93da75c1849d | 13 | height = swatter_height; |
el18rg | 2:b936aa854de2 | 14 | _speed = speed; |
el18rg | 2:b936aa854de2 | 15 | x = GAP; |
el18rg | 10:b6e45e4acde7 | 16 | _swatter.init(x,height,width); |
el18rg | 9:e7dce4de0910 | 17 | _bug.init(_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 | 2:b936aa854de2 | 22 | _d = pad.get_direction(); |
el18rg | 2:b936aa854de2 | 23 | _mag = pad.get_mag(); |
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 | 9:e7dce4de0910 | 28 | _bug.draw(lcd); |
el18rg | 10:b6e45e4acde7 | 29 | _swatter.draw(lcd); |
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 | 10:b6e45e4acde7 | 34 | _swatter.update(_d,_mag); |
el18rg | 9:e7dce4de0910 | 35 | _bug.update(); |
el18rg | 14:9b4a219dd91e | 36 | check_side_collision(pad); |
el18rg | 11:93da75c1849d | 37 | check_swatter_collisions(lcd, pad); |
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 | 13:09bc615e6665 | 41 | srand(time(NULL)); |
el18rg | 9:e7dce4de0910 | 42 | Vector2D bug_pos = _bug.get_pos(); |
el18rg | 9:e7dce4de0910 | 43 | Vector2D bug_velocity = _bug.get_velocity(); |
el18rg | 11:93da75c1849d | 44 | if (bug_pos.y <= 1) { |
el18rg | 11:93da75c1849d | 45 | bug_pos.y = 1; |
el18rg | 9:e7dce4de0910 | 46 | bug_velocity.y = -bug_velocity.y; |
el18rg | 13:09bc615e6665 | 47 | bug_velocity.x = rand() % 5 + 0; |
el18rg | 11:93da75c1849d | 48 | |
el18rg | 2:b936aa854de2 | 49 | pad.tone(750.0,0.1); |
el18rg | 11:93da75c1849d | 50 | } else if (bug_pos.y + 10 >= (HEIGHT-1) ) { |
el18rg | 9:e7dce4de0910 | 51 | bug_pos.y = (HEIGHT-1) - 10; |
el18rg | 9:e7dce4de0910 | 52 | bug_velocity.y = -bug_velocity.y ; |
el18rg | 13:09bc615e6665 | 53 | bug_velocity.x = rand() % 5 + 0; |
el18rg | 4:5cc1b81999a9 | 54 | pad.tone(750.0,0.1); |
el18rg | 4:5cc1b81999a9 | 55 | } |
el18rg | 11:93da75c1849d | 56 | if (bug_pos.x <= 1) { |
el18rg | 11:93da75c1849d | 57 | bug_pos.x = 1; |
el18rg | 9:e7dce4de0910 | 58 | bug_velocity.x = -bug_velocity.x; |
el18rg | 13:09bc615e6665 | 59 | bug_velocity.y = rand() % 5 + 0; |
el18rg | 4:5cc1b81999a9 | 60 | pad.tone(750.0,0.1); |
el18rg | 11:93da75c1849d | 61 | } else if (bug_pos.x + 10 >= (WIDTH-1) ) { |
el18rg | 9:e7dce4de0910 | 62 | bug_pos.x = (WIDTH-1) - 10; |
el18rg | 9:e7dce4de0910 | 63 | bug_velocity.x = -bug_velocity.x ; |
el18rg | 13:09bc615e6665 | 64 | bug_velocity.y = rand() % 5 + 0; |
el18rg | 2:b936aa854de2 | 65 | pad.tone(750.0,0.1); |
el18rg | 2:b936aa854de2 | 66 | } |
el18rg | 2:b936aa854de2 | 67 | |
el18rg | 9:e7dce4de0910 | 68 | _bug.set_velocity(bug_velocity); |
el18rg | 9:e7dce4de0910 | 69 | _bug.set_pos(bug_pos); |
el18rg | 2:b936aa854de2 | 70 | } |
el18rg | 15:4ed54ff548cf | 71 | void Engine::check_swatter_collisions(N5110 & lcd, Gamepad & pad) //checks if the swatter and bug have collided |
el18rg | 2:b936aa854de2 | 72 | { |
el18rg | 9:e7dce4de0910 | 73 | Vector2D bug_pos = _bug.get_pos(); |
el18rg | 10:b6e45e4acde7 | 74 | Vector2D p_pos = _swatter.get_pos(); |
el18rg | 9:e7dce4de0910 | 75 | Vector2D bug_velocity = _bug.get_velocity(); |
el18rg | 13:09bc615e6665 | 76 | |
el18rg | 13:09bc615e6665 | 77 | if ((bug_pos.x <= (p_pos.x +5)) && (bug_pos.x +5 >= p_pos.x)) { |
el18rg | 3:2f4a7787beb3 | 78 | collisionX=true; |
el18rg | 11:93da75c1849d | 79 | } else { |
el18rg | 3:2f4a7787beb3 | 80 | collisionX=false; |
el18rg | 3:2f4a7787beb3 | 81 | } |
el18rg | 11:93da75c1849d | 82 | if ((bug_pos.y <= (p_pos.y+5)) && (bug_pos.y+5 >= p_pos.y )) { |
el18rg | 3:2f4a7787beb3 | 83 | collisionY=true; |
el18rg | 11:93da75c1849d | 84 | } else { |
el18rg | 3:2f4a7787beb3 | 85 | collisionY=false; |
el18rg | 2:b936aa854de2 | 86 | } |
el18rg | 11:93da75c1849d | 87 | if (collisionX && collisionY) { |
el18rg | 7:b5ef03efe784 | 88 | ending(lcd, pad); |
el18rg | 11:93da75c1849d | 89 | } |
el18rg | 2:b936aa854de2 | 90 | } |
el18rg | 15:4ed54ff548cf | 91 | void Engine::timer() //starts the timer off |
el18rg | 13:09bc615e6665 | 92 | { |
el18rg | 13:09bc615e6665 | 93 | start = clock(); |
el18rg | 13:09bc615e6665 | 94 | } |
el18rg | 15:4ed54ff548cf | 95 | void Engine::ending(N5110 & lcd, Gamepad & pad) //runs end of the game sequence |
el18rg | 7:b5ef03efe784 | 96 | { |
el18rg | 7:b5ef03efe784 | 97 | lcd.clear(); |
el18rg | 7:b5ef03efe784 | 98 | pad.leds_on(); |
el18rg | 17:a50b273253ef | 99 | pad.tone(500.0,0.5); |
el18rg | 16:33d8b58a1a65 | 100 | duration = clock() - start; |
el18rg | 16:33d8b58a1a65 | 101 | final = ((float)duration/CLOCKS_PER_SEC)/2; |
el18rg | 13:09bc615e6665 | 102 | char buffer[1]; |
el18rg | 16:33d8b58a1a65 | 103 | sprintf(buffer,"%.2f",final); |
el18rg | 11:93da75c1849d | 104 | for (double i = 0; i < 100000; i += 0.1) { |
el18rg | 11:93da75c1849d | 105 | lcd.refresh(); |
el18rg | 16:33d8b58a1a65 | 106 | lcd.printString(buffer,23,2); |
el18rg | 16:33d8b58a1a65 | 107 | lcd.printString("s",50,2); |
el18rg | 11:93da75c1849d | 108 | _splat.draw(lcd); |
el18rg | 7:b5ef03efe784 | 109 | } |
el18rg | 7:b5ef03efe784 | 110 | } |