Haoyan Zhang
/
el17h2z1
deemo1
mian.cpp@1:8c48fb8ca5e0, 2020-05-11 (annotated)
- Committer:
- haoyan
- Date:
- Mon May 11 06:50:18 2020 +0000
- Revision:
- 1:8c48fb8ca5e0
- Parent:
- 0:08b2bdddf22e
- Child:
- 2:03cd3bb32511
Finish
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 | 1:8c48fb8ca5e0 | 24 | |
haoyan | 1:8c48fb8ca5e0 | 25 | |
haoyan | 1:8c48fb8ca5e0 | 26 | |
haoyan | 1:8c48fb8ca5e0 | 27 | /////////////// structs ///////////////// |
haoyan | 1:8c48fb8ca5e0 | 28 | struct UserInput { |
haoyan | 1:8c48fb8ca5e0 | 29 | Direction d; |
haoyan | 1:8c48fb8ca5e0 | 30 | float mag; |
haoyan | 1:8c48fb8ca5e0 | 31 | }; |
haoyan | 1:8c48fb8ca5e0 | 32 | |
haoyan | 1:8c48fb8ca5e0 | 33 | /////////////// objects /////////////// |
haoyan | 1:8c48fb8ca5e0 | 34 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
haoyan | 1:8c48fb8ca5e0 | 35 | Gamepad pad; |
haoyan | 1:8c48fb8ca5e0 | 36 | StarcraftEngine Starcraft; |
haoyan | 1:8c48fb8ca5e0 | 37 | |
haoyan | 1:8c48fb8ca5e0 | 38 | ///////////// prototypes /////////////// |
haoyan | 1:8c48fb8ca5e0 | 39 | void init(); |
haoyan | 1:8c48fb8ca5e0 | 40 | void update_game(UserInput input); |
haoyan | 1:8c48fb8ca5e0 | 41 | void render(); |
haoyan | 1:8c48fb8ca5e0 | 42 | void welcome(); |
haoyan | 1:8c48fb8ca5e0 | 43 | void gameover(); |
haoyan | 1:8c48fb8ca5e0 | 44 | void background(); |
haoyan | 1:8c48fb8ca5e0 | 45 | void score(); |
haoyan | 1:8c48fb8ca5e0 | 46 | |
haoyan | 1:8c48fb8ca5e0 | 47 | ///////////// functions //////////////// |
haoyan | 1:8c48fb8ca5e0 | 48 | int main() |
haoyan | 1:8c48fb8ca5e0 | 49 | { |
haoyan | 1:8c48fb8ca5e0 | 50 | int fps = 8; // frames per second |
haoyan | 1:8c48fb8ca5e0 | 51 | S1:init(); // initialise and then display welcome screen... |
haoyan | 1:8c48fb8ca5e0 | 52 | welcome(); // waiting for the user to start |
haoyan | 1:8c48fb8ca5e0 | 53 | background(); //introduce background story |
haoyan | 1:8c48fb8ca5e0 | 54 | render(); // first draw the initial frame |
haoyan | 1:8c48fb8ca5e0 | 55 | wait(1.0f/fps); // and wait for one frame period |
haoyan | 1:8c48fb8ca5e0 | 56 | |
haoyan | 1:8c48fb8ca5e0 | 57 | // game loop - read input, update the game state and render the display |
haoyan | 1:8c48fb8ca5e0 | 58 | while(Starcraft.find_life() > 0) { |
haoyan | 1:8c48fb8ca5e0 | 59 | Starcraft.read_input(pad); |
haoyan | 1:8c48fb8ca5e0 | 60 | Starcraft.update(pad); |
haoyan | 1:8c48fb8ca5e0 | 61 | render(); |
haoyan | 1:8c48fb8ca5e0 | 62 | wait(1.0f/fps); |
haoyan | 1:8c48fb8ca5e0 | 63 | } |
haoyan | 1:8c48fb8ca5e0 | 64 | score(); |
haoyan | 1:8c48fb8ca5e0 | 65 | gameover(); |
haoyan | 1:8c48fb8ca5e0 | 66 | wait(1); |
haoyan | 1:8c48fb8ca5e0 | 67 | goto S1; |
haoyan | 1:8c48fb8ca5e0 | 68 | } |
haoyan | 1:8c48fb8ca5e0 | 69 | |
haoyan | 1:8c48fb8ca5e0 | 70 | // initialies all classes and libraries |
haoyan | 1:8c48fb8ca5e0 | 71 | void init() |
haoyan | 1:8c48fb8ca5e0 | 72 | { |
haoyan | 1:8c48fb8ca5e0 | 73 | // need to initialise LCD and Gamepad |
haoyan | 1:8c48fb8ca5e0 | 74 | lcd.init(); |
haoyan | 1:8c48fb8ca5e0 | 75 | pad.init(); |
haoyan | 1:8c48fb8ca5e0 | 76 | |
haoyan | 1:8c48fb8ca5e0 | 77 | // initialise the game with correct ball and paddle sizes |
haoyan | 1:8c48fb8ca5e0 | 78 | Starcraft.init(BATTLESHIP_HEIGHT, BATTLESHIP_WIDTH, LASER_HEIGHT, LASER_WIDTH,SWARM_HEIGHT, SWARM_WIDTH, SWARM_SPEED); |
haoyan | 1:8c48fb8ca5e0 | 79 | } |
haoyan | 1:8c48fb8ca5e0 | 80 | |
haoyan | 1:8c48fb8ca5e0 | 81 | // this function draws each frame on the LCD |
haoyan | 1:8c48fb8ca5e0 | 82 | void render() |
haoyan | 1:8c48fb8ca5e0 | 83 | { |
haoyan | 1:8c48fb8ca5e0 | 84 | // clear screen, re-draw and refresh |
haoyan | 1:8c48fb8ca5e0 | 85 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 86 | Starcraft.draw(lcd); |
haoyan | 1:8c48fb8ca5e0 | 87 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 88 | } |
haoyan | 1:8c48fb8ca5e0 | 89 | |
haoyan | 1:8c48fb8ca5e0 | 90 | // simple splash screen displayed on start-up |
haoyan | 1:8c48fb8ca5e0 | 91 | void welcome() |
haoyan | 1:8c48fb8ca5e0 | 92 | { |
haoyan | 1:8c48fb8ca5e0 | 93 | lcd.printString("Starcraft!",0,1); |
haoyan | 1:8c48fb8ca5e0 | 94 | lcd.printString("Press Start",0,4); |
haoyan | 1:8c48fb8ca5e0 | 95 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 96 | |
haoyan | 1:8c48fb8ca5e0 | 97 | // wait flashing LEDs until start button is pressed |
haoyan | 1:8c48fb8ca5e0 | 98 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
haoyan | 1:8c48fb8ca5e0 | 99 | pad.leds_on(); |
haoyan | 1:8c48fb8ca5e0 | 100 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 101 | pad.leds_off(); |
haoyan | 1:8c48fb8ca5e0 | 102 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 103 | } |
haoyan | 1:8c48fb8ca5e0 | 104 | |
haoyan | 1:8c48fb8ca5e0 | 105 | } |
haoyan | 1:8c48fb8ca5e0 | 106 | |
haoyan | 1:8c48fb8ca5e0 | 107 | // introduce background story |
haoyan | 1:8c48fb8ca5e0 | 108 | void background() |
haoyan | 1:8c48fb8ca5e0 | 109 | { |
haoyan | 1:8c48fb8ca5e0 | 110 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 111 | lcd.printString(" WARNING!!! ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 112 | lcd.printString(" WARNING!!! ",1,3); |
haoyan | 1:8c48fb8ca5e0 | 113 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 114 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 115 | |
haoyan | 1:8c48fb8ca5e0 | 116 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 117 | lcd.printString(" Swarm invide ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 118 | lcd.printString(" Our galaxy ",1,3); |
haoyan | 1:8c48fb8ca5e0 | 119 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 120 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 121 | |
haoyan | 1:8c48fb8ca5e0 | 122 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 123 | lcd.printString(" Our fleet ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 124 | lcd.printString(" Has been ",1,2); |
haoyan | 1:8c48fb8ca5e0 | 125 | lcd.printString(" Destroyed ",2,3); |
haoyan | 1:8c48fb8ca5e0 | 126 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 127 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 128 | |
haoyan | 1:8c48fb8ca5e0 | 129 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 130 | lcd.printString(" You're the ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 131 | lcd.printString(" Last ",2,2); |
haoyan | 1:8c48fb8ca5e0 | 132 | lcd.printString(" Battleship ",3,4); |
haoyan | 1:8c48fb8ca5e0 | 133 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 134 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 135 | |
haoyan | 1:8c48fb8ca5e0 | 136 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 137 | lcd.printString(" Kill them ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 138 | lcd.printString(" All!!! ",3,3); |
haoyan | 1:8c48fb8ca5e0 | 139 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 140 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 141 | |
haoyan | 1:8c48fb8ca5e0 | 142 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 143 | lcd.printString(" Good luck ",2,2); |
haoyan | 1:8c48fb8ca5e0 | 144 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 145 | wait(2); |
haoyan | 1:8c48fb8ca5e0 | 146 | } |
haoyan | 1:8c48fb8ca5e0 | 147 | |
haoyan | 1:8c48fb8ca5e0 | 148 | // when game over the screen will display the score |
haoyan | 1:8c48fb8ca5e0 | 149 | void score() |
haoyan | 1:8c48fb8ca5e0 | 150 | { |
haoyan | 1:8c48fb8ca5e0 | 151 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 152 | int battleship_score = Starcraft.find_score(); |
haoyan | 1:8c48fb8ca5e0 | 153 | char buffer3[14]; |
haoyan | 1:8c48fb8ca5e0 | 154 | sprintf(buffer3,"%2d",battleship_score); |
haoyan | 1:8c48fb8ca5e0 | 155 | lcd.printString("SCORE",25 ,2); |
haoyan | 1:8c48fb8ca5e0 | 156 | lcd.printString(buffer3,35 ,3); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits |
haoyan | 1:8c48fb8ca5e0 | 157 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 158 | wait(1.5); |
haoyan | 1:8c48fb8ca5e0 | 159 | } |
haoyan | 1:8c48fb8ca5e0 | 160 | |
haoyan | 1:8c48fb8ca5e0 | 161 | void gameover() |
haoyan | 1:8c48fb8ca5e0 | 162 | { |
haoyan | 1:8c48fb8ca5e0 | 163 | lcd.clear(); |
haoyan | 1:8c48fb8ca5e0 | 164 | lcd.printString(" You failed! ",1,1); |
haoyan | 1:8c48fb8ca5e0 | 165 | lcd.printString(" Press back ",1,3); |
haoyan | 1:8c48fb8ca5e0 | 166 | lcd.printString(" to play again ",1,4); |
haoyan | 1:8c48fb8ca5e0 | 167 | lcd.refresh(); |
haoyan | 1:8c48fb8ca5e0 | 168 | |
haoyan | 1:8c48fb8ca5e0 | 169 | while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::BACK_PRESSED) == false) { |
haoyan | 1:8c48fb8ca5e0 | 170 | pad.leds_on(); |
haoyan | 1:8c48fb8ca5e0 | 171 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 172 | pad.leds_off(); |
haoyan | 1:8c48fb8ca5e0 | 173 | wait(0.1); |
haoyan | 1:8c48fb8ca5e0 | 174 | } |
haoyan | 1:8c48fb8ca5e0 | 175 | } |