Rosie Gillman

Dependencies:   mbed Gamepad2 ELEC2645_Project_el18rg

Dependents:   ELEC2645_Project_el18rg

https://os.mbed.com/media/uploads/el18rg/bug_splat_logo.png

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 https://os.mbed.com/media/uploads/el18rg/buttons.png

Instructions

  1. Turn the gamepad on
  2. Wait for start screen and press start
  3. Move the joystick to control the swatter
  4. Splat the bug as fast as you can
  5. 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

https://os.mbed.com/media/uploads/el18rg/intro_screen1.jpg

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

https://os.mbed.com/media/uploads/el18rg/intro_screen_2.jpg

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

https://os.mbed.com/media/uploads/el18rg/gameplay.jpg

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 https://os.mbed.com/media/uploads/el18rg/end_screen.jpg
Revision:
19:bdfab290446a
Parent:
18:879daedaff42
Child:
22:0b207694a5f7
--- a/Engine/Engine.cpp	Fri May 29 19:26:33 2020 +0000
+++ b/Engine/Engine.cpp	Fri May 29 20:58:32 2020 +0000
@@ -9,83 +9,81 @@
 
 void Engine::init(int swatter_width,int swatter_height,int speed) //initalises the game
 {
-    width = swatter_width;
-    height = swatter_height;
-    _speed = speed;
-    x = GAP;
-    _swatter.init(x,height,width);
-    _bug.init(_speed);
+    width = swatter_width;                          //set width
+    height = swatter_height;                        //set hight
+    _speed = speed;                                 //sets the speed
+    x = GAP;                                        //set x
+    _swatter.init(x,height,width);                  //initialise the swatter with parameters
+    _bug.init(_speed);                              //initilise the bug with speed
 }
 
 void Engine::read_input(Gamepad &pad)               //reads the direction and magnitude inputs
 {
-    _d = pad.get_direction();
-    _mag = pad.get_mag();
+    _d = pad.get_direction();                       //gets the direction
+    _mag = pad.get_mag();                           //gets the magnitude
 }
 
 void Engine::draw(N5110 &lcd)                       //draws the bug and swatter
 {
-    _bug.draw(lcd);
-    _swatter.draw(lcd);
+    _bug.draw(lcd);                                 //draws the bug
+    _swatter.draw(lcd);                             //draws the swatter
 }
 
 void Engine::update(N5110 & lcd, Gamepad & pad)     //updates the game
 {
-    _swatter.update(_d,_mag);
-    _bug.update();
-    check_side_collision(pad);
-    check_swatter_collisions(lcd, pad);
+    _swatter.update(_d,_mag);                       //updates the swatter with direction/magnitude
+    _bug.update();                                  //updates the bug
+    check_side_collision(pad);                      //checks side/bug collision
+    check_swatter_collisions(lcd, pad);             //checks swatter/bug collision
 }
 void Engine::check_side_collision(Gamepad &pad)     //checks if the bug collides with the sides and redirects it
 {
-    srand(time(NULL));
-    Vector2D bug_pos = _bug.get_pos();
-    Vector2D bug_velocity = _bug.get_velocity();
-    if (bug_pos.y <= 1) {
-        bug_pos.y = 1;
-        bug_velocity.y = -bug_velocity.y;
-        bug_velocity.x = rand() % 5 + 0;
-
-        pad.tone(750.0,0.1);
-    } else if (bug_pos.y + 10 >= (HEIGHT-1) ) {
-        bug_pos.y = (HEIGHT-1) - 10;
-        bug_velocity.y = -bug_velocity.y ;
-        bug_velocity.x = rand() % 5 + 0;
-        pad.tone(750.0,0.1);
+    Vector2D bug_pos = _bug.get_pos();              //get the bug position
+    Vector2D bug_velocity = _bug.get_velocity();    //get the bug velocity
+    if (bug_pos.y <= 1) {                           //if the bug touches the top y-axis
+        bug_pos.y = 1;                              //put the bug back inside the boundaries
+        bug_velocity.y = -bug_velocity.y;           //reverse the y velocity
+        bug_velocity.x = rand() % 5 + 0;            //bounce on the x-axis at a 0-5 random velocity
+        pad.tone(750.0,0.1);                        //play a medium pad tone
+    } else if (bug_pos.y + 10 >= (HEIGHT-1) ) {     //if the bug touches the bottom y-axis
+        bug_pos.y = (HEIGHT-1) - 10;                //put the bug back inside the boundaries
+        bug_velocity.y = -bug_velocity.y ;          //reverse the y velocity
+        bug_velocity.x = rand() % 5 + 0;            //bounce on the x-axis at a 0-5 random velocity
+        pad.tone(750.0,0.1);                        //play a medium pad tone
     }
-    if (bug_pos.x <= 1) {
-        bug_pos.x = 1;
-        bug_velocity.x = -bug_velocity.x;
-        bug_velocity.y = rand() % 5 + 0;
-        pad.tone(750.0,0.1);
-    } else if (bug_pos.x + 10 >= (WIDTH-1) ) {
-        bug_pos.x = (WIDTH-1) - 10;
-        bug_velocity.x = -bug_velocity.x ;
-        bug_velocity.y = rand() % 5 + 0;
-        pad.tone(750.0,0.1);
+    if (bug_pos.x <= 1) {                           //if the bug touches the left x-axis
+        bug_pos.x = 1;                              //put the bug back inside the boundaries
+        bug_velocity.x = -bug_velocity.x;           //reverse the x velocity
+        bug_velocity.y = rand() % 5 + 0;            //bounce on the y-axis at a 0-5 random velocity
+        pad.tone(750.0,0.1);                        //play a medium pad tone
+    } else if (bug_pos.x + 10 >= (WIDTH-1) ) {      //if the bug touches the right x-axis
+        bug_pos.x = (WIDTH-1) - 10;                 //put the bug back inside the boundaries
+        bug_velocity.x = -bug_velocity.x ;          //reverse the x velocity
+        bug_velocity.y = rand() % 5 + 0;            //bounce on the y-axis at a 0-5 random velocity
+        pad.tone(750.0,0.1);                        //play a medium pad tone
     }
 
-    _bug.set_velocity(bug_velocity);
-    _bug.set_pos(bug_pos);
+    _bug.set_velocity(bug_velocity);                //set the bug velocity
+    _bug.set_pos(bug_pos);                          //set the bug position
 }
 void Engine::check_swatter_collisions(N5110 & lcd, Gamepad & pad)   //checks if the swatter and bug have collided
 {
-    Vector2D bug_pos = _bug.get_pos();
-    Vector2D p_pos = _swatter.get_pos();
-    Vector2D bug_velocity = _bug.get_velocity();
+    Vector2D bug_pos = _bug.get_pos();                              //get bug position
+    Vector2D p_pos = _swatter.get_pos();                            //get swatter position
+    Vector2D bug_velocity = _bug.get_velocity();                    //get bug velocity
     
-    if ((bug_pos.x <= (p_pos.x +5)) && (bug_pos.x +5 >= p_pos.x)) {
+    if ((bug_pos.x <= (p_pos.x +5)) && (bug_pos.x +5 >= p_pos.x)) { //check for x-axis collision
         collisionX=true;
     } else {
         collisionX=false;
     }
-    if ((bug_pos.y <= (p_pos.y+5)) && (bug_pos.y+5 >= p_pos.y )) {
+    if ((bug_pos.y <= (p_pos.y+5)) && (bug_pos.y+5 >= p_pos.y )) {  //check for y-axis collision
         collisionY=true;
     } else {
         collisionY=false;
     }
-    if (collisionX && collisionY) {
-        ending(lcd, pad);
+    if (collisionX && collisionY) {                                 //if there's a x and y-axis collision
+        ending(lcd, pad);                                           //run ending
     }
 }
 void Engine::timer()                                //starts the timer off
@@ -94,17 +92,17 @@
 }
 void Engine::ending(N5110 & lcd, Gamepad & pad)     //runs end of the game sequence
 {
-    lcd.clear();
-    pad.leds_on();
-    pad.tone(500.0,0.5);
-    duration = clock() - start;
-    final = ((float)duration/CLOCKS_PER_SEC)/2;
-    char buffer[1];
-    sprintf(buffer,"%.2f",final);
-    for (double i = 0; i < 100000; i += 0.1) {
-        lcd.refresh();        
-        lcd.printString(buffer,20,1);
-        lcd.printString("secs",35,2);
-        _splat.draw(lcd);
+    lcd.clear();                                    //clear lcd
+    pad.leds_on();                                  //leds on
+    pad.tone(500.0,0.5);                            //play low pad tone
+    duration = clock() - start;                     //find duration of timer
+    final = ((float)duration/CLOCKS_PER_SEC)/2;     //change duration into seconds (final)
+    char buffer[1];                                 //create buffer
+    sprintf(buffer,"%.2f",final);                   //write value to buffer
+    for (double i = 0; i < 100000; i += 0.1) {      //for loop to keep the game stopped
+        lcd.refresh();                              //refresh lcd
+        lcd.printString(buffer,20,1);               //print time
+        lcd.printString("secs",35,2);               //print text
+        _splat.draw(lcd);                           //draw splats
     }
 }
\ No newline at end of file