Li Ruofan 201199450
Dependencies: mbed
main.cpp@0:80a59a49d504, 2020-05-23 (annotated)
- Committer:
- DannyLee
- Date:
- Sat May 23 19:01:15 2020 +0000
- Revision:
- 0:80a59a49d504
- Child:
- 1:bd7c99a5bd10
The new project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DannyLee | 0:80a59a49d504 | 1 | /* |
DannyLee | 0:80a59a49d504 | 2 | ELEC2645 Embedded Systems Project |
DannyLee | 0:80a59a49d504 | 3 | School of Electronic & Electrical Engineering |
DannyLee | 0:80a59a49d504 | 4 | University of Leeds |
DannyLee | 0:80a59a49d504 | 5 | Name: Ruofan Li |
DannyLee | 0:80a59a49d504 | 6 | Username: el17rl |
DannyLee | 0:80a59a49d504 | 7 | Student ID Number: 201199450 |
DannyLee | 0:80a59a49d504 | 8 | Date: May, 2020 |
DannyLee | 0:80a59a49d504 | 9 | */ |
DannyLee | 0:80a59a49d504 | 10 | |
DannyLee | 0:80a59a49d504 | 11 | #include "mbed.h" |
DannyLee | 0:80a59a49d504 | 12 | #include "N5110.h" |
DannyLee | 0:80a59a49d504 | 13 | #include "Gamepad.h" |
DannyLee | 0:80a59a49d504 | 14 | #include "Snake.h" |
DannyLee | 0:80a59a49d504 | 15 | |
DannyLee | 0:80a59a49d504 | 16 | struct UserInput { |
DannyLee | 0:80a59a49d504 | 17 | Direction d; |
DannyLee | 0:80a59a49d504 | 18 | }; |
DannyLee | 0:80a59a49d504 | 19 | |
DannyLee | 0:80a59a49d504 | 20 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);//set LCD pins |
DannyLee | 0:80a59a49d504 | 21 | Gamepad pad; |
DannyLee | 0:80a59a49d504 | 22 | Snake snake; |
DannyLee | 0:80a59a49d504 | 23 | |
DannyLee | 0:80a59a49d504 | 24 | int bgm_(); |
DannyLee | 0:80a59a49d504 | 25 | int sound_(); |
DannyLee | 0:80a59a49d504 | 26 | void begin(); |
DannyLee | 0:80a59a49d504 | 27 | void init(Gamepad &pad); |
DannyLee | 0:80a59a49d504 | 28 | void game_tips(); |
DannyLee | 0:80a59a49d504 | 29 | |
DannyLee | 0:80a59a49d504 | 30 | int main() |
DannyLee | 0:80a59a49d504 | 31 | { |
DannyLee | 0:80a59a49d504 | 32 | init(pad); |
DannyLee | 0:80a59a49d504 | 33 | // game loop |
DannyLee | 0:80a59a49d504 | 34 | while (1) { |
DannyLee | 0:80a59a49d504 | 35 | snake.draw(lcd); |
DannyLee | 0:80a59a49d504 | 36 | snake.update(pad); |
DannyLee | 0:80a59a49d504 | 37 | } |
DannyLee | 0:80a59a49d504 | 38 | } |
DannyLee | 0:80a59a49d504 | 39 | |
DannyLee | 0:80a59a49d504 | 40 | void init(Gamepad &pad) |
DannyLee | 0:80a59a49d504 | 41 | { |
DannyLee | 0:80a59a49d504 | 42 | // initialise LCD and Gamepad |
DannyLee | 0:80a59a49d504 | 43 | lcd.init(); |
DannyLee | 0:80a59a49d504 | 44 | pad.init(); |
DannyLee | 0:80a59a49d504 | 45 | begin(); |
DannyLee | 0:80a59a49d504 | 46 | snake.init(25,25,6,1); |
DannyLee | 0:80a59a49d504 | 47 | // initialise parameters of the project |
DannyLee | 0:80a59a49d504 | 48 | // display tips |
DannyLee | 0:80a59a49d504 | 49 | game_tips(); |
DannyLee | 0:80a59a49d504 | 50 | } |
DannyLee | 0:80a59a49d504 | 51 | |
DannyLee | 0:80a59a49d504 | 52 | void begin(){ |
DannyLee | 0:80a59a49d504 | 53 | lcd.printString(" Greedy Snake ",0,1); |
DannyLee | 0:80a59a49d504 | 54 | lcd.printString(" Li Ruofan ",0,2); |
DannyLee | 0:80a59a49d504 | 55 | lcd.printString(" 201199450 ",0,3); |
DannyLee | 0:80a59a49d504 | 56 | lcd.printString(" Quickly ",0,4); |
DannyLee | 0:80a59a49d504 | 57 | lcd.printString(" Push X twice ",0,5); |
DannyLee | 0:80a59a49d504 | 58 | lcd.refresh(); |
DannyLee | 0:80a59a49d504 | 59 | |
DannyLee | 0:80a59a49d504 | 60 | while (pad.check_event(Gamepad::X_PRESSED) == false){ |
DannyLee | 0:80a59a49d504 | 61 | pad.leds_on(); |
DannyLee | 0:80a59a49d504 | 62 | sound_(); |
DannyLee | 0:80a59a49d504 | 63 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 64 | pad.leds_off(); |
DannyLee | 0:80a59a49d504 | 65 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 66 | } |
DannyLee | 0:80a59a49d504 | 67 | |
DannyLee | 0:80a59a49d504 | 68 | lcd.clear(); |
DannyLee | 0:80a59a49d504 | 69 | |
DannyLee | 0:80a59a49d504 | 70 | } |
DannyLee | 0:80a59a49d504 | 71 | |
DannyLee | 0:80a59a49d504 | 72 | void game_tips(){ |
DannyLee | 0:80a59a49d504 | 73 | lcd.clear(); |
DannyLee | 0:80a59a49d504 | 74 | lcd.printString(" Greedy Snake! ",0,0); |
DannyLee | 0:80a59a49d504 | 75 | lcd.printString(" Move the snake ",0,1); |
DannyLee | 0:80a59a49d504 | 76 | lcd.printString(" To eat apples ",0,2); |
DannyLee | 0:80a59a49d504 | 77 | lcd.printString(" Far from wall ",0,3); |
DannyLee | 0:80a59a49d504 | 78 | lcd.printString(" Press start! ",0,5); |
DannyLee | 0:80a59a49d504 | 79 | lcd.refresh(); |
DannyLee | 0:80a59a49d504 | 80 | |
DannyLee | 0:80a59a49d504 | 81 | while (pad.check_event(Gamepad::START_PRESSED) == false) { |
DannyLee | 0:80a59a49d504 | 82 | pad.leds_on(); |
DannyLee | 0:80a59a49d504 | 83 | wait(0.2); |
DannyLee | 0:80a59a49d504 | 84 | pad.leds_off(); |
DannyLee | 0:80a59a49d504 | 85 | wait(0.2); |
DannyLee | 0:80a59a49d504 | 86 | } |
DannyLee | 0:80a59a49d504 | 87 | } |
DannyLee | 0:80a59a49d504 | 88 | |
DannyLee | 0:80a59a49d504 | 89 | int sound_(){ |
DannyLee | 0:80a59a49d504 | 90 | pad.tone(784.0,0.3); |
DannyLee | 0:80a59a49d504 | 91 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 92 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 93 | return 0; |
DannyLee | 0:80a59a49d504 | 94 | } |
DannyLee | 0:80a59a49d504 | 95 | pad.tone(784.0,0.3); |
DannyLee | 0:80a59a49d504 | 96 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 97 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 98 | return 0; |
DannyLee | 0:80a59a49d504 | 99 | } |
DannyLee | 0:80a59a49d504 | 100 | pad.tone(880.0,0.3); |
DannyLee | 0:80a59a49d504 | 101 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 102 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 103 | return 0; |
DannyLee | 0:80a59a49d504 | 104 | } |
DannyLee | 0:80a59a49d504 | 105 | pad.tone(587.0,0.6); |
DannyLee | 0:80a59a49d504 | 106 | wait(0.5); |
DannyLee | 0:80a59a49d504 | 107 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 108 | return 0; |
DannyLee | 0:80a59a49d504 | 109 | } |
DannyLee | 0:80a59a49d504 | 110 | pad.tone(523.0,0.3); |
DannyLee | 0:80a59a49d504 | 111 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 112 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 113 | return 0; |
DannyLee | 0:80a59a49d504 | 114 | } |
DannyLee | 0:80a59a49d504 | 115 | pad.tone(523.0,0.3); |
DannyLee | 0:80a59a49d504 | 116 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 117 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 118 | return 0; |
DannyLee | 0:80a59a49d504 | 119 | } |
DannyLee | 0:80a59a49d504 | 120 | pad.tone(440.0,0.3); |
DannyLee | 0:80a59a49d504 | 121 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 122 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 123 | return 0; |
DannyLee | 0:80a59a49d504 | 124 | } |
DannyLee | 0:80a59a49d504 | 125 | pad.tone(587.0,0.6); |
DannyLee | 0:80a59a49d504 | 126 | wait(0.4); |
DannyLee | 0:80a59a49d504 | 127 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 128 | return 0; |
DannyLee | 0:80a59a49d504 | 129 | } |
DannyLee | 0:80a59a49d504 | 130 | pad.tone(784.0,0.4); |
DannyLee | 0:80a59a49d504 | 131 | wait(0.4); |
DannyLee | 0:80a59a49d504 | 132 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 133 | return 0; |
DannyLee | 0:80a59a49d504 | 134 | } |
DannyLee | 0:80a59a49d504 | 135 | pad.tone(784.0,0.4); |
DannyLee | 0:80a59a49d504 | 136 | wait(0.4); |
DannyLee | 0:80a59a49d504 | 137 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 138 | return 0; |
DannyLee | 0:80a59a49d504 | 139 | } |
DannyLee | 0:80a59a49d504 | 140 | pad.tone(880.0,0.3); |
DannyLee | 0:80a59a49d504 | 141 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 142 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 143 | return 0; |
DannyLee | 0:80a59a49d504 | 144 | } |
DannyLee | 0:80a59a49d504 | 145 | pad.tone(1046.0,0.3); |
DannyLee | 0:80a59a49d504 | 146 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 147 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 148 | return 0; |
DannyLee | 0:80a59a49d504 | 149 | } |
DannyLee | 0:80a59a49d504 | 150 | pad.tone(880.0,0.3); |
DannyLee | 0:80a59a49d504 | 151 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 152 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 153 | return 0; |
DannyLee | 0:80a59a49d504 | 154 | } |
DannyLee | 0:80a59a49d504 | 155 | pad.tone(784.0,0.3); |
DannyLee | 0:80a59a49d504 | 156 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 157 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 158 | return 0; |
DannyLee | 0:80a59a49d504 | 159 | } |
DannyLee | 0:80a59a49d504 | 160 | pad.tone(523.0,0.4); |
DannyLee | 0:80a59a49d504 | 161 | wait(0.4); |
DannyLee | 0:80a59a49d504 | 162 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 163 | return 0; |
DannyLee | 0:80a59a49d504 | 164 | } |
DannyLee | 0:80a59a49d504 | 165 | pad.tone(523.0,0.4); |
DannyLee | 0:80a59a49d504 | 166 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 167 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 168 | return 0; |
DannyLee | 0:80a59a49d504 | 169 | } |
DannyLee | 0:80a59a49d504 | 170 | |
DannyLee | 0:80a59a49d504 | 171 | pad.tone(440.0,0.3); |
DannyLee | 0:80a59a49d504 | 172 | wait(0.3); |
DannyLee | 0:80a59a49d504 | 173 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 174 | return 0; |
DannyLee | 0:80a59a49d504 | 175 | } |
DannyLee | 0:80a59a49d504 | 176 | pad.tone(587.0,0.6); |
DannyLee | 0:80a59a49d504 | 177 | wait(0.6); |
DannyLee | 0:80a59a49d504 | 178 | if(pad.check_event(Gamepad::X_PRESSED) == true){ |
DannyLee | 0:80a59a49d504 | 179 | return 0; |
DannyLee | 0:80a59a49d504 | 180 | } |
DannyLee | 0:80a59a49d504 | 181 | return 0; |
DannyLee | 0:80a59a49d504 | 182 | }//play the Chinese song "East is Red" |
DannyLee | 0:80a59a49d504 | 183 | |
DannyLee | 0:80a59a49d504 | 184 |