This is the description
Dependencies: mbed Menu Joystick
main.cpp@0:59cbc5800eb6, 2020-04-27 (annotated)
- Committer:
- mrkang
- Date:
- Mon Apr 27 14:04:30 2020 +0000
- Revision:
- 0:59cbc5800eb6
Starwars
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mrkang | 0:59cbc5800eb6 | 1 | #include <Joystick.h> |
mrkang | 0:59cbc5800eb6 | 2 | #include <N5110.h> |
mrkang | 0:59cbc5800eb6 | 3 | #include "Bullet.h" |
mrkang | 0:59cbc5800eb6 | 4 | #include "Enemy.h" |
mrkang | 0:59cbc5800eb6 | 5 | #include "Sound.h" |
mrkang | 0:59cbc5800eb6 | 6 | #include "Plane.h" |
mrkang | 0:59cbc5800eb6 | 7 | #include "Menu.h" |
mrkang | 0:59cbc5800eb6 | 8 | |
mrkang | 0:59cbc5800eb6 | 9 | //objects// |
mrkang | 0:59cbc5800eb6 | 10 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTD11); |
mrkang | 0:59cbc5800eb6 | 11 | Joystick joystick(PTB10,PTB11,PTC16); |
mrkang | 0:59cbc5800eb6 | 12 | InterruptIn start(PTC5); |
mrkang | 0:59cbc5800eb6 | 13 | InterruptIn buttonX(PTC17); |
mrkang | 0:59cbc5800eb6 | 14 | InterruptIn buttonY(PTC12); |
mrkang | 0:59cbc5800eb6 | 15 | InterruptIn buttonA(PTB9); |
mrkang | 0:59cbc5800eb6 | 16 | InterruptIn buttonB(PTD0); |
mrkang | 0:59cbc5800eb6 | 17 | BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3); |
mrkang | 0:59cbc5800eb6 | 18 | Sound sound; |
mrkang | 0:59cbc5800eb6 | 19 | |
mrkang | 0:59cbc5800eb6 | 20 | Enemy *enemy = NULL; |
mrkang | 0:59cbc5800eb6 | 21 | Bullet *bullets[5] = {NULL,NULL,NULL,NULL,NULL}; |
mrkang | 0:59cbc5800eb6 | 22 | Plane *plane = NULL; |
mrkang | 0:59cbc5800eb6 | 23 | Menu *menu = NULL; |
mrkang | 0:59cbc5800eb6 | 24 | |
mrkang | 0:59cbc5800eb6 | 25 | int score[3] = {0,0,0}; |
mrkang | 0:59cbc5800eb6 | 26 | int curScore = 0; |
mrkang | 0:59cbc5800eb6 | 27 | int fail = 0; |
mrkang | 0:59cbc5800eb6 | 28 | int Count = 0; |
mrkang | 0:59cbc5800eb6 | 29 | int mode = 0; |
mrkang | 0:59cbc5800eb6 | 30 | |
mrkang | 0:59cbc5800eb6 | 31 | void atttack(); |
mrkang | 0:59cbc5800eb6 | 32 | void move(int signal); |
mrkang | 0:59cbc5800eb6 | 33 | void shoot(); |
mrkang | 0:59cbc5800eb6 | 34 | void welcome(); |
mrkang | 0:59cbc5800eb6 | 35 | void over(); |
mrkang | 0:59cbc5800eb6 | 36 | void updateScore(); |
mrkang | 0:59cbc5800eb6 | 37 | void over(); |
mrkang | 0:59cbc5800eb6 | 38 | void init(); |
mrkang | 0:59cbc5800eb6 | 39 | void updateGame(); |
mrkang | 0:59cbc5800eb6 | 40 | |
mrkang | 0:59cbc5800eb6 | 41 | int main(){ |
mrkang | 0:59cbc5800eb6 | 42 | init(); |
mrkang | 0:59cbc5800eb6 | 43 | while(1) { |
mrkang | 0:59cbc5800eb6 | 44 | menu->welcome(lcd,output,sound); |
mrkang | 0:59cbc5800eb6 | 45 | menu->menu(lcd,buttonA,buttonY,buttonX,buttonB,start,score,3); |
mrkang | 0:59cbc5800eb6 | 46 | plane = new Plane(); |
mrkang | 0:59cbc5800eb6 | 47 | plane ->init(37,38,10,10); |
mrkang | 0:59cbc5800eb6 | 48 | mode = menu->getMode(); |
mrkang | 0:59cbc5800eb6 | 49 | while(1){ |
mrkang | 0:59cbc5800eb6 | 50 | updateGame(); |
mrkang | 0:59cbc5800eb6 | 51 | if (fail == 1){ |
mrkang | 0:59cbc5800eb6 | 52 | int tmp = menu ->again(lcd,curScore,buttonX,buttonY); |
mrkang | 0:59cbc5800eb6 | 53 | if(tmp == 0) |
mrkang | 0:59cbc5800eb6 | 54 | break; |
mrkang | 0:59cbc5800eb6 | 55 | else{ |
mrkang | 0:59cbc5800eb6 | 56 | curScore+=tmp; |
mrkang | 0:59cbc5800eb6 | 57 | fail = 0; |
mrkang | 0:59cbc5800eb6 | 58 | lcd.clear(); |
mrkang | 0:59cbc5800eb6 | 59 | enemy = NULL; |
mrkang | 0:59cbc5800eb6 | 60 | } |
mrkang | 0:59cbc5800eb6 | 61 | } |
mrkang | 0:59cbc5800eb6 | 62 | Count++; |
mrkang | 0:59cbc5800eb6 | 63 | if(Count %8 == 0)enemy->setSpeed(enemy->getSpeed()+1); |
mrkang | 0:59cbc5800eb6 | 64 | wait(1.0f/3); |
mrkang | 0:59cbc5800eb6 | 65 | } |
mrkang | 0:59cbc5800eb6 | 66 | over(); |
mrkang | 0:59cbc5800eb6 | 67 | while (start == 0 ){ |
mrkang | 0:59cbc5800eb6 | 68 | } |
mrkang | 0:59cbc5800eb6 | 69 | lcd.clear(); |
mrkang | 0:59cbc5800eb6 | 70 | } |
mrkang | 0:59cbc5800eb6 | 71 | } |
mrkang | 0:59cbc5800eb6 | 72 | void updateGame(){ |
mrkang | 0:59cbc5800eb6 | 73 | menu->displayCurScore(lcd,curScore); |
mrkang | 0:59cbc5800eb6 | 74 | Direction d = joystick.get_direction(); |
mrkang | 0:59cbc5800eb6 | 75 | plane->update(d); |
mrkang | 0:59cbc5800eb6 | 76 | plane->draw(lcd); |
mrkang | 0:59cbc5800eb6 | 77 | shoot(); |
mrkang | 0:59cbc5800eb6 | 78 | //attack(); |
mrkang | 0:59cbc5800eb6 | 79 | lcd.clear(); |
mrkang | 0:59cbc5800eb6 | 80 | } |
mrkang | 0:59cbc5800eb6 | 81 | void init(){ |
mrkang | 0:59cbc5800eb6 | 82 | buttonX.mode(PullDown); |
mrkang | 0:59cbc5800eb6 | 83 | start.mode(PullDown); |
mrkang | 0:59cbc5800eb6 | 84 | buttonA.mode(PullDown); |
mrkang | 0:59cbc5800eb6 | 85 | buttonY.mode(PullDown); |
mrkang | 0:59cbc5800eb6 | 86 | buttonB.mode(PullDown); |
mrkang | 0:59cbc5800eb6 | 87 | joystick.init(); |
mrkang | 0:59cbc5800eb6 | 88 | lcd.init(); |
mrkang | 0:59cbc5800eb6 | 89 | menu = new Menu(); |
mrkang | 0:59cbc5800eb6 | 90 | } |
mrkang | 0:59cbc5800eb6 | 91 | void over(){ |
mrkang | 0:59cbc5800eb6 | 92 | sound.tone(1500.0,0.5); |
mrkang | 0:59cbc5800eb6 | 93 | menu->over(lcd,output); |
mrkang | 0:59cbc5800eb6 | 94 | enemy = NULL; |
mrkang | 0:59cbc5800eb6 | 95 | for(int i = 0;i<3;i++)bullets[i]=NULL; |
mrkang | 0:59cbc5800eb6 | 96 | fail = 0; |
mrkang | 0:59cbc5800eb6 | 97 | Count = 0; |
mrkang | 0:59cbc5800eb6 | 98 | updateScore(); |
mrkang | 0:59cbc5800eb6 | 99 | curScore = 0; |
mrkang | 0:59cbc5800eb6 | 100 | } |
mrkang | 0:59cbc5800eb6 | 101 | void updateScore(){ |
mrkang | 0:59cbc5800eb6 | 102 | for(int i = 0;i<3;i++){ |
mrkang | 0:59cbc5800eb6 | 103 | for(int j = 2;j>i;j--){ |
mrkang | 0:59cbc5800eb6 | 104 | score[j] = score[j-1]; |
mrkang | 0:59cbc5800eb6 | 105 | } |
mrkang | 0:59cbc5800eb6 | 106 | score[i] = curScore; |
mrkang | 0:59cbc5800eb6 | 107 | break; |
mrkang | 0:59cbc5800eb6 | 108 | } |
mrkang | 0:59cbc5800eb6 | 109 | } |
mrkang | 0:59cbc5800eb6 | 110 | void shoot() { |
mrkang | 0:59cbc5800eb6 | 111 | if(buttonX == 1){ |
mrkang | 0:59cbc5800eb6 | 112 | for(int i=0;i<5;i++){ |
mrkang | 0:59cbc5800eb6 | 113 | if(bullets[i] == NULL){ |
mrkang | 0:59cbc5800eb6 | 114 | bullets[i] = new Bullet(); |
mrkang | 0:59cbc5800eb6 | 115 | bullets[i] ->init(plane->getPos().x+3,35,5,3); |
mrkang | 0:59cbc5800eb6 | 116 | break; |
mrkang | 0:59cbc5800eb6 | 117 | } |
mrkang | 0:59cbc5800eb6 | 118 | } |
mrkang | 0:59cbc5800eb6 | 119 | } |
mrkang | 0:59cbc5800eb6 | 120 | for(int i=0;i<5;i++){ |
mrkang | 0:59cbc5800eb6 | 121 | if(bullets[i]!=NULL){ |
mrkang | 0:59cbc5800eb6 | 122 | bullets[i]->draw(lcd); |
mrkang | 0:59cbc5800eb6 | 123 | lcd.refresh(); |
mrkang | 0:59cbc5800eb6 | 124 | if(bullets[i]->getPos().x>=enemy->getPos().x && bullets[i]->getPos().x<=(enemy->getPos().x+12)&&bullets[i]->getPos().y<=(enemy->getPos().y+5)){ |
mrkang | 0:59cbc5800eb6 | 125 | sound.tone(750.0,0.2); |
mrkang | 0:59cbc5800eb6 | 126 | bullets[i]=NULL; |
mrkang | 0:59cbc5800eb6 | 127 | enemy->setBlood(1); |
mrkang | 0:59cbc5800eb6 | 128 | if(enemy->getBlood()<=0){ |
mrkang | 0:59cbc5800eb6 | 129 | enemy = NULL; |
mrkang | 0:59cbc5800eb6 | 130 | curScore+=((mode+1)*2); |
mrkang | 0:59cbc5800eb6 | 131 | } |
mrkang | 0:59cbc5800eb6 | 132 | }else if(bullets[i]->getPos().y<=0){ |
mrkang | 0:59cbc5800eb6 | 133 | bullets[i] = NULL; |
mrkang | 0:59cbc5800eb6 | 134 | }else { |
mrkang | 0:59cbc5800eb6 | 135 | bullets[i] ->update(); |
mrkang | 0:59cbc5800eb6 | 136 | } |
mrkang | 0:59cbc5800eb6 | 137 | } |
mrkang | 0:59cbc5800eb6 | 138 | } |
mrkang | 0:59cbc5800eb6 | 139 | } |
mrkang | 0:59cbc5800eb6 | 140 | void attack(){ |
mrkang | 0:59cbc5800eb6 | 141 | if(enemy == NULL){ |
mrkang | 0:59cbc5800eb6 | 142 | enemy = new Enemy(mode); |
mrkang | 0:59cbc5800eb6 | 143 | enemy ->init(12,5); |
mrkang | 0:59cbc5800eb6 | 144 | enemy ->draw(lcd,mode); |
mrkang | 0:59cbc5800eb6 | 145 | lcd.refresh(); |
mrkang | 0:59cbc5800eb6 | 146 | }else{ |
mrkang | 0:59cbc5800eb6 | 147 | enemy->update(); |
mrkang | 0:59cbc5800eb6 | 148 | if(enemy->getPos().y>=33){ |
mrkang | 0:59cbc5800eb6 | 149 | fail = 1; |
mrkang | 0:59cbc5800eb6 | 150 | }else{ |
mrkang | 0:59cbc5800eb6 | 151 | enemy->draw(lcd,mode); |
mrkang | 0:59cbc5800eb6 | 152 | lcd.refresh(); |
mrkang | 0:59cbc5800eb6 | 153 | } |
mrkang | 0:59cbc5800eb6 | 154 | } |
mrkang | 0:59cbc5800eb6 | 155 | } |
mrkang | 0:59cbc5800eb6 | 156 |