deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Tue May 12 08:01:52 2020 +0000
Revision:
2:03cd3bb32511
Parent:
1:8c48fb8ca5e0
Child:
3:1db91ad3ab84
dlc1

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