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.cpp@10:1c3bdbf2fe9f, 2019-05-08 (annotated)
- Committer:
- el17mtu
- Date:
- Wed May 08 23:29:53 2019 +0000
- Revision:
- 10:1c3bdbf2fe9f
- Parent:
- 9:571f92b0211b
- Child:
- 11:e29b173ccb27
version with comments
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17mtu | 0:15b74f0f8c7f | 1 | #include "ModeA.h" |
| el17mtu | 0:15b74f0f8c7f | 2 | |
| el17mtu | 0:15b74f0f8c7f | 3 | ModeA::ModeA() |
| el17mtu | 0:15b74f0f8c7f | 4 | { |
| el17mtu | 0:15b74f0f8c7f | 5 | |
| el17mtu | 0:15b74f0f8c7f | 6 | } |
| el17mtu | 0:15b74f0f8c7f | 7 | |
| el17mtu | 0:15b74f0f8c7f | 8 | ModeA::~ModeA() |
| el17mtu | 0:15b74f0f8c7f | 9 | { |
| el17mtu | 0:15b74f0f8c7f | 10 | |
| el17mtu | 0:15b74f0f8c7f | 11 | } |
| el17mtu | 0:15b74f0f8c7f | 12 | |
| el17mtu | 0:15b74f0f8c7f | 13 | void ModeA::screen(N5110 &lcd) |
| el17mtu | 0:15b74f0f8c7f | 14 | { |
| el17mtu | 0:15b74f0f8c7f | 15 | |
| el17mtu | 0:15b74f0f8c7f | 16 | lcd.clear(); |
| el17mtu | 0:15b74f0f8c7f | 17 | lcd.printString("GAME",0,0); |
| el17mtu | 0:15b74f0f8c7f | 18 | lcd.printString("Back:Rules",0,2); |
| el17mtu | 0:15b74f0f8c7f | 19 | |
| el17mtu | 0:15b74f0f8c7f | 20 | }; |
| el17mtu | 0:15b74f0f8c7f | 21 | |
| el17mtu | 0:15b74f0f8c7f | 22 | void ModeA::initialise(N5110 &lcd) |
| el17mtu | 0:15b74f0f8c7f | 23 | { |
| el17mtu | 10:1c3bdbf2fe9f | 24 | //variables for the square |
| el17mtu | 10:1c3bdbf2fe9f | 25 | x_position = 12; |
| el17mtu | 10:1c3bdbf2fe9f | 26 | y_position = 18; |
| el17mtu | 10:1c3bdbf2fe9f | 27 | speed = 0; |
| el17mtu | 0:15b74f0f8c7f | 28 | gravity = 1; |
| el17mtu | 10:1c3bdbf2fe9f | 29 | //variables for the bar |
| el17mtu | 8:d9b602e58126 | 30 | screen_width = 80; |
| el17mtu | 1:44f4594eacac | 31 | bar_width = 10; |
| el17mtu | 4:a81f05c8806a | 32 | bar_speed = 2; |
| el17mtu | 3:8e6950aeec8a | 33 | |
| el17mtu | 10:1c3bdbf2fe9f | 34 | //score = 1; |
| el17mtu | 10:1c3bdbf2fe9f | 35 | |
| el17mtu | 10:1c3bdbf2fe9f | 36 | //generating random sizes of the bar |
| el17mtu | 10:1c3bdbf2fe9f | 37 | srand(time(NULL)); |
| el17mtu | 10:1c3bdbf2fe9f | 38 | size_top = rand() % 15; |
| el17mtu | 10:1c3bdbf2fe9f | 39 | srand(time(NULL)); |
| el17mtu | 10:1c3bdbf2fe9f | 40 | size_bottom = rand() % 15; |
| el17mtu | 3:8e6950aeec8a | 41 | |
| el17mtu | 9:571f92b0211b | 42 | |
| el17mtu | 9:571f92b0211b | 43 | } |
| el17mtu | 0:15b74f0f8c7f | 44 | |
| el17mtu | 9:571f92b0211b | 45 | void ModeA::Bird(N5110 &lcd) |
| el17mtu | 0:15b74f0f8c7f | 46 | |
| el17mtu | 0:15b74f0f8c7f | 47 | { |
| el17mtu | 0:15b74f0f8c7f | 48 | |
| el17mtu | 0:15b74f0f8c7f | 49 | |
| el17mtu | 0:15b74f0f8c7f | 50 | lcd.drawRect(x_position, y_position,6,6,FILL_BLACK); |
| el17mtu | 3:8e6950aeec8a | 51 | //lcd.drawRect(screen_width,0,bar_width,size_top,FILL_BLACK); |
| el17mtu | 3:8e6950aeec8a | 52 | //lcd.drawRect(screen_width,48-size_bottom,bar_width,size_bottom,FILL_BLACK); |
| el17mtu | 0:15b74f0f8c7f | 53 | lcd.refresh(); |
| el17mtu | 0:15b74f0f8c7f | 54 | |
| el17mtu | 0:15b74f0f8c7f | 55 | |
| el17mtu | 0:15b74f0f8c7f | 56 | |
| el17mtu | 0:15b74f0f8c7f | 57 | if ( pad.check_event(Gamepad::Y_PRESSED) == true) { |
| el17mtu | 8:d9b602e58126 | 58 | |
| el17mtu | 0:15b74f0f8c7f | 59 | speed = speed - gravity*5; |
| el17mtu | 0:15b74f0f8c7f | 60 | |
| el17mtu | 0:15b74f0f8c7f | 61 | } |
| el17mtu | 0:15b74f0f8c7f | 62 | |
| el17mtu | 10:1c3bdbf2fe9f | 63 | if (y_position > 48) { |
| el17mtu | 6:f7484a2f32fd | 64 | |
| el17mtu | 6:f7484a2f32fd | 65 | lcd.clear(); |
| el17mtu | 6:f7484a2f32fd | 66 | lcd.printString("GAME OVER",6,2); |
| el17mtu | 6:f7484a2f32fd | 67 | lcd.printString("Press BACK",6,4); |
| el17mtu | 6:f7484a2f32fd | 68 | pad.tone(2000.0,0.3); |
| el17mtu | 6:f7484a2f32fd | 69 | lcd.refresh(); |
| el17mtu | 6:f7484a2f32fd | 70 | bar_speed = 0; |
| el17mtu | 6:f7484a2f32fd | 71 | speed = 0; |
| el17mtu | 6:f7484a2f32fd | 72 | gravity = 0; |
| el17mtu | 6:f7484a2f32fd | 73 | y_position = 48; |
| el17mtu | 6:f7484a2f32fd | 74 | bar_width = 0; |
| el17mtu | 6:f7484a2f32fd | 75 | size_top = 0; |
| el17mtu | 6:f7484a2f32fd | 76 | size_bottom = 0; |
| el17mtu | 6:f7484a2f32fd | 77 | |
| el17mtu | 6:f7484a2f32fd | 78 | wait(0.5); |
| el17mtu | 6:f7484a2f32fd | 79 | |
| el17mtu | 0:15b74f0f8c7f | 80 | } |
| el17mtu | 9:571f92b0211b | 81 | |
| el17mtu | 9:571f92b0211b | 82 | |
| el17mtu | 0:15b74f0f8c7f | 83 | |
| el17mtu | 0:15b74f0f8c7f | 84 | if (y_position < 0) { |
| el17mtu | 0:15b74f0f8c7f | 85 | y_position = 0; |
| el17mtu | 0:15b74f0f8c7f | 86 | speed = 0; |
| el17mtu | 0:15b74f0f8c7f | 87 | } |
| el17mtu | 0:15b74f0f8c7f | 88 | |
| el17mtu | 0:15b74f0f8c7f | 89 | speed = speed + gravity; |
| el17mtu | 0:15b74f0f8c7f | 90 | y_position = y_position + speed; |
| el17mtu | 0:15b74f0f8c7f | 91 | wait(0.1); |
| el17mtu | 2:6ee8616695d1 | 92 | |
| el17mtu | 3:8e6950aeec8a | 93 | |
| el17mtu | 3:8e6950aeec8a | 94 | lcd.drawRect(screen_width,0,bar_width,size_top,FILL_BLACK); |
| el17mtu | 3:8e6950aeec8a | 95 | lcd.drawRect(screen_width,48-size_bottom,bar_width,size_bottom,FILL_BLACK); |
| el17mtu | 3:8e6950aeec8a | 96 | lcd.refresh(); |
| el17mtu | 3:8e6950aeec8a | 97 | |
| el17mtu | 9:571f92b0211b | 98 | // if (screen_width > 50) { |
| el17mtu | 9:571f92b0211b | 99 | // srand(time(NULL)); |
| el17mtu | 9:571f92b0211b | 100 | // size_top = rand() % 48; |
| el17mtu | 9:571f92b0211b | 101 | // srand(time(NULL)); |
| el17mtu | 9:571f92b0211b | 102 | // size_bottom = rand() % 48; |
| el17mtu | 3:8e6950aeec8a | 103 | // bar_width = 50; |
| el17mtu | 3:8e6950aeec8a | 104 | // bar_speed = 0; |
| el17mtu | 9:571f92b0211b | 105 | // } |
| el17mtu | 2:6ee8616695d1 | 106 | |
| el17mtu | 9:571f92b0211b | 107 | // if (screen_width == 5) { |
| el17mtu | 9:571f92b0211b | 108 | // srand(time(NULL)); |
| el17mtu | 9:571f92b0211b | 109 | // size_top = rand() % 15; |
| el17mtu | 9:571f92b0211b | 110 | // srand(time(NULL)); |
| el17mtu | 9:571f92b0211b | 111 | // size_bottom = rand() % 15; |
| el17mtu | 9:571f92b0211b | 112 | // } |
| el17mtu | 9:571f92b0211b | 113 | // if (screen_width == 11) { |
| el17mtu | 9:571f92b0211b | 114 | // lcd.clear(); |
| el17mtu | 9:571f92b0211b | 115 | // lcd.printString("GAME OVER",6,2); |
| el17mtu | 9:571f92b0211b | 116 | // lcd.printString("Press BACK",6,4); |
| el17mtu | 9:571f92b0211b | 117 | // pad.tone(2000.0,0.3); |
| el17mtu | 9:571f92b0211b | 118 | // lcd.refresh(); |
| el17mtu | 9:571f92b0211b | 119 | // bar_speed = 0; |
| el17mtu | 9:571f92b0211b | 120 | // speed = 0; |
| el17mtu | 9:571f92b0211b | 121 | // gravity = 0; |
| el17mtu | 9:571f92b0211b | 122 | // y_position = 48; |
| el17mtu | 9:571f92b0211b | 123 | // bar_width = 0; |
| el17mtu | 9:571f92b0211b | 124 | // size_top = 0; |
| el17mtu | 9:571f92b0211b | 125 | // size_bottom = 0; |
| el17mtu | 9:571f92b0211b | 126 | |
| el17mtu | 9:571f92b0211b | 127 | |
| el17mtu | 9:571f92b0211b | 128 | |
| el17mtu | 9:571f92b0211b | 129 | // wait(0.5); |
| el17mtu | 9:571f92b0211b | 130 | // } |
| el17mtu | 4:a81f05c8806a | 131 | |
| el17mtu | 4:a81f05c8806a | 132 | |
| el17mtu | 9:571f92b0211b | 133 | if ( pad.check_event(Gamepad::X_PRESSED) == true) { |
| el17mtu | 9:571f92b0211b | 134 | |
| el17mtu | 9:571f92b0211b | 135 | srand(time(NULL)); |
| el17mtu | 9:571f92b0211b | 136 | size_top = rand() % 15; |
| el17mtu | 9:571f92b0211b | 137 | srand(time(NULL)); |
| el17mtu | 9:571f92b0211b | 138 | size_bottom = rand() % 15; |
| el17mtu | 9:571f92b0211b | 139 | |
| el17mtu | 9:571f92b0211b | 140 | } |
| el17mtu | 9:571f92b0211b | 141 | |
| el17mtu | 9:571f92b0211b | 142 | if ((screen_width == x_position)&& (size_top > y_position)) { |
| el17mtu | 9:571f92b0211b | 143 | // size_top > y_position |
| el17mtu | 9:571f92b0211b | 144 | // screen_width == x_position |
| el17mtu | 9:571f92b0211b | 145 | lcd.clear(); |
| el17mtu | 9:571f92b0211b | 146 | lcd.printString("GAME OVER",6,2); |
| el17mtu | 9:571f92b0211b | 147 | lcd.printString("Press BACK",6,4); |
| el17mtu | 4:a81f05c8806a | 148 | pad.tone(2000.0,0.3); |
| el17mtu | 9:571f92b0211b | 149 | lcd.refresh(); |
| el17mtu | 9:571f92b0211b | 150 | bar_speed = 0; |
| el17mtu | 9:571f92b0211b | 151 | speed = 0; |
| el17mtu | 9:571f92b0211b | 152 | gravity = 0; |
| el17mtu | 9:571f92b0211b | 153 | y_position = 48; |
| el17mtu | 9:571f92b0211b | 154 | bar_width = 0; |
| el17mtu | 9:571f92b0211b | 155 | size_top = 0; |
| el17mtu | 9:571f92b0211b | 156 | size_bottom = 0; |
| el17mtu | 10:1c3bdbf2fe9f | 157 | |
| el17mtu | 4:a81f05c8806a | 158 | |
| el17mtu | 9:571f92b0211b | 159 | |
| el17mtu | 9:571f92b0211b | 160 | |
| el17mtu | 9:571f92b0211b | 161 | wait(0.5); |
| el17mtu | 9:571f92b0211b | 162 | |
| el17mtu | 10:1c3bdbf2fe9f | 163 | |
| el17mtu | 10:1c3bdbf2fe9f | 164 | |
| el17mtu | 10:1c3bdbf2fe9f | 165 | } |
| el17mtu | 10:1c3bdbf2fe9f | 166 | |
| el17mtu | 10:1c3bdbf2fe9f | 167 | |
| el17mtu | 10:1c3bdbf2fe9f | 168 | |
| el17mtu | 9:571f92b0211b | 169 | |
| el17mtu | 10:1c3bdbf2fe9f | 170 | // if ((screen_width == x_position)&& (size_bottom < (y_position - 6))) { |
| el17mtu | 10:1c3bdbf2fe9f | 171 | // |
| el17mtu | 10:1c3bdbf2fe9f | 172 | // lcd.printString("GAME OVER",6,2); |
| el17mtu | 10:1c3bdbf2fe9f | 173 | // lcd.printString("Press BACK",6,4); |
| el17mtu | 10:1c3bdbf2fe9f | 174 | // pad.tone(2000.0,0.3); |
| el17mtu | 10:1c3bdbf2fe9f | 175 | // lcd.refresh(); |
| el17mtu | 10:1c3bdbf2fe9f | 176 | // bar_speed = 0; |
| el17mtu | 10:1c3bdbf2fe9f | 177 | // speed = 0; |
| el17mtu | 10:1c3bdbf2fe9f | 178 | // gravity = 0; |
| el17mtu | 10:1c3bdbf2fe9f | 179 | // y_position = 48; |
| el17mtu | 10:1c3bdbf2fe9f | 180 | // bar_width = 0; |
| el17mtu | 10:1c3bdbf2fe9f | 181 | // size_top = 0; |
| el17mtu | 10:1c3bdbf2fe9f | 182 | // size_bottom = 0; |
| el17mtu | 10:1c3bdbf2fe9f | 183 | |
| el17mtu | 10:1c3bdbf2fe9f | 184 | |
| el17mtu | 10:1c3bdbf2fe9f | 185 | |
| el17mtu | 10:1c3bdbf2fe9f | 186 | // |
| el17mtu | 10:1c3bdbf2fe9f | 187 | // wait(0.5); |
| el17mtu | 10:1c3bdbf2fe9f | 188 | |
| el17mtu | 10:1c3bdbf2fe9f | 189 | // } |
| el17mtu | 10:1c3bdbf2fe9f | 190 | |
| el17mtu | 10:1c3bdbf2fe9f | 191 | if ((screen_width == x_position)&& (size_top < y_position)) { |
| el17mtu | 10:1c3bdbf2fe9f | 192 | score = 1; |
| el17mtu | 10:1c3bdbf2fe9f | 193 | lcd.clear(); |
| el17mtu | 10:1c3bdbf2fe9f | 194 | char buffer[14]; |
| el17mtu | 10:1c3bdbf2fe9f | 195 | sprintf(buffer,"% .2f ", score); |
| el17mtu | 10:1c3bdbf2fe9f | 196 | lcd.printString(buffer,2,2); |
| el17mtu | 10:1c3bdbf2fe9f | 197 | lcd.printString("Score =",1,1); |
| el17mtu | 10:1c3bdbf2fe9f | 198 | lcd.refresh(); |
| el17mtu | 10:1c3bdbf2fe9f | 199 | wait(1.0); |
| el17mtu | 10:1c3bdbf2fe9f | 200 | |
| el17mtu | 10:1c3bdbf2fe9f | 201 | } |
| el17mtu | 10:1c3bdbf2fe9f | 202 | // if ((screen_width == x_position)&& (size_bottom < (y_position - 6))) { |
| el17mtu | 10:1c3bdbf2fe9f | 203 | |
| el17mtu | 10:1c3bdbf2fe9f | 204 | // } |
| el17mtu | 0:15b74f0f8c7f | 205 | |
| el17mtu | 1:44f4594eacac | 206 | screen_width = screen_width - bar_speed; |
| el17mtu | 10:1c3bdbf2fe9f | 207 | |
| el17mtu | 3:8e6950aeec8a | 208 | wait(0.1); |
| el17mtu | 4:a81f05c8806a | 209 | |
| el17mtu | 4:a81f05c8806a | 210 | |
| el17mtu | 0:15b74f0f8c7f | 211 | |
| el17mtu | 0:15b74f0f8c7f | 212 | } |
| el17mtu | 0:15b74f0f8c7f | 213 | |
| el17mtu | 0:15b74f0f8c7f | 214 | |
| el17mtu | 0:15b74f0f8c7f | 215 | |
| el17mtu | 0:15b74f0f8c7f | 216 | |
| el17mtu | 0:15b74f0f8c7f | 217 | |
| el17mtu | 0:15b74f0f8c7f | 218 |