deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Tue May 12 15:13:22 2020 +0000
Revision:
3:1db91ad3ab84
Parent:
2:03cd3bb32511
Child:
4:9fa0c5edd1a1
finish2

Who changed what in which revision?

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