Haoyan Zhang
/
el17h2z1
deemo1
mian.cpp@5:32dbfaf578dd, 2020-05-14 (annotated)
- Committer:
- haoyan
- Date:
- Thu May 14 05:21:50 2020 +0000
- Revision:
- 5:32dbfaf578dd
- Parent:
- 4:9fa0c5edd1a1
- Child:
- 6:b59bc5e15cf3
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haoyan | 0:08b2bdddf22e | 1 | /* |
haoyan | 0:08b2bdddf22e | 2 | ELEC2645 Embedded Systems Project |
haoyan | 0:08b2bdddf22e | 3 | School of Electronic & Electrical Engineering |
haoyan | 0:08b2bdddf22e | 4 | University of Leeds |
haoyan | 0:08b2bdddf22e | 5 | Name: Haoyan Zhang |
haoyan | 0:08b2bdddf22e | 6 | Username: el17h2z |
haoyan | 0:08b2bdddf22e | 7 | Student ID Number: 201199698 |
haoyan | 1:8c48fb8ca5e0 | 8 | Date: 02/05/2020 |
haoyan | 0:08b2bdddf22e | 9 | */ |
haoyan | 0:08b2bdddf22e | 10 | |
haoyan | 0:08b2bdddf22e | 11 | ///////// pre-processor directives //////// |
haoyan | 0:08b2bdddf22e | 12 | #include "mbed.h" |
haoyan | 0:08b2bdddf22e | 13 | #include "N5110.h" |
haoyan | 0:08b2bdddf22e | 14 | #include "Gamepad.h" |
haoyan | 1:8c48fb8ca5e0 | 15 | #include "StarcraftEngine.h" |
haoyan | 0:08b2bdddf22e | 16 | |
haoyan | 1:8c48fb8ca5e0 | 17 | #define BATTLESHIP_HEIGHT 3 |
haoyan | 1:8c48fb8ca5e0 | 18 | #define BATTLESHIP_WIDTH 6 |
haoyan | 1:8c48fb8ca5e0 | 19 | #define LASER_HEIGHT 2 |
haoyan | 1:8c48fb8ca5e0 | 20 | #define LASER_WIDTH 1 |
haoyan | 1:8c48fb8ca5e0 | 21 | #define SWARM_HEIGHT 6 |
haoyan | 1:8c48fb8ca5e0 | 22 | #define SWARM_WIDTH 6 |
haoyan | 1:8c48fb8ca5e0 | 23 | #define SWARM_SPEED 1 |
haoyan | 3:1db91ad3ab84 | 24 | #define BOSS_HEIGHT 4 |
haoyan | 3:1db91ad3ab84 | 25 | #define BOSS_WIDTH 6 |
haoyan | 4:9fa0c5edd1a1 | 26 | #define ACID_HEIGHT 3 |
haoyan | 4:9fa0c5edd1a1 | 27 | #define ACID_WIDTH 1 |
haoyan | 1:8c48fb8ca5e0 | 28 | |
haoyan | 1:8c48fb8ca5e0 | 29 | |
haoyan | 1:8c48fb8ca5e0 | 30 | |
haoyan | 1:8c48fb8ca5e0 | 31 | /////////////// structs ///////////////// |
haoyan | 1:8c48fb8ca5e0 | 32 | struct UserInput { |
haoyan | 1:8c48fb8ca5e0 | 33 | Direction d; |
haoyan | 1:8c48fb8ca5e0 | 34 | float mag; |
haoyan | 1:8c48fb8ca5e0 | 35 | }; |
haoyan | 1:8c48fb8ca5e0 | 36 | |
haoyan | 1:8c48fb8ca5e0 | 37 | /////////////// objects /////////////// |
haoyan | 1:8c48fb8ca5e0 | 38 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
haoyan | 1:8c48fb8ca5e0 | 39 | Gamepad pad; |
haoyan | 1:8c48fb8ca5e0 | 40 | StarcraftEngine Starcraft; |
haoyan | 1:8c48fb8ca5e0 | 41 | |
haoyan | 1:8c48fb8ca5e0 | 42 | ///////////// prototypes /////////////// |
haoyan | 1:8c48fb8ca5e0 | 43 | void init(); |
haoyan | 1:8c48fb8ca5e0 | 44 | void update_game(UserInput input); |
haoyan | 1:8c48fb8ca5e0 | 45 | void render(); |
haoyan | 1:8c48fb8ca5e0 | 46 | void welcome(); |
haoyan | 2:03cd3bb32511 | 47 | void victory(); |
haoyan | 1:8c48fb8ca5e0 | 48 | void gameover(); |
haoyan | 1:8c48fb8ca5e0 | 49 | void background(); |
haoyan | 1:8c48fb8ca5e0 | 50 | void score(); |
haoyan | 1:8c48fb8ca5e0 | 51 | |
haoyan | 1:8c48fb8ca5e0 | 52 | ///////////// functions //////////////// |
haoyan | 1:8c48fb8ca5e0 | 53 | int main() |
haoyan | 1:8c48fb8ca5e0 | 54 | { |
haoyan | 1:8c48fb8ca5e0 | 55 | int fps = 8; // frames per second |
haoyan | 1:8c48fb8ca5e0 | 56 | S1:init(); // initialise and then display welcome screen... |
haoyan | 1:8c48fb8ca5e0 | 57 | welcome(); // waiting for the user to start |
haoyan | 1:8c48fb8ca5e0 | 58 | background(); //introduce background story |
haoyan | 1:8c48fb8ca5e0 | 59 | render(); // first draw the initial frame |
haoyan | 1:8c48fb8ca5e0 | 60 | wait(1.0f/fps); // and wait for one frame period |
haoyan | 1:8c48fb8ca5e0 | 61 | |
haoyan | 1:8c48fb8ca5e0 | 62 | // game loop - read input, update the game state and render the display |
haoyan | 1:8c48fb8ca5e0 | 63 | while(Starcraft.find_life() > 0) { |
haoyan | 1:8c48fb8ca5e0 | 64 | Starcraft.read_input(pad); |
haoyan | 1:8c48fb8ca5e0 | 65 | Starcraft.update(pad); |
haoyan | 1:8c48fb8ca5e0 | 66 | render(); |
haoyan | 1:8c48fb8ca5e0 | 67 | wait(1.0f/fps); |
haoyan | 5:32dbfaf578dd | 68 | if (Starcraft.find_score() == 15) { |
haoyan | 2:03cd3bb32511 | 69 | break; |
haoyan | 2:03cd3bb32511 | 70 | } |
haoyan | 1:8c48fb8ca5e0 | 71 | } |
haoyan | 2:03cd3bb32511 | 72 | |
haoyan | 1:8c48fb8ca5e0 | 73 | score(); |
haoyan | 5:32dbfaf578dd | 74 | if (Starcraft.find_score() == 15) { // judge victory or fault |
haoyan | 2:03cd3bb32511 | 75 | victory(); |
haoyan | 2:03cd3bb32511 | 76 | wait(1); |
haoyan | 2:03cd3bb32511 | 77 | goto S1; |
haoyan | 2:03cd3bb32511 | 78 | } |
haoyan | 2:03cd3bb32511 | 79 | |
haoyan | 2:03cd3bb32511 | 80 | else{ |
haoyan | 2:03cd3bb32511 | 81 | gameover(); |
haoyan | 2:03cd3bb32511 | 82 | wait(1); |
haoyan | 2:03cd3bb32511 | 83 | goto S1; |
haoyan | 2:03cd3bb32511 | 84 | } |
haoyan | 1:8c48fb8ca5e0 | 85 | } |
haoyan | 1:8c48fb8ca5e0 | 86 | |
haoyan | 1:8c48fb8ca5e0 | 87 | // initialies all classes and libraries |
haoyan | 1:8c48fb8ca5e0 | 88 | void init() |
haoyan | 1:8c48fb8ca5e0 | 89 | { |
haoyan | 1:8c48fb8ca5e0 | 90 | // need to initialise LCD and Gamepad |
haoyan | 1:8c48fb8ca5e0 | 91 | lcd.init(); |
haoyan | 1:8c48fb8ca5e0 | 92 | pad.init(); |
haoyan | 1:8c48fb8ca5e0 | 93 | |
haoyan | 5:32dbfaf578dd | 94 | // initialise the game with correct battleship, laser, boss and acid's sizes |
haoyan | 4:9fa0c5edd1a1 | 95 | Starcraft.init(BATTLESHIP_HEIGHT, BATTLESHIP_WIDTH, LASER_HEIGHT, LASER_WIDTH,SWARM_HEIGHT, SWARM_WIDTH, BOSS_HEIGHT, BOSS_WIDTH, ACID_HEIGHT, ACID_WIDTH, SWARM_SPEED); |
haoyan | 1:8c48fb8ca5e0 | 96 | } |
haoyan | 1:8c48fb8ca5e0 | 97 | |
haoyan | 1:8c48fb8ca5e0 | 98 | // this function draws each frame on the LCD |
haoyan | 1:8c48fb8ca5e0 | 99 | void render() |
haoyan | 1:8c48fb8ca5e0 | 100 | { |
haoyan | 1:8c48fb8ca5e0 | 101 | // clear screen, re-draw and refresh |
haoyan | 1:8c48fb8ca5e0 | 102 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 103 | Starcraft.draw(lcd); |
haoyan | 1:8c48fb8ca5e0 | 104 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 105 | } |
haoyan | 1:8c48fb8ca5e0 | 106 | |
haoyan | 1:8c48fb8ca5e0 | 107 | // simple splash screen displayed on start-up |
haoyan | 1:8c48fb8ca5e0 | 108 | void welcome() |
haoyan | 1:8c48fb8ca5e0 | 109 | { |
haoyan | 1:8c48fb8ca5e0 | 110 | lcd.printString("Starcraft!",0,1); |
haoyan | 1:8c48fb8ca5e0 | 111 | lcd.printString("Press Start",0,4); |
haoyan | 1:8c48fb8ca5e0 | 112 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 113 | |
haoyan | 1:8c48fb8ca5e0 | 114 | // wait flashing LEDs until start button is pressed |
haoyan | 1:8c48fb8ca5e0 | 115 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
haoyan | 1:8c48fb8ca5e0 | 116 | pad.leds_on(); |
haoyan | 1:8c48fb8ca5e0 | 117 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 118 | pad.leds_off(); |
haoyan | 1:8c48fb8ca5e0 | 119 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 120 | } |
haoyan | 1:8c48fb8ca5e0 | 121 | |
haoyan | 1:8c48fb8ca5e0 | 122 | } |
haoyan | 1:8c48fb8ca5e0 | 123 | |
haoyan | 1:8c48fb8ca5e0 | 124 | // introduce background story |
haoyan | 1:8c48fb8ca5e0 | 125 | void background() |
haoyan | 1:8c48fb8ca5e0 | 126 | { |
haoyan | 1:8c48fb8ca5e0 | 127 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 128 | lcd.printString(" WARNING!!! ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 129 | lcd.printString(" WARNING!!! ",1,3); |
haoyan | 1:8c48fb8ca5e0 | 130 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 131 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 132 | |
haoyan | 1:8c48fb8ca5e0 | 133 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 134 | lcd.printString(" Swarm invide ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 135 | lcd.printString(" Our galaxy ",1,3); |
haoyan | 1:8c48fb8ca5e0 | 136 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 137 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 138 | |
haoyan | 1:8c48fb8ca5e0 | 139 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 140 | lcd.printString(" Our fleet ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 141 | lcd.printString(" Has been ",1,2); |
haoyan | 1:8c48fb8ca5e0 | 142 | lcd.printString(" Destroyed ",2,3); |
haoyan | 1:8c48fb8ca5e0 | 143 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 144 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 145 | |
haoyan | 1:8c48fb8ca5e0 | 146 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 147 | lcd.printString(" You're the ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 148 | lcd.printString(" Last ",2,2); |
haoyan | 1:8c48fb8ca5e0 | 149 | lcd.printString(" Battleship ",3,4); |
haoyan | 1:8c48fb8ca5e0 | 150 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 151 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 152 | |
haoyan | 1:8c48fb8ca5e0 | 153 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 154 | lcd.printString(" Kill them ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 155 | lcd.printString(" All!!! ",3,3); |
haoyan | 1:8c48fb8ca5e0 | 156 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 157 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 158 | |
haoyan | 1:8c48fb8ca5e0 | 159 | lcd.clear(); |
haoyan | 2:03cd3bb32511 | 160 | lcd.printString(" Good luck! ",2,2); |
haoyan | 1:8c48fb8ca5e0 | 161 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 162 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 163 | } |
haoyan | 1:8c48fb8ca5e0 | 164 | |
haoyan | 1:8c48fb8ca5e0 | 165 | // when game over the screen will display the score |
haoyan | 1:8c48fb8ca5e0 | 166 | void score() |
haoyan | 1:8c48fb8ca5e0 | 167 | { |
haoyan | 1:8c48fb8ca5e0 | 168 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 169 | int battleship_score = Starcraft.find_score(); |
haoyan | 1:8c48fb8ca5e0 | 170 | char buffer3[14]; |
haoyan | 1:8c48fb8ca5e0 | 171 | sprintf(buffer3,"%2d",battleship_score); |
haoyan | 1:8c48fb8ca5e0 | 172 | lcd.printString("SCORE",25 ,2); |
haoyan | 1:8c48fb8ca5e0 | 173 | lcd.printString(buffer3,35 ,3); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
haoyan | 1:8c48fb8ca5e0 | 174 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 175 | wait(1.5); |
haoyan | 1:8c48fb8ca5e0 | 176 | } |
haoyan | 1:8c48fb8ca5e0 | 177 | |
haoyan | 2:03cd3bb32511 | 178 | void victory() |
haoyan | 2:03cd3bb32511 | 179 | { |
haoyan | 2:03cd3bb32511 | 180 | lcd.clear(); |
haoyan | 3:1db91ad3ab84 | 181 | lcd.printString(" Victory! ",1,1); |
haoyan | 3:1db91ad3ab84 | 182 | lcd.printString(" You protect ",1,2); |
haoyan | 3:1db91ad3ab84 | 183 | lcd.printString(" the earth! ",1,3); |
haoyan | 2:03cd3bb32511 | 184 | lcd.refresh(); |
haoyan | 2:03cd3bb32511 | 185 | wait(2); |
haoyan | 2:03cd3bb32511 | 186 | |
haoyan | 2:03cd3bb32511 | 187 | lcd.clear(); |
haoyan | 3:1db91ad3ab84 | 188 | lcd.printString(" Press back ",1,1); |
haoyan | 3:1db91ad3ab84 | 189 | lcd.printString(" to play again ",1,3); |
haoyan | 2:03cd3bb32511 | 190 | lcd.refresh(); |
haoyan | 2:03cd3bb32511 | 191 | |
haoyan | 2:03cd3bb32511 | 192 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::BACK_PRESSED) == false) { |
haoyan | 2:03cd3bb32511 | 193 | pad.leds_on(); |
haoyan | 2:03cd3bb32511 | 194 | wait(0.1); |
haoyan | 2:03cd3bb32511 | 195 | pad.leds_off(); |
haoyan | 2:03cd3bb32511 | 196 | wait(0.1); |
haoyan | 2:03cd3bb32511 | 197 | } |
haoyan | 2:03cd3bb32511 | 198 | } |
haoyan | 2:03cd3bb32511 | 199 | |
haoyan | 1:8c48fb8ca5e0 | 200 | void gameover() |
haoyan | 1:8c48fb8ca5e0 | 201 | { |
haoyan | 1:8c48fb8ca5e0 | 202 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 203 | lcd.printString(" You failed! ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 204 | lcd.printString(" Press back ",1,3); |
haoyan | 1:8c48fb8ca5e0 | 205 | lcd.printString(" to play again ",1,4); |
haoyan | 1:8c48fb8ca5e0 | 206 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 207 | |
haoyan | 1:8c48fb8ca5e0 | 208 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::BACK_PRESSED) == false) { |
haoyan | 1:8c48fb8ca5e0 | 209 | pad.leds_on(); |
haoyan | 1:8c48fb8ca5e0 | 210 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 211 | pad.leds_off(); |
haoyan | 1:8c48fb8ca5e0 | 212 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 213 | } |
haoyan | 1:8c48fb8ca5e0 | 214 | } |