Li Ruofan 201199450
Dependencies: mbed Gamepad Joystick
main.cpp@4:1ebf8b8842e0, 2020-05-15 (annotated)
- Committer:
- DannyLee
- Date:
- Fri May 15 01:25:16 2020 +0000
- Revision:
- 4:1ebf8b8842e0
- Parent:
- 3:cf9fead9c3f4
- Child:
- 5:e3a9f0548922
without a complete main
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DannyLee | 3:cf9fead9c3f4 | 1 | /* |
DannyLee | 3:cf9fead9c3f4 | 2 | ELEC2645 Embedded Systems Project |
DannyLee | 3:cf9fead9c3f4 | 3 | School of Electronic & Electrical Engineering |
DannyLee | 3:cf9fead9c3f4 | 4 | University of Leeds |
DannyLee | 3:cf9fead9c3f4 | 5 | Name:Li Ruofan |
DannyLee | 3:cf9fead9c3f4 | 6 | Username:el17rl |
DannyLee | 3:cf9fead9c3f4 | 7 | Student ID Number:201199450 |
DannyLee | 3:cf9fead9c3f4 | 8 | Date:12 May 2020 |
DannyLee | 3:cf9fead9c3f4 | 9 | */ |
DannyLee | 3:cf9fead9c3f4 | 10 | #include "mbed.h" |
DannyLee | 3:cf9fead9c3f4 | 11 | #include "N5110.h" |
DannyLee | 3:cf9fead9c3f4 | 12 | #include "Gamepad.h" |
DannyLee | 3:cf9fead9c3f4 | 13 | #include "shot.h" |
DannyLee | 3:cf9fead9c3f4 | 14 | #include "Ufo.h" |
DannyLee | 3:cf9fead9c3f4 | 15 | #include "Spaceship.h" |
DannyLee | 3:cf9fead9c3f4 | 16 | #include "bgm.h" |
DannyLee | 3:cf9fead9c3f4 | 17 | |
DannyLee | 3:cf9fead9c3f4 | 18 | #define SPACESHIP_WIDTH 5 |
DannyLee | 3:cf9fead9c3f4 | 19 | #define SPACESHIP_HEIGHT 6 |
DannyLee | 3:cf9fead9c3f4 | 20 | #define UFO_SIZE 4 |
DannyLee | 3:cf9fead9c3f4 | 21 | #define UFO_SPEED 1 |
DannyLee | 3:cf9fead9c3f4 | 22 | #define SPACESHIP_LIFE 2 |
DannyLee | 3:cf9fead9c3f4 | 23 | |
DannyLee | 4:1ebf8b8842e0 | 24 | struct UserInput { |
DannyLee | 4:1ebf8b8842e0 | 25 | Direction d; |
DannyLee | 4:1ebf8b8842e0 | 26 | float mag; |
DannyLee | 4:1ebf8b8842e0 | 27 | }; |
DannyLee | 4:1ebf8b8842e0 | 28 | |
DannyLee | 3:cf9fead9c3f4 | 29 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
DannyLee | 4:1ebf8b8842e0 | 30 | Gamepad pad; |
DannyLee | 4:1ebf8b8842e0 | 31 | Spaceship Spaceship; |
DannyLee | 4:1ebf8b8842e0 | 32 | |
DannyLee | 3:cf9fead9c3f4 | 33 | InterruptIn start(PTC5); |
DannyLee | 3:cf9fead9c3f4 | 34 | InterruptIn buttonX(PTC17); |
DannyLee | 3:cf9fead9c3f4 | 35 | InterruptIn buttonY(PTC12); |
DannyLee | 3:cf9fead9c3f4 | 36 | InterruptIn buttonA(PTB9); |
DannyLee | 3:cf9fead9c3f4 | 37 | InterruptIn buttonB(PTD0); |
DannyLee | 3:cf9fead9c3f4 | 38 | BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3); //leds |
DannyLee | 3:cf9fead9c3f4 | 39 | bgm bgm; |
DannyLee | 3:cf9fead9c3f4 | 40 | |
DannyLee | 3:cf9fead9c3f4 | 41 | Ufo *Ufo = NULL; |
DannyLee | 3:cf9fead9c3f4 | 42 | shot *shot[5] = {NULL,NULL,NULL,NULL,NULL}; |
DannyLee | 3:cf9fead9c3f4 | 43 | Spaceship *Spaceship = NULL; |
DannyLee | 3:cf9fead9c3f4 | 44 | Homepage *Homepage = NULL; |
DannyLee | 3:cf9fead9c3f4 | 45 | |
DannyLee | 3:cf9fead9c3f4 | 46 | int score[3] = {0,0,0}; |
DannyLee | 3:cf9fead9c3f4 | 47 | int curScore = 0; |
DannyLee | 3:cf9fead9c3f4 | 48 | int fail = 0; |
DannyLee | 3:cf9fead9c3f4 | 49 | int count = 0; |
DannyLee | 3:cf9fead9c3f4 | 50 | int mode = 0; |
DannyLee | 3:cf9fead9c3f4 | 51 | |
DannyLee | 3:cf9fead9c3f4 | 52 | ///////////// prototypes /////////////// |
DannyLee | 3:cf9fead9c3f4 | 53 | void attack(); |
DannyLee | 3:cf9fead9c3f4 | 54 | void move(int signal); |
DannyLee | 3:cf9fead9c3f4 | 55 | void shoot(); |
DannyLee | 3:cf9fead9c3f4 | 56 | void welcome(); |
DannyLee | 3:cf9fead9c3f4 | 57 | void over(); |
DannyLee | 3:cf9fead9c3f4 | 58 | void updateScore(); |
DannyLee | 3:cf9fead9c3f4 | 59 | void over(); |
DannyLee | 3:cf9fead9c3f4 | 60 | void init(); |
DannyLee | 3:cf9fead9c3f4 | 61 | void updateGame(); |
DannyLee | 3:cf9fead9c3f4 | 62 | |
DannyLee | 3:cf9fead9c3f4 | 63 | ///////////// functions //////////////// |
DannyLee | 3:cf9fead9c3f4 | 64 | int main(){ |
DannyLee | 3:cf9fead9c3f4 | 65 | |
DannyLee | 3:cf9fead9c3f4 | 66 | init(); |
DannyLee | 3:cf9fead9c3f4 | 67 | |
DannyLee | 3:cf9fead9c3f4 | 68 | while (1) { |
DannyLee | 3:cf9fead9c3f4 | 69 | //welcome to the star war |
DannyLee | 3:cf9fead9c3f4 | 70 | Homepage->welcome(lcd,output,bgm); |
DannyLee | 3:cf9fead9c3f4 | 71 | //choose different function |
DannyLee | 3:cf9fead9c3f4 | 72 | Homepage->Homepage(lcd,buttonA,buttonY,buttonX,buttonB,start,score,3); |
DannyLee | 3:cf9fead9c3f4 | 73 | Spaceship = new Spaceship(); |
DannyLee | 3:cf9fead9c3f4 | 74 | Spaceship->init(37,38,10,10); |
DannyLee | 3:cf9fead9c3f4 | 75 | //choose the mode: easy, normal, defficult |
DannyLee | 3:cf9fead9c3f4 | 76 | mode = Homepage->getMode(); |
DannyLee | 3:cf9fead9c3f4 | 77 | while(1){ |
DannyLee | 3:cf9fead9c3f4 | 78 | //playing... |
DannyLee | 3:cf9fead9c3f4 | 79 | updateGame(); |
DannyLee | 3:cf9fead9c3f4 | 80 | //failing, check weather playing again |
DannyLee | 3:cf9fead9c3f4 | 81 | if (fail == 1) { |
DannyLee | 3:cf9fead9c3f4 | 82 | int tmp = Homepage->again(lcd,curScore,buttonX,buttonY); |
DannyLee | 3:cf9fead9c3f4 | 83 | if(tmp == 0) |
DannyLee | 3:cf9fead9c3f4 | 84 | //do not play again |
DannyLee | 3:cf9fead9c3f4 | 85 | break; |
DannyLee | 3:cf9fead9c3f4 | 86 | else{ |
DannyLee | 3:cf9fead9c3f4 | 87 | //play again |
DannyLee | 3:cf9fead9c3f4 | 88 | curScore+=tmp; |
DannyLee | 3:cf9fead9c3f4 | 89 | fail = 0; |
DannyLee | 3:cf9fead9c3f4 | 90 | lcd.clear(); |
DannyLee | 3:cf9fead9c3f4 | 91 | Ufo = NULL; |
DannyLee | 3:cf9fead9c3f4 | 92 | } |
DannyLee | 3:cf9fead9c3f4 | 93 | } |
DannyLee | 3:cf9fead9c3f4 | 94 | count++; |
DannyLee | 3:cf9fead9c3f4 | 95 | //change Ufo's speed |
DannyLee | 3:cf9fead9c3f4 | 96 | if(count %8 == 0)Ufo->setSpeed(Ufo->getSpeed()+1); |
DannyLee | 3:cf9fead9c3f4 | 97 | wait(1.0f/3); |
DannyLee | 3:cf9fead9c3f4 | 98 | } |
DannyLee | 3:cf9fead9c3f4 | 99 | over(); |
DannyLee | 3:cf9fead9c3f4 | 100 | while (start == 0){ |
DannyLee | 3:cf9fead9c3f4 | 101 | } |
DannyLee | 3:cf9fead9c3f4 | 102 | lcd.clear(); |
DannyLee | 3:cf9fead9c3f4 | 103 | |
DannyLee | 3:cf9fead9c3f4 | 104 | } |
DannyLee | 3:cf9fead9c3f4 | 105 | } |
DannyLee | 3:cf9fead9c3f4 | 106 | //this function is used to update a game, such as |
DannyLee | 3:cf9fead9c3f4 | 107 | //display the current score, draw Spaceship, shot ect. |
DannyLee | 3:cf9fead9c3f4 | 108 | void updateGame(){ |
DannyLee | 3:cf9fead9c3f4 | 109 | Homepage->displayCurScore(lcd,curScore); |
DannyLee | 3:cf9fead9c3f4 | 110 | Direction d = joystick.get_direction(); |
DannyLee | 3:cf9fead9c3f4 | 111 | Spaceship->update(d); |
DannyLee | 3:cf9fead9c3f4 | 112 | Spaceship->draw(lcd); |
DannyLee | 3:cf9fead9c3f4 | 113 | shoot(); |
DannyLee | 3:cf9fead9c3f4 | 114 | attack(); |
DannyLee | 3:cf9fead9c3f4 | 115 | lcd.clear(); |
DannyLee | 3:cf9fead9c3f4 | 116 | } |
DannyLee | 3:cf9fead9c3f4 | 117 | |
DannyLee | 3:cf9fead9c3f4 | 118 | // initialies buttons, lcd and joystick |
DannyLee | 3:cf9fead9c3f4 | 119 | void init(){ |
DannyLee | 3:cf9fead9c3f4 | 120 | buttonX.mode(PullDown); |
DannyLee | 3:cf9fead9c3f4 | 121 | start.mode(PullDown); |
DannyLee | 3:cf9fead9c3f4 | 122 | buttonA.mode(PullDown); |
DannyLee | 3:cf9fead9c3f4 | 123 | buttonY.mode(PullDown); |
DannyLee | 3:cf9fead9c3f4 | 124 | buttonB.mode(PullDown); |
DannyLee | 3:cf9fead9c3f4 | 125 | joystick.init(); |
DannyLee | 3:cf9fead9c3f4 | 126 | lcd.init(); |
DannyLee | 3:cf9fead9c3f4 | 127 | Homepage = new Homepage(); |
DannyLee | 3:cf9fead9c3f4 | 128 | } |
DannyLee | 3:cf9fead9c3f4 | 129 | |
DannyLee | 3:cf9fead9c3f4 | 130 | //do something about game over, |
DannyLee | 3:cf9fead9c3f4 | 131 | //clear data, Bgm, etc. |
DannyLee | 3:cf9fead9c3f4 | 132 | void over(){ |
DannyLee | 3:cf9fead9c3f4 | 133 | bgm.tone(1500.0,0.5); |
DannyLee | 3:cf9fead9c3f4 | 134 | Homepage->over(lcd,output); |
DannyLee | 3:cf9fead9c3f4 | 135 | Ufo = NULL; |
DannyLee | 3:cf9fead9c3f4 | 136 | for(int i = 0;i<5;i++)shot[i] = NULL; |
DannyLee | 3:cf9fead9c3f4 | 137 | fail = 0; |
DannyLee | 3:cf9fead9c3f4 | 138 | count = 0; |
DannyLee | 3:cf9fead9c3f4 | 139 | updateScore(); |
DannyLee | 3:cf9fead9c3f4 | 140 | curScore = 0; |
DannyLee | 3:cf9fead9c3f4 | 141 | } |
DannyLee | 3:cf9fead9c3f4 | 142 | |
DannyLee | 3:cf9fead9c3f4 | 143 | //update top 3 history scores when game over |
DannyLee | 3:cf9fead9c3f4 | 144 | void updateScore(){ |
DannyLee | 3:cf9fead9c3f4 | 145 | for(int i = 0;i<3;i++){ |
DannyLee | 3:cf9fead9c3f4 | 146 | if(score[i]<curScore){ |
DannyLee | 3:cf9fead9c3f4 | 147 | //score[i] = curScore; |
DannyLee | 3:cf9fead9c3f4 | 148 | for(int j = 2;j>i;j--){ |
DannyLee | 3:cf9fead9c3f4 | 149 | score[j] = score[j-1]; |
DannyLee | 3:cf9fead9c3f4 | 150 | } |
DannyLee | 3:cf9fead9c3f4 | 151 | score[i] = curScore; |
DannyLee | 3:cf9fead9c3f4 | 152 | break; |
DannyLee | 3:cf9fead9c3f4 | 153 | } |
DannyLee | 3:cf9fead9c3f4 | 154 | } |
DannyLee | 3:cf9fead9c3f4 | 155 | } |
DannyLee | 3:cf9fead9c3f4 | 156 | |
DannyLee | 3:cf9fead9c3f4 | 157 | //display the movement of the shot |
DannyLee | 3:cf9fead9c3f4 | 158 | //initialize the shot |
DannyLee | 3:cf9fead9c3f4 | 159 | //shot moves upwards until the Ufo's blood ran out. |
DannyLee | 3:cf9fead9c3f4 | 160 | void shoot() { |
DannyLee | 3:cf9fead9c3f4 | 161 | if (buttonX == 1) { |
DannyLee | 3:cf9fead9c3f4 | 162 | for(int i = 0;i<5;i++){ |
DannyLee | 3:cf9fead9c3f4 | 163 | if(shot[i] == NULL){ |
DannyLee | 3:cf9fead9c3f4 | 164 | shot[i] = new shot(); |
DannyLee | 3:cf9fead9c3f4 | 165 | shot[i]->init(Spaceship->getPos().x+3,35,5,3); |
DannyLee | 3:cf9fead9c3f4 | 166 | break; |
DannyLee | 3:cf9fead9c3f4 | 167 | } |
DannyLee | 3:cf9fead9c3f4 | 168 | } |
DannyLee | 3:cf9fead9c3f4 | 169 | } |
DannyLee | 3:cf9fead9c3f4 | 170 | for(int i = 0;i<5;i++){ |
DannyLee | 3:cf9fead9c3f4 | 171 | if(shot[i]!=NULL){ |
DannyLee | 3:cf9fead9c3f4 | 172 | shot[i]->draw(lcd); |
DannyLee | 3:cf9fead9c3f4 | 173 | lcd.refresh(); |
DannyLee | 3:cf9fead9c3f4 | 174 | if(shot[i]->getPos().x>=Ufo->getPos().x && shot[i]->getPos().x<=(Ufo->getPos().x+12) && shot[i]->getPos().y<=(Ufo->getPos().y+5)){ //being shot |
DannyLee | 3:cf9fead9c3f4 | 175 | bgm.tone(750.0,0.1); |
DannyLee | 3:cf9fead9c3f4 | 176 | shot[i] = NULL; |
DannyLee | 3:cf9fead9c3f4 | 177 | Ufo->setBlood(1); |
DannyLee | 3:cf9fead9c3f4 | 178 | if(Ufo->getBlood() <= 0){ |
DannyLee | 3:cf9fead9c3f4 | 179 | Ufo = NULL; //destroy |
DannyLee | 3:cf9fead9c3f4 | 180 | curScore+=((mode+1)*2); //get points according to the mode |
DannyLee | 3:cf9fead9c3f4 | 181 | } |
DannyLee | 3:cf9fead9c3f4 | 182 | }else if(shot[i]->getPos().y<=0){ |
DannyLee | 3:cf9fead9c3f4 | 183 | shot[i] = NULL; |
DannyLee | 3:cf9fead9c3f4 | 184 | }else { |
DannyLee | 3:cf9fead9c3f4 | 185 | shot[i]->update(); |
DannyLee | 3:cf9fead9c3f4 | 186 | } |
DannyLee | 3:cf9fead9c3f4 | 187 | } |
DannyLee | 3:cf9fead9c3f4 | 188 | } |
DannyLee | 3:cf9fead9c3f4 | 189 | } |
DannyLee | 3:cf9fead9c3f4 | 190 | |
DannyLee | 3:cf9fead9c3f4 | 191 | //display the movement of the Ufo |
DannyLee | 3:cf9fead9c3f4 | 192 | //initialize the Ufo |
DannyLee | 3:cf9fead9c3f4 | 193 | //Ufo moves downwards until meets the Spaceship |
DannyLee | 3:cf9fead9c3f4 | 194 | void attack() { |
DannyLee | 3:cf9fead9c3f4 | 195 | if(Ufo == NULL){ |
DannyLee | 3:cf9fead9c3f4 | 196 | Ufo = new Ufo(mode); |
DannyLee | 3:cf9fead9c3f4 | 197 | Ufo->init(12,5); |
DannyLee | 3:cf9fead9c3f4 | 198 | Ufo->draw(lcd,mode); |
DannyLee | 3:cf9fead9c3f4 | 199 | lcd.refresh(); |
DannyLee | 3:cf9fead9c3f4 | 200 | } else { |
DannyLee | 3:cf9fead9c3f4 | 201 | Ufo->update(); |
DannyLee | 3:cf9fead9c3f4 | 202 | if(Ufo->getPos().y>=33){ |
DannyLee | 3:cf9fead9c3f4 | 203 | fail = 1; |
DannyLee | 3:cf9fead9c3f4 | 204 | }else{ |
DannyLee | 3:cf9fead9c3f4 | 205 | Ufo->draw(lcd,mode); |
DannyLee | 3:cf9fead9c3f4 | 206 | lcd.refresh(); |
DannyLee | 3:cf9fead9c3f4 | 207 | } |
DannyLee | 3:cf9fead9c3f4 | 208 | } |
DannyLee | 3:cf9fead9c3f4 | 209 | } |
DannyLee | 3:cf9fead9c3f4 | 210 | |
DannyLee | 3:cf9fead9c3f4 | 211 | |
DannyLee | 3:cf9fead9c3f4 | 212 | |
DannyLee | 3:cf9fead9c3f4 | 213 | |
DannyLee | 3:cf9fead9c3f4 | 214 |