Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

Committer:
DannyLee
Date:
Sat May 16 17:26:10 2020 +0000
Revision:
8:b4a2954dd74f
Parent:
7:e3844250b77d
Debugging

Who changed what in which revision?

UserRevisionLine numberNew 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 5:e3a9f0548922 5 Coded by Li Ruofan
DannyLee 3:cf9fead9c3f4 6 Username:el17rl
DannyLee 5:e3a9f0548922 7 StudentIDNumber:201199450
DannyLee 5:e3a9f0548922 8 Date:5/10/2020
DannyLee 3:cf9fead9c3f4 9 */
DannyLee 5:e3a9f0548922 10 #include <N5110.h>
DannyLee 5:e3a9f0548922 11 #include <Joystick.h>
DannyLee 3:cf9fead9c3f4 12 #include "shot.h"
DannyLee 5:e3a9f0548922 13 #include "UFO.h"
DannyLee 7:e3844250b77d 14 #include "spaceship.h"
DannyLee 7:e3844250b77d 15 #include "homepage.h"
DannyLee 7:e3844250b77d 16 #include "bgm.h"
DannyLee 3:cf9fead9c3f4 17
DannyLee 5:e3a9f0548922 18 /////////////// objects ///////////////
DannyLee 3:cf9fead9c3f4 19 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
DannyLee 5:e3a9f0548922 20 Joystick joystick(PTB10,PTB11,PTC16);
DannyLee 3:cf9fead9c3f4 21 InterruptIn start(PTC5);
DannyLee 3:cf9fead9c3f4 22 InterruptIn buttonX(PTC17);
DannyLee 3:cf9fead9c3f4 23 InterruptIn buttonY(PTC12);
DannyLee 3:cf9fead9c3f4 24 InterruptIn buttonA(PTB9);
DannyLee 3:cf9fead9c3f4 25 InterruptIn buttonB(PTD0);
DannyLee 3:cf9fead9c3f4 26 BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3); //leds
DannyLee 5:e3a9f0548922 27 Bgm bgm;
DannyLee 3:cf9fead9c3f4 28
DannyLee 5:e3a9f0548922 29 UFO *UFO = NULL;
DannyLee 5:e3a9f0548922 30 Shot *shots[5] = {NULL,NULL,NULL,NULL,NULL};
DannyLee 5:e3a9f0548922 31 Spaceship *spaceship = NULL;
DannyLee 3:cf9fead9c3f4 32
DannyLee 3:cf9fead9c3f4 33 int score[3] = {0,0,0};
DannyLee 3:cf9fead9c3f4 34 int curScore = 0;
DannyLee 3:cf9fead9c3f4 35 int fail = 0;
DannyLee 3:cf9fead9c3f4 36 int count = 0;
DannyLee 3:cf9fead9c3f4 37
DannyLee 3:cf9fead9c3f4 38 ///////////// prototypes ///////////////
DannyLee 3:cf9fead9c3f4 39 void attack();
DannyLee 3:cf9fead9c3f4 40 void move(int signal);
DannyLee 3:cf9fead9c3f4 41 void shoot();
DannyLee 3:cf9fead9c3f4 42 void welcome();
DannyLee 3:cf9fead9c3f4 43 void over();
DannyLee 3:cf9fead9c3f4 44 void updateScore();
DannyLee 3:cf9fead9c3f4 45 void over();
DannyLee 3:cf9fead9c3f4 46 void init();
DannyLee 3:cf9fead9c3f4 47 void updateGame();
DannyLee 3:cf9fead9c3f4 48
DannyLee 3:cf9fead9c3f4 49 ///////////// functions ////////////////
DannyLee 3:cf9fead9c3f4 50 int main(){
DannyLee 3:cf9fead9c3f4 51
DannyLee 3:cf9fead9c3f4 52 init();
DannyLee 3:cf9fead9c3f4 53 while (1) {
DannyLee 5:e3a9f0548922 54 //welcome to the star war
DannyLee 5:e3a9f0548922 55 homepage->welcome(lcd,output,bgm);
DannyLee 5:e3a9f0548922 56 //choose different function
DannyLee 5:e3a9f0548922 57 homepage->homepage(lcd,buttonA,start,score,3);
DannyLee 5:e3a9f0548922 58 spaceship = new Spaceship();
DannyLee 5:e3a9f0548922 59 spaceship->init(37,38,10,10);
DannyLee 5:e3a9f0548922 60 while (start == 0){
DannyLee 3:cf9fead9c3f4 61 }
DannyLee 3:cf9fead9c3f4 62 lcd.clear();
DannyLee 3:cf9fead9c3f4 63
DannyLee 3:cf9fead9c3f4 64 }
DannyLee 3:cf9fead9c3f4 65 }
DannyLee 5:e3a9f0548922 66
DannyLee 5:e3a9f0548922 67 //updating the game
DannyLee 3:cf9fead9c3f4 68 void updateGame(){
DannyLee 5:e3a9f0548922 69 homepage->displayCurScore(lcd,curScore);
DannyLee 3:cf9fead9c3f4 70 Direction d = joystick.get_direction();
DannyLee 5:e3a9f0548922 71 spaceship->update(d);
DannyLee 5:e3a9f0548922 72 spaceship->draw(lcd);
DannyLee 3:cf9fead9c3f4 73 shoot();
DannyLee 3:cf9fead9c3f4 74 attack();
DannyLee 3:cf9fead9c3f4 75 lcd.clear();
DannyLee 3:cf9fead9c3f4 76 }
DannyLee 3:cf9fead9c3f4 77
DannyLee 3:cf9fead9c3f4 78 // initialies buttons, lcd and joystick
DannyLee 3:cf9fead9c3f4 79 void init(){
DannyLee 3:cf9fead9c3f4 80 buttonX.mode(PullDown);
DannyLee 3:cf9fead9c3f4 81 start.mode(PullDown);
DannyLee 3:cf9fead9c3f4 82 buttonA.mode(PullDown);
DannyLee 3:cf9fead9c3f4 83 buttonY.mode(PullDown);
DannyLee 3:cf9fead9c3f4 84 buttonB.mode(PullDown);
DannyLee 3:cf9fead9c3f4 85 joystick.init();
DannyLee 3:cf9fead9c3f4 86 lcd.init();
DannyLee 5:e3a9f0548922 87 homepage = new Homepage();
DannyLee 3:cf9fead9c3f4 88 }
DannyLee 3:cf9fead9c3f4 89
DannyLee 5:e3a9f0548922 90 //game over,
DannyLee 5:e3a9f0548922 91 //clear data, bgm, etc.
DannyLee 3:cf9fead9c3f4 92 void over(){
DannyLee 5:e3a9f0548922 93 bgm->died(bgm);
DannyLee 5:e3a9f0548922 94 homepage->over(lcd,output);
DannyLee 5:e3a9f0548922 95 UFO = NULL;
DannyLee 5:e3a9f0548922 96 for(int i = 0;i<5;i++)shots[i] = NULL;
DannyLee 3:cf9fead9c3f4 97 fail = 0;
DannyLee 3:cf9fead9c3f4 98 count = 0;
DannyLee 3:cf9fead9c3f4 99 updateScore();
DannyLee 3:cf9fead9c3f4 100 curScore = 0;
DannyLee 3:cf9fead9c3f4 101 }
DannyLee 3:cf9fead9c3f4 102
DannyLee 5:e3a9f0548922 103 //show the movement of the shot
DannyLee 3:cf9fead9c3f4 104 //initialize the shot
DannyLee 5:e3a9f0548922 105 //shot moves upwards until the UFO has been slained.
DannyLee 3:cf9fead9c3f4 106 void shoot() {
DannyLee 3:cf9fead9c3f4 107 if (buttonX == 1) {
DannyLee 3:cf9fead9c3f4 108 for(int i = 0;i<5;i++){
DannyLee 5:e3a9f0548922 109 if(shots[i] == NULL){
DannyLee 5:e3a9f0548922 110 shots[i] = new shot();
DannyLee 5:e3a9f0548922 111 shots[i]->init(spaceship->getPos().x+3,35,5,3);
DannyLee 3:cf9fead9c3f4 112 break;
DannyLee 3:cf9fead9c3f4 113 }
DannyLee 3:cf9fead9c3f4 114 }
DannyLee 3:cf9fead9c3f4 115 }
DannyLee 3:cf9fead9c3f4 116 for(int i = 0;i<5;i++){
DannyLee 5:e3a9f0548922 117 if(shots[i]!=NULL){
DannyLee 5:e3a9f0548922 118 shots[i]->draw(lcd);
DannyLee 3:cf9fead9c3f4 119 lcd.refresh();
DannyLee 5:e3a9f0548922 120 if(shots[i]->getPos().x>=UFO->getPos().x && shots[i]->getPos().x<=(UFO->getPos().x+12) && shots[i]->getPos().y<=(UFO->getPos().y+5)){ //being shot
DannyLee 3:cf9fead9c3f4 121 bgm.tone(750.0,0.1);
DannyLee 5:e3a9f0548922 122 shots[i] = NULL;
DannyLee 5:e3a9f0548922 123 UFO->setBlood(1);
DannyLee 5:e3a9f0548922 124 if(UFO->getBlood() <= 0){
DannyLee 5:e3a9f0548922 125 UFO = NULL; //destroy
DannyLee 5:e3a9f0548922 126 curScore+=1; //get score
DannyLee 3:cf9fead9c3f4 127 }
DannyLee 5:e3a9f0548922 128 }else if(shots[i]->getPos().y<=0){
DannyLee 5:e3a9f0548922 129 shots[i] = NULL;
DannyLee 3:cf9fead9c3f4 130 }else {
DannyLee 5:e3a9f0548922 131 shots[i]->update();
DannyLee 3:cf9fead9c3f4 132 }
DannyLee 3:cf9fead9c3f4 133 }
DannyLee 3:cf9fead9c3f4 134 }
DannyLee 3:cf9fead9c3f4 135 }
DannyLee 3:cf9fead9c3f4 136
DannyLee 5:e3a9f0548922 137 //display the movement of the UFO
DannyLee 5:e3a9f0548922 138 //initialize the UFO
DannyLee 5:e3a9f0548922 139 //UFO moves downwards until meets the spaceship
DannyLee 3:cf9fead9c3f4 140 void attack() {
DannyLee 5:e3a9f0548922 141 if(UFO == NULL){
DannyLee 6:cbd9e1f26a10 142 UFO = new UFO;
DannyLee 5:e3a9f0548922 143 UFO->init(12,5);
DannyLee 6:cbd9e1f26a10 144 UFO->draw(lcd);
DannyLee 3:cf9fead9c3f4 145 lcd.refresh();
DannyLee 3:cf9fead9c3f4 146 } else {
DannyLee 5:e3a9f0548922 147 UFO->update();
DannyLee 5:e3a9f0548922 148 if(UFO->getPos().y>=33){
DannyLee 3:cf9fead9c3f4 149 fail = 1;
DannyLee 3:cf9fead9c3f4 150 }else{
DannyLee 6:cbd9e1f26a10 151 UFO->draw(lcd);
DannyLee 3:cf9fead9c3f4 152 lcd.refresh();
DannyLee 3:cf9fead9c3f4 153 }
DannyLee 3:cf9fead9c3f4 154 }
DannyLee 6:cbd9e1f26a10 155 }