ELEC2645 (2018/19) / Mbed 2 deprecated el17mtu_

Dependencies:   mbed

Committer:
el17mtu
Date:
Thu May 09 03:53:43 2019 +0000
Revision:
20:d9987ed262c7
Parent:
19:5e3ee84d9233
final with comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17mtu 0:15b74f0f8c7f 1 #include "ModeA.h"
el17mtu 0:15b74f0f8c7f 2
el17mtu 18:59befe1eaa56 3 ModeA::ModeA() //constructor
el17mtu 0:15b74f0f8c7f 4 {
el17mtu 0:15b74f0f8c7f 5
el17mtu 0:15b74f0f8c7f 6 }
el17mtu 0:15b74f0f8c7f 7
el17mtu 18:59befe1eaa56 8 ModeA::~ModeA() //destructor
el17mtu 0:15b74f0f8c7f 9 {
el17mtu 0:15b74f0f8c7f 10
el17mtu 0:15b74f0f8c7f 11 }
el17mtu 0:15b74f0f8c7f 12
el17mtu 19:5e3ee84d9233 13 //initialise all values
el17mtu 14:558c85380f84 14 void ModeA::initialise(N5110 &lcd)
el17mtu 14:558c85380f84 15 {
el17mtu 18:59befe1eaa56 16 x_position = 12; //x coordinate of square
el17mtu 18:59befe1eaa56 17 y_position = 18; //y coordinate of square
el17mtu 19:5e3ee84d9233 18 speed = 1; // speed of square
el17mtu 19:5e3ee84d9233 19 gravity = 1; //gravitational force
el17mtu 20:d9987ed262c7 20 screen_width = 70; // pixel at which bar begins (x coordinate)
el17mtu 18:59befe1eaa56 21 bar_width = 10; // width of the bar
el17mtu 18:59befe1eaa56 22 bar_speed = 2; // spped of the bar
el17mtu 18:59befe1eaa56 23 score = 0; // score
el17mtu 14:558c85380f84 24 srand(time(NULL));
el17mtu 18:59befe1eaa56 25 size_top = rand() % 15; //15 random lengths of the top bar
el17mtu 14:558c85380f84 26 srand(time(NULL));
el17mtu 18:59befe1eaa56 27 size_bottom = rand() % 15; //15 random length of the bottom bar
el17mtu 14:558c85380f84 28 }
el17mtu 14:558c85380f84 29
el17mtu 19:5e3ee84d9233 30 //game function
el17mtu 18:59befe1eaa56 31 void ModeA::Game(N5110 &lcd)
el17mtu 0:15b74f0f8c7f 32 {
el17mtu 14:558c85380f84 33 char buffer[14];
el17mtu 19:5e3ee84d9233 34 sprintf(buffer,"%2d",score); //print score on the screen
el17mtu 19:5e3ee84d9233 35 lcd.printString(buffer,70,0); // print the score at the right end of screen
el17mtu 19:5e3ee84d9233 36 lcd.drawRect(x_position, y_position,6,6,FILL_BLACK); //draw the square
el17mtu 14:558c85380f84 37 lcd.refresh();
el17mtu 14:558c85380f84 38
el17mtu 19:5e3ee84d9233 39 // multiply gravity with 5 for a resonable speed
el17mtu 14:558c85380f84 40 if ( pad.check_event(Gamepad::Y_PRESSED) == true) {
el17mtu 14:558c85380f84 41 speed = speed - gravity*5;
el17mtu 14:558c85380f84 42 }
el17mtu 14:558c85380f84 43
el17mtu 19:5e3ee84d9233 44 //if the square drops to the bottom of screen- GAME OVER
el17mtu 19:5e3ee84d9233 45 //redifine all parameters to 0, clear screen
el17mtu 19:5e3ee84d9233 46 //press back to play again
el17mtu 14:558c85380f84 47 if (y_position > 44) {
el17mtu 14:558c85380f84 48 lcd.clear();
el17mtu 14:558c85380f84 49 lcd.printString("GAME OVER",6,2);
el17mtu 9:571f92b0211b 50 lcd.printString("Press BACK",6,4);
el17mtu 19:5e3ee84d9233 51 pad.tone(2000.0,0.3); //music
el17mtu 14:558c85380f84 52 lcd.refresh();
el17mtu 9:571f92b0211b 53 bar_speed = 0;
el17mtu 14:558c85380f84 54 speed = 0;
el17mtu 9:571f92b0211b 55 gravity = 0;
el17mtu 9:571f92b0211b 56 y_position = 48;
el17mtu 14:558c85380f84 57 bar_width = 0;
el17mtu 14:558c85380f84 58 size_top = 0;
el17mtu 9:571f92b0211b 59 size_bottom = 0;
el17mtu 14:558c85380f84 60 wait(0.5);
el17mtu 14:558c85380f84 61 }
el17mtu 14:558c85380f84 62
el17mtu 19:5e3ee84d9233 63 //if square goes to top part of screen, goes back down
el17mtu 14:558c85380f84 64 if (y_position < 0) {
el17mtu 14:558c85380f84 65 y_position = 0;
el17mtu 14:558c85380f84 66 speed = 0;
el17mtu 14:558c85380f84 67 }
el17mtu 14:558c85380f84 68
el17mtu 19:5e3ee84d9233 69 speed = speed + gravity; //increment the speed by the value of gravity
el17mtu 20:d9987ed262c7 70 y_position = y_position + speed; // as the square moves up, the position changes
el17mtu 14:558c85380f84 71 wait(0.1);
el17mtu 14:558c85380f84 72
el17mtu 19:5e3ee84d9233 73 //draw the 2 bars
el17mtu 19:5e3ee84d9233 74 //the top one starts at y coordinate 0 and it has a height of size_top (random)
el17mtu 19:5e3ee84d9233 75 //the bottom one starts size_bottom pixels above the edge of the screen
el17mtu 14:558c85380f84 76 lcd.drawRect(screen_width,0,bar_width,size_top,FILL_BLACK);
el17mtu 14:558c85380f84 77 lcd.drawRect(screen_width,48-size_bottom,bar_width,size_bottom,FILL_BLACK);
el17mtu 14:558c85380f84 78 lcd.refresh();
el17mtu 14:558c85380f84 79
el17mtu 20:d9987ed262c7 80 // if the bar and square have the same x coordinates and
el17mtu 19:5e3ee84d9233 81 // their y coordinates don't intersect, score increases by 1
el17mtu 14:558c85380f84 82 if ((screen_width == x_position) && (size_top < y_position)) {
el17mtu 14:558c85380f84 83 score = score + 1;
el17mtu 14:558c85380f84 84 lcd.clear();
el17mtu 19:5e3ee84d9233 85 sprintf(buffer,"%2d",score); //update buffer
el17mtu 16:7b179005cd91 86 lcd.printString("Reset",15,2);
el17mtu 16:7b179005cd91 87 lcd.refresh();
el17mtu 16:7b179005cd91 88 wait(1.0);
el17mtu 19:5e3ee84d9233 89 }
el17mtu 14:558c85380f84 90
el17mtu 20:d9987ed262c7 91 //if X is pressed random hights of the bars are genetrated
el17mtu 14:558c85380f84 92 if ( pad.check_event(Gamepad::X_PRESSED) == true) {
el17mtu 14:558c85380f84 93 srand(time(NULL));
el17mtu 14:558c85380f84 94 size_top = rand() % 15;
el17mtu 14:558c85380f84 95 srand(time(NULL));
el17mtu 14:558c85380f84 96 size_bottom = rand() % 15;
el17mtu 0:15b74f0f8c7f 97 }
el17mtu 14:558c85380f84 98
el17mtu 19:5e3ee84d9233 99 //if the bar and ball collide, Game Over
el17mtu 20:d9987ed262c7 100 //redifine all parameters to 0, clear screen
el17mtu 20:d9987ed262c7 101 //press back to play again
el17mtu 14:558c85380f84 102 if ((screen_width == x_position)&& (size_top > y_position)) {
el17mtu 14:558c85380f84 103 lcd.clear();
el17mtu 14:558c85380f84 104 lcd.printString("GAME OVER",6,2);
el17mtu 14:558c85380f84 105 lcd.printString("Press BACK",6,4);
el17mtu 14:558c85380f84 106 pad.tone(2000.0,0.3);
el17mtu 14:558c85380f84 107 lcd.refresh();
el17mtu 14:558c85380f84 108 bar_speed = 0;
el17mtu 14:558c85380f84 109 speed = 0;
el17mtu 14:558c85380f84 110 gravity = 0;
el17mtu 14:558c85380f84 111 y_position = 48;
el17mtu 14:558c85380f84 112 bar_width = 0;
el17mtu 14:558c85380f84 113 size_top = 0;
el17mtu 14:558c85380f84 114 size_bottom = 0;
el17mtu 14:558c85380f84 115 wait(0.5);
el17mtu 14:558c85380f84 116 }
el17mtu 19:5e3ee84d9233 117
el17mtu 20:d9987ed262c7 118 //as the bar travels to the left, its position changes
el17mtu 14:558c85380f84 119 screen_width = screen_width - bar_speed;
el17mtu 14:558c85380f84 120 wait(0.1);
el17mtu 14:558c85380f84 121 }
el17mtu 14:558c85380f84 122
el17mtu 14:558c85380f84 123
el17mtu 14:558c85380f84 124
el17mtu 14:558c85380f84 125
el17mtu 14:558c85380f84 126