Joshua O'hara 201291390

Dependencies:   mbed

Committer:
josh_ohara
Date:
Tue May 26 15:15:46 2020 +0000
Revision:
44:3b904d25ee12
Parent:
43:1ac200335a68
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 0:b7f1f47bb26a 1 /*
eencae 0:b7f1f47bb26a 2 ELEC2645 Embedded Systems Project
eencae 0:b7f1f47bb26a 3 School of Electronic & Electrical Engineering
eencae 0:b7f1f47bb26a 4 University of Leeds
eencae 0:b7f1f47bb26a 5 2019/20
eencae 0:b7f1f47bb26a 6
josh_ohara 1:9b659b3c092b 7 Name: Joshua O'hara
josh_ohara 1:9b659b3c092b 8 Username: el18jkeo
josh_ohara 1:9b659b3c092b 9 Student ID Number: 201291390
josh_ohara 1:9b659b3c092b 10 Date: 11.03.2020
eencae 0:b7f1f47bb26a 11 */
eencae 0:b7f1f47bb26a 12 // includes
eencae 0:b7f1f47bb26a 13 #include "mbed.h"
eencae 0:b7f1f47bb26a 14 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 15 #include "N5110.h"
josh_ohara 8:86cb9a9f8a73 16 #include "SpaceInvaderEngine.h"
eencae 0:b7f1f47bb26a 17
josh_ohara 12:be491ab6e742 18 #define SHIP_WIDTH 9
josh_ohara 8:86cb9a9f8a73 19 #define SHIP_HEIGHT 2
josh_ohara 36:78efa0e7bd31 20 #define ALIEN_SIZE 3
josh_ohara 12:be491ab6e742 21 #define ROW_SIZE 3
josh_ohara 12:be491ab6e742 22 #define COLUMN_SIZE 5
josh_ohara 15:dde4ce4bf7fe 23 #define COVER_Y 38
josh_ohara 15:dde4ce4bf7fe 24 #define COVER1_X 9
josh_ohara 15:dde4ce4bf7fe 25 #define COVER2_X 34
josh_ohara 15:dde4ce4bf7fe 26 #define COVER3_X 59
josh_ohara 15:dde4ce4bf7fe 27 #define ROCK_NUMBER 10
eencae 0:b7f1f47bb26a 28
eencae 0:b7f1f47bb26a 29 // objects
eencae 0:b7f1f47bb26a 30 Gamepad pad;
eencae 0:b7f1f47bb26a 31 N5110 lcd;
josh_ohara 8:86cb9a9f8a73 32 SpaceInvaderEngine SpaceInvader;
josh_ohara 8:86cb9a9f8a73 33
josh_ohara 8:86cb9a9f8a73 34 // structs
josh_ohara 8:86cb9a9f8a73 35 struct UserInput {
josh_ohara 8:86cb9a9f8a73 36 Direction d;
josh_ohara 8:86cb9a9f8a73 37 float mag;
josh_ohara 8:86cb9a9f8a73 38 };
josh_ohara 8:86cb9a9f8a73 39
josh_ohara 8:86cb9a9f8a73 40 //functions
josh_ohara 22:3e978b1d7958 41 void hardware_init();
josh_ohara 22:3e978b1d7958 42 void game_init();
josh_ohara 8:86cb9a9f8a73 43 void draw_game();
josh_ohara 8:86cb9a9f8a73 44 void start_menu();
josh_ohara 22:3e978b1d7958 45 void game_over(int level);
josh_ohara 22:3e978b1d7958 46 void next_level(int level);
josh_ohara 24:ff5af5a013b5 47 void select_difficulty();
josh_ohara 33:d8284dee58db 48 //variables
josh_ohara 33:d8284dee58db 49 int _fps;
josh_ohara 33:d8284dee58db 50 int _counter;
josh_ohara 33:d8284dee58db 51 int _level;
josh_ohara 33:d8284dee58db 52 int _difficulty;
josh_ohara 33:d8284dee58db 53 int _number_of_aliens;
josh_ohara 33:d8284dee58db 54 bool _armada_life;
josh_ohara 33:d8284dee58db 55 bool _ship_life;
josh_ohara 24:ff5af5a013b5 56
josh_ohara 24:ff5af5a013b5 57 Direction direction;
eencae 0:b7f1f47bb26a 58
eencae 0:b7f1f47bb26a 59 int main()
eencae 0:b7f1f47bb26a 60 {
josh_ohara 41:648a271fbbc0 61 _fps = 6; //set frames per second
josh_ohara 41:648a271fbbc0 62 _counter = 0; //game loop counter set to 0
josh_ohara 41:648a271fbbc0 63 _level = 1; //level set
josh_ohara 41:648a271fbbc0 64 _armada_life = true; //armada life flag set
josh_ohara 41:648a271fbbc0 65 _ship_life = true; //ship life flag set
eencae 0:b7f1f47bb26a 66
josh_ohara 41:648a271fbbc0 67 hardware_init(); //initialise gamepad hardware
josh_ohara 41:648a271fbbc0 68 start_menu(); //show player the start menu
josh_ohara 41:648a271fbbc0 69 select_difficulty(); //show player the difficulty select menu
josh_ohara 24:ff5af5a013b5 70
josh_ohara 41:648a271fbbc0 71 _number_of_aliens = 5 + 5*_difficulty; //number of aliens increases with difficulty
josh_ohara 24:ff5af5a013b5 72
josh_ohara 41:648a271fbbc0 73 draw_game(); //draw initial game
josh_ohara 41:648a271fbbc0 74 wait(1.0f/_fps); //wait frame rate
josh_ohara 8:86cb9a9f8a73 75
josh_ohara 41:648a271fbbc0 76 while(1){ //MAIN LOOP
josh_ohara 41:648a271fbbc0 77 hardware_init(); //initialise hardware
josh_ohara 41:648a271fbbc0 78 game_init(); //initialise game engine
josh_ohara 41:648a271fbbc0 79 next_level(_level); //show level on screen
josh_ohara 40:35f27f0e7833 80 //printf(" level == %2d",level);
josh_ohara 37:90a0671d2ba7 81
josh_ohara 41:648a271fbbc0 82 while(1) { //LEVEL LOOP
josh_ohara 41:648a271fbbc0 83 SpaceInvader.read_input(pad); //read player pad input
josh_ohara 41:648a271fbbc0 84 SpaceInvader.update(pad, lcd, _counter, _level); //update engine
josh_ohara 41:648a271fbbc0 85 draw_game(); //draw updated game
josh_ohara 41:648a271fbbc0 86 wait(1.0f/_fps); //wait frame rate
josh_ohara 41:648a271fbbc0 87 _counter++; //increment counter to show 1 loop completed
josh_ohara 41:648a271fbbc0 88 _ship_life = SpaceInvader.get_ship_life(); //get the ship life
josh_ohara 41:648a271fbbc0 89 if(_ship_life == false){ //display game over menu if ship life is false (ship has died)
josh_ohara 37:90a0671d2ba7 90 game_over(_level);
josh_ohara 37:90a0671d2ba7 91 }
josh_ohara 41:648a271fbbc0 92 _armada_life = SpaceInvader.get_armada_life(); //get armada life
josh_ohara 41:648a271fbbc0 93 if(_armada_life == false){ //break from level loop and progress to next level if armada is dead
josh_ohara 37:90a0671d2ba7 94 break;
josh_ohara 37:90a0671d2ba7 95 }
josh_ohara 35:517b56b010df 96 }
josh_ohara 41:648a271fbbc0 97 SpaceInvader.kill_all(); //kill all remaining objects from this level
josh_ohara 41:648a271fbbc0 98 _level++; //increment level
josh_ohara 40:35f27f0e7833 99 //printf(" counter = %2d",counter);
josh_ohara 8:86cb9a9f8a73 100 }
eencae 0:b7f1f47bb26a 101 }
eencae 0:b7f1f47bb26a 102
josh_ohara 33:d8284dee58db 103 void start_menu()
josh_ohara 33:d8284dee58db 104 {
josh_ohara 41:648a271fbbc0 105 lcd.printString("Space Invaders",0,1); //display start menu
josh_ohara 8:86cb9a9f8a73 106 lcd.printString(" Press Start ",0,4);
josh_ohara 8:86cb9a9f8a73 107 lcd.refresh();
josh_ohara 8:86cb9a9f8a73 108
josh_ohara 41:648a271fbbc0 109 while(pad.start_pressed() == false) { //shows start menu until player presses start
josh_ohara 41:648a271fbbc0 110 lcd.setContrast( pad.read_pot1()); //set contrast with pad
josh_ohara 41:648a271fbbc0 111 pad.leds_on(); //flash LEDs
josh_ohara 8:86cb9a9f8a73 112 wait(0.1);
josh_ohara 8:86cb9a9f8a73 113 pad.leds_off();
josh_ohara 8:86cb9a9f8a73 114 wait(0.1);
josh_ohara 8:86cb9a9f8a73 115 }
josh_ohara 8:86cb9a9f8a73 116 }
josh_ohara 8:86cb9a9f8a73 117
josh_ohara 41:648a271fbbc0 118 void draw_game() //draws game engine
josh_ohara 8:86cb9a9f8a73 119 {
josh_ohara 41:648a271fbbc0 120 lcd.clear(); //clear previous frame
josh_ohara 41:648a271fbbc0 121 SpaceInvader.render(lcd); //load new frame
josh_ohara 41:648a271fbbc0 122 lcd.refresh(); //display new frame
josh_ohara 8:86cb9a9f8a73 123 }
josh_ohara 8:86cb9a9f8a73 124
josh_ohara 22:3e978b1d7958 125 void hardware_init()
josh_ohara 8:86cb9a9f8a73 126 {
josh_ohara 41:648a271fbbc0 127 lcd.init(); //initialise hardware
josh_ohara 8:86cb9a9f8a73 128 pad.init();
josh_ohara 21:970807533b10 129 }
josh_ohara 41:648a271fbbc0 130 void game_init() //initialise game engine and subclasses
josh_ohara 21:970807533b10 131 {
josh_ohara 34:853f0cf0ce03 132 SpaceInvader.init(SHIP_HEIGHT,SHIP_WIDTH,ALIEN_SIZE,_number_of_aliens,COLUMN_SIZE,ROW_SIZE,COVER_Y,COVER1_X,COVER2_X,COVER3_X,ROCK_NUMBER,_level);
josh_ohara 8:86cb9a9f8a73 133 }
josh_ohara 8:86cb9a9f8a73 134
josh_ohara 41:648a271fbbc0 135 void game_over(int level) //game over menu function
josh_ohara 33:d8284dee58db 136 {
josh_ohara 22:3e978b1d7958 137 lcd.clear();
josh_ohara 43:1ac200335a68 138 pad.leds_on();
josh_ohara 22:3e978b1d7958 139 while(1){
josh_ohara 41:648a271fbbc0 140 lcd.printString(" Game Over",0,0); //display game over and level until player resets game
josh_ohara 33:d8284dee58db 141 char buffer_[14];
josh_ohara 33:d8284dee58db 142 sprintf(buffer_," Level %2d",level);
josh_ohara 33:d8284dee58db 143 lcd.printString(buffer_,0,2);
josh_ohara 22:3e978b1d7958 144 lcd.printString(" Press reset ",0,4);
josh_ohara 22:3e978b1d7958 145 lcd.refresh();
josh_ohara 22:3e978b1d7958 146 }
josh_ohara 22:3e978b1d7958 147 }
josh_ohara 22:3e978b1d7958 148
josh_ohara 41:648a271fbbc0 149 void next_level(int level) //next level function
josh_ohara 33:d8284dee58db 150 {
josh_ohara 41:648a271fbbc0 151 lcd.clear(); //displays upcoming level number
josh_ohara 33:d8284dee58db 152 char buffer_[14];
josh_ohara 33:d8284dee58db 153 sprintf(buffer_," Level %2d",level);
josh_ohara 33:d8284dee58db 154 lcd.printString(buffer_,0,2);
josh_ohara 22:3e978b1d7958 155 lcd.refresh();
josh_ohara 22:3e978b1d7958 156 wait(2);
josh_ohara 22:3e978b1d7958 157 lcd.clear();
josh_ohara 37:90a0671d2ba7 158 }
josh_ohara 24:ff5af5a013b5 159
josh_ohara 41:648a271fbbc0 160 void select_difficulty() //select difficulty function
josh_ohara 33:d8284dee58db 161 {
josh_ohara 41:648a271fbbc0 162 _difficulty = 1; //initialise difficulty to 1
josh_ohara 41:648a271fbbc0 163 bool select_ = false; //initialise select flag to false
josh_ohara 43:1ac200335a68 164 //select flag indicates if A has been pressed
josh_ohara 43:1ac200335a68 165 while(select_ == false){ //run loop while A has not yet been pressed (i.e. a selection has not yet been made)
josh_ohara 43:1ac200335a68 166 lcd.clear(); //prints difficulty options on screen
josh_ohara 24:ff5af5a013b5 167 lcd.printString(" Easy",0,0);
josh_ohara 24:ff5af5a013b5 168 lcd.printString(" Medium",0,2);
josh_ohara 24:ff5af5a013b5 169 lcd.printString(" Hard",0,4);
josh_ohara 43:1ac200335a68 170 switch (_difficulty){ //switch to draw selection box next to the difficulty the player is currently hovering over
josh_ohara 42:816e444e660b 171 case 1:
josh_ohara 42:816e444e660b 172 lcd.drawRect(0,0,3,3,FILL_BLACK);
josh_ohara 42:816e444e660b 173 break;
josh_ohara 42:816e444e660b 174 case 2:
josh_ohara 42:816e444e660b 175 lcd.drawRect(0,16,3,3,FILL_BLACK);
josh_ohara 42:816e444e660b 176 break;
josh_ohara 42:816e444e660b 177 case 3:
josh_ohara 42:816e444e660b 178 lcd.drawRect(0,32,3,3,FILL_BLACK);
josh_ohara 42:816e444e660b 179 break;
josh_ohara 42:816e444e660b 180 }
josh_ohara 24:ff5af5a013b5 181 lcd.refresh();
josh_ohara 43:1ac200335a68 182 wait(1); //wait statement reduces sensitivity of switch making it usable
josh_ohara 24:ff5af5a013b5 183
josh_ohara 24:ff5af5a013b5 184 while(1){
josh_ohara 24:ff5af5a013b5 185 direction = pad.get_direction();
josh_ohara 43:1ac200335a68 186 if(pad.A_pressed()==true){ //set select flag to true if A is pressed
josh_ohara 33:d8284dee58db 187 select_ = true;
josh_ohara 24:ff5af5a013b5 188 break;
josh_ohara 24:ff5af5a013b5 189 }
josh_ohara 43:1ac200335a68 190 if((_difficulty==1)&& //if player is hovering over easy and moves joystick up then hover over hard
josh_ohara 24:ff5af5a013b5 191 (direction==N)){
josh_ohara 33:d8284dee58db 192 _difficulty = 3;
josh_ohara 24:ff5af5a013b5 193 break;
josh_ohara 24:ff5af5a013b5 194 }
josh_ohara 43:1ac200335a68 195 if((_difficulty==1)&& //if player is hovering over easy and moves joystick down then hover over medium
josh_ohara 24:ff5af5a013b5 196 (direction==S)){
josh_ohara 33:d8284dee58db 197 _difficulty = 2;
josh_ohara 24:ff5af5a013b5 198 break;
josh_ohara 24:ff5af5a013b5 199 }
josh_ohara 43:1ac200335a68 200 if((_difficulty==2)&& //if player is hovering over medium and moves joystick up then hover over easy
josh_ohara 24:ff5af5a013b5 201 (direction==N)){
josh_ohara 33:d8284dee58db 202 _difficulty = 1;
josh_ohara 24:ff5af5a013b5 203 break;
josh_ohara 24:ff5af5a013b5 204 }
josh_ohara 43:1ac200335a68 205 if((_difficulty==2)&& //if player is hovering over medium and moves joystick down then hover over hard
josh_ohara 24:ff5af5a013b5 206 (direction==S)){
josh_ohara 33:d8284dee58db 207 _difficulty = 3;
josh_ohara 24:ff5af5a013b5 208 break;
josh_ohara 24:ff5af5a013b5 209 }
josh_ohara 43:1ac200335a68 210 if((_difficulty==3)&& //if player is hovering over hard and moves joystick up then hover over medium
josh_ohara 24:ff5af5a013b5 211 (direction==N)){
josh_ohara 33:d8284dee58db 212 _difficulty = 2;
josh_ohara 24:ff5af5a013b5 213 break;
josh_ohara 24:ff5af5a013b5 214 }
josh_ohara 43:1ac200335a68 215 if((_difficulty==3)&& //if player is hovering over hard and moves joystick down then hover over easy
josh_ohara 24:ff5af5a013b5 216 (direction==S)){
josh_ohara 33:d8284dee58db 217 _difficulty = 1;
josh_ohara 24:ff5af5a013b5 218 break;
josh_ohara 24:ff5af5a013b5 219 }
josh_ohara 24:ff5af5a013b5 220 }
josh_ohara 24:ff5af5a013b5 221 //printf(" difficulty %2d",direction);
josh_ohara 24:ff5af5a013b5 222 }
josh_ohara 24:ff5af5a013b5 223 }
josh_ohara 24:ff5af5a013b5 224
josh_ohara 24:ff5af5a013b5 225