Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
ModeA/ModeA.h@22:eadcb3d8fec0, 2019-05-09 (annotated)
- Committer:
- el17mtu
- Date:
- Thu May 09 04:45:21 2019 +0000
- Revision:
- 22:eadcb3d8fec0
- Parent:
- 20:d9987ed262c7
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17mtu | 0:15b74f0f8c7f | 1 | #ifndef MODEA_H |
el17mtu | 0:15b74f0f8c7f | 2 | #define MODEA_H |
el17mtu | 0:15b74f0f8c7f | 3 | |
el17mtu | 0:15b74f0f8c7f | 4 | #include "mbed.h" |
el17mtu | 0:15b74f0f8c7f | 5 | #include "main.h" |
el17mtu | 0:15b74f0f8c7f | 6 | #include "N5110.h" |
el17mtu | 0:15b74f0f8c7f | 7 | #include "Gamepad.h" |
el17mtu | 0:15b74f0f8c7f | 8 | |
el17mtu | 20:d9987ed262c7 | 9 | /** ModeA class |
el17mtu | 20:d9987ed262c7 | 10 | |
el17mtu | 20:d9987ed262c7 | 11 | @brief Class displaying instructions |
el17mtu | 20:d9987ed262c7 | 12 | @author Maria Ungureanu |
el17mtu | 20:d9987ed262c7 | 13 | @date May 2019 |
el17mtu | 20:d9987ed262c7 | 14 | |
el17mtu | 20:d9987ed262c7 | 15 | */ |
el17mtu | 0:15b74f0f8c7f | 16 | |
el17mtu | 0:15b74f0f8c7f | 17 | class ModeA |
el17mtu | 0:15b74f0f8c7f | 18 | { |
el17mtu | 0:15b74f0f8c7f | 19 | |
el17mtu | 0:15b74f0f8c7f | 20 | public: |
el17mtu | 20:d9987ed262c7 | 21 | /** Constructor */ |
el17mtu | 0:15b74f0f8c7f | 22 | ModeA(); |
el17mtu | 20:d9987ed262c7 | 23 | /** Deconstructor */ |
el17mtu | 0:15b74f0f8c7f | 24 | ~ModeA(); |
el17mtu | 0:15b74f0f8c7f | 25 | |
el17mtu | 20:d9987ed262c7 | 26 | |
el17mtu | 20:d9987ed262c7 | 27 | /** |
el17mtu | 20:d9987ed262c7 | 28 | * @brief Variables are defined |
el17mtu | 20:d9987ed262c7 | 29 | * @param x_position @details x coordinate of square |
el17mtu | 20:d9987ed262c7 | 30 | * @param y_position @details y coordinate of square |
el17mtu | 20:d9987ed262c7 | 31 | * @param seped @details speed of the square |
el17mtu | 20:d9987ed262c7 | 32 | * @param gravity @details gravitational force |
el17mtu | 20:d9987ed262c7 | 33 | * @param screen_width @details x coordinate of the bar |
el17mtu | 20:d9987ed262c7 | 34 | * @param bar_width @details width of the bar |
el17mtu | 20:d9987ed262c7 | 35 | * @param bar_speed @details speed of the bar |
el17mtu | 20:d9987ed262c7 | 36 | * @param score @details score |
el17mtu | 22:eadcb3d8fec0 | 37 | * @param size_top @details random length |
el17mtu | 22:eadcb3d8fec0 | 38 | * @param size_bottom @details random length |
el17mtu | 20:d9987ed262c7 | 39 | */ |
el17mtu | 0:15b74f0f8c7f | 40 | void initialise(N5110 &lcd); |
el17mtu | 20:d9987ed262c7 | 41 | |
el17mtu | 20:d9987ed262c7 | 42 | /** |
el17mtu | 20:d9987ed262c7 | 43 | * @brief Game function |
el17mtu | 20:d9987ed262c7 | 44 | * @brief It draws the square, bars, it increments the score, |
el17mtu | 20:d9987ed262c7 | 45 | * @brief moves the square and the bars. |
el17mtu | 20:d9987ed262c7 | 46 | * @brief When the square and bar collide or the square is at |
el17mtu | 20:d9987ed262c7 | 47 | * @brief the bottom of the screen it ends the game |
el17mtu | 20:d9987ed262c7 | 48 | @code |
el17mtu | 20:d9987ed262c7 | 49 | //game function |
el17mtu | 20:d9987ed262c7 | 50 | void ModeA::Game(N5110 &lcd) |
el17mtu | 20:d9987ed262c7 | 51 | { |
el17mtu | 20:d9987ed262c7 | 52 | char buffer[14]; |
el17mtu | 20:d9987ed262c7 | 53 | sprintf(buffer,"%2d",score); //print score on the screen |
el17mtu | 20:d9987ed262c7 | 54 | lcd.printString(buffer,70,0); // print the score at the right end of screen |
el17mtu | 20:d9987ed262c7 | 55 | lcd.drawRect(x_position, y_position,6,6,FILL_BLACK); //draw the square |
el17mtu | 20:d9987ed262c7 | 56 | lcd.refresh(); |
el17mtu | 20:d9987ed262c7 | 57 | |
el17mtu | 20:d9987ed262c7 | 58 | // multiply gravity with 5 for a resonable speed |
el17mtu | 20:d9987ed262c7 | 59 | if ( pad.check_event(Gamepad::Y_PRESSED) == true) { |
el17mtu | 20:d9987ed262c7 | 60 | speed = speed - gravity*5; |
el17mtu | 20:d9987ed262c7 | 61 | } |
el17mtu | 20:d9987ed262c7 | 62 | |
el17mtu | 20:d9987ed262c7 | 63 | //if the square drops to the bottom of screen- GAME OVER |
el17mtu | 20:d9987ed262c7 | 64 | //redifine all parameters to 0, clear screen |
el17mtu | 20:d9987ed262c7 | 65 | //press back to play again |
el17mtu | 20:d9987ed262c7 | 66 | if (y_position > 44) { |
el17mtu | 20:d9987ed262c7 | 67 | lcd.clear(); |
el17mtu | 20:d9987ed262c7 | 68 | lcd.printString("GAME OVER",6,2); |
el17mtu | 20:d9987ed262c7 | 69 | lcd.printString("Press BACK",6,4); |
el17mtu | 20:d9987ed262c7 | 70 | pad.tone(2000.0,0.3); //music |
el17mtu | 20:d9987ed262c7 | 71 | lcd.refresh(); |
el17mtu | 20:d9987ed262c7 | 72 | bar_speed = 0; |
el17mtu | 20:d9987ed262c7 | 73 | speed = 0; |
el17mtu | 20:d9987ed262c7 | 74 | gravity = 0; |
el17mtu | 20:d9987ed262c7 | 75 | y_position = 48; |
el17mtu | 20:d9987ed262c7 | 76 | bar_width = 0; |
el17mtu | 20:d9987ed262c7 | 77 | size_top = 0; |
el17mtu | 20:d9987ed262c7 | 78 | size_bottom = 0; |
el17mtu | 20:d9987ed262c7 | 79 | wait(0.5); |
el17mtu | 20:d9987ed262c7 | 80 | } |
el17mtu | 20:d9987ed262c7 | 81 | |
el17mtu | 20:d9987ed262c7 | 82 | //if square goes to top part of screen, goes back down |
el17mtu | 20:d9987ed262c7 | 83 | if (y_position < 0) { |
el17mtu | 20:d9987ed262c7 | 84 | y_position = 0; |
el17mtu | 20:d9987ed262c7 | 85 | speed = 0; |
el17mtu | 20:d9987ed262c7 | 86 | } |
el17mtu | 20:d9987ed262c7 | 87 | |
el17mtu | 20:d9987ed262c7 | 88 | speed = speed + gravity; //increment the speed by the value of gravity |
el17mtu | 20:d9987ed262c7 | 89 | y_position = y_position + speed; // as the square moves up, the position changes |
el17mtu | 20:d9987ed262c7 | 90 | wait(0.1); |
el17mtu | 20:d9987ed262c7 | 91 | |
el17mtu | 20:d9987ed262c7 | 92 | //draw the 2 bars |
el17mtu | 20:d9987ed262c7 | 93 | //the top one starts at y coordinate 0 and it has a height of size_top (random) |
el17mtu | 20:d9987ed262c7 | 94 | //the bottom one starts size_bottom pixels above the edge of the screen |
el17mtu | 20:d9987ed262c7 | 95 | lcd.drawRect(screen_width,0,bar_width,size_top,FILL_BLACK); |
el17mtu | 20:d9987ed262c7 | 96 | lcd.drawRect(screen_width,48-size_bottom,bar_width,size_bottom,FILL_BLACK); |
el17mtu | 20:d9987ed262c7 | 97 | lcd.refresh(); |
el17mtu | 20:d9987ed262c7 | 98 | |
el17mtu | 20:d9987ed262c7 | 99 | // if the bar and square have the same x coordinates and |
el17mtu | 20:d9987ed262c7 | 100 | // their y coordinates don't intersect, score increases by 1 |
el17mtu | 20:d9987ed262c7 | 101 | if ((screen_width == x_position) && (size_top < y_position)) { |
el17mtu | 20:d9987ed262c7 | 102 | score = score + 1; |
el17mtu | 20:d9987ed262c7 | 103 | lcd.clear(); |
el17mtu | 20:d9987ed262c7 | 104 | sprintf(buffer,"%2d",score); //update buffer |
el17mtu | 20:d9987ed262c7 | 105 | lcd.printString("Reset",15,2); |
el17mtu | 20:d9987ed262c7 | 106 | lcd.refresh(); |
el17mtu | 20:d9987ed262c7 | 107 | wait(1.0); |
el17mtu | 20:d9987ed262c7 | 108 | } |
el17mtu | 20:d9987ed262c7 | 109 | |
el17mtu | 20:d9987ed262c7 | 110 | //if X is pressed random hights of the bars are genetrated |
el17mtu | 20:d9987ed262c7 | 111 | if ( pad.check_event(Gamepad::X_PRESSED) == true) { |
el17mtu | 20:d9987ed262c7 | 112 | srand(time(NULL)); |
el17mtu | 20:d9987ed262c7 | 113 | size_top = rand() % 15; |
el17mtu | 20:d9987ed262c7 | 114 | srand(time(NULL)); |
el17mtu | 20:d9987ed262c7 | 115 | size_bottom = rand() % 15; |
el17mtu | 20:d9987ed262c7 | 116 | } |
el17mtu | 20:d9987ed262c7 | 117 | |
el17mtu | 20:d9987ed262c7 | 118 | //if the bar and ball collide, Game Over |
el17mtu | 20:d9987ed262c7 | 119 | //redifine all parameters to 0, clear screen |
el17mtu | 20:d9987ed262c7 | 120 | //press back to play again |
el17mtu | 20:d9987ed262c7 | 121 | if ((screen_width == x_position)&& (size_top > y_position)) { |
el17mtu | 20:d9987ed262c7 | 122 | lcd.clear(); |
el17mtu | 20:d9987ed262c7 | 123 | lcd.printString("GAME OVER",6,2); |
el17mtu | 20:d9987ed262c7 | 124 | lcd.printString("Press BACK",6,4); |
el17mtu | 20:d9987ed262c7 | 125 | pad.tone(2000.0,0.3); |
el17mtu | 20:d9987ed262c7 | 126 | lcd.refresh(); |
el17mtu | 20:d9987ed262c7 | 127 | bar_speed = 0; |
el17mtu | 20:d9987ed262c7 | 128 | speed = 0; |
el17mtu | 20:d9987ed262c7 | 129 | gravity = 0; |
el17mtu | 20:d9987ed262c7 | 130 | y_position = 48; |
el17mtu | 20:d9987ed262c7 | 131 | bar_width = 0; |
el17mtu | 20:d9987ed262c7 | 132 | size_top = 0; |
el17mtu | 20:d9987ed262c7 | 133 | size_bottom = 0; |
el17mtu | 20:d9987ed262c7 | 134 | wait(0.5); |
el17mtu | 20:d9987ed262c7 | 135 | } |
el17mtu | 20:d9987ed262c7 | 136 | |
el17mtu | 20:d9987ed262c7 | 137 | //as the bar travels to the left, its position changes |
el17mtu | 20:d9987ed262c7 | 138 | screen_width = screen_width - bar_speed; |
el17mtu | 20:d9987ed262c7 | 139 | wait(0.1); |
el17mtu | 20:d9987ed262c7 | 140 | } |
el17mtu | 20:d9987ed262c7 | 141 | @endcode |
el17mtu | 20:d9987ed262c7 | 142 | */ |
el17mtu | 18:59befe1eaa56 | 143 | void Game(N5110 &lcd); |
el17mtu | 0:15b74f0f8c7f | 144 | |
el17mtu | 22:eadcb3d8fec0 | 145 | //variables that are privatly defined |
el17mtu | 0:15b74f0f8c7f | 146 | private: |
el17mtu | 20:d9987ed262c7 | 147 | int x_position; |
el17mtu | 20:d9987ed262c7 | 148 | int y_position; |
el17mtu | 5:1c56acecc96e | 149 | |
el17mtu | 20:d9987ed262c7 | 150 | int gravity; |
el17mtu | 20:d9987ed262c7 | 151 | int screen_width; |
el17mtu | 20:d9987ed262c7 | 152 | int bar_width; |
el17mtu | 20:d9987ed262c7 | 153 | int bar_speed; |
el17mtu | 20:d9987ed262c7 | 154 | int speed; |
el17mtu | 20:d9987ed262c7 | 155 | int score; |
el17mtu | 3:8e6950aeec8a | 156 | |
el17mtu | 1:44f4594eacac | 157 | int size_bottom; |
el17mtu | 3:8e6950aeec8a | 158 | int size_top; |
el17mtu | 20:d9987ed262c7 | 159 | |
el17mtu | 20:d9987ed262c7 | 160 | |
el17mtu | 0:15b74f0f8c7f | 161 | }; |
el17mtu | 0:15b74f0f8c7f | 162 | #endif |