Hao zhang
/
el17h3z
HaoZhang SID: 201199702
main.cpp@0:45ce241d316b, 2020-05-14 (annotated)
- Committer:
- zh870524589
- Date:
- Thu May 14 17:26:41 2020 +0000
- Revision:
- 0:45ce241d316b
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zh870524589 | 0:45ce241d316b | 1 | /* |
zh870524589 | 0:45ce241d316b | 2 | ELEC2645 Embedded Systems Project |
zh870524589 | 0:45ce241d316b | 3 | School of Electronic & Electrical Engineering |
zh870524589 | 0:45ce241d316b | 4 | University of Leeds |
zh870524589 | 0:45ce241d316b | 5 | Name:HaoZhang |
zh870524589 | 0:45ce241d316b | 6 | Username: el17h3z |
zh870524589 | 0:45ce241d316b | 7 | Student ID Number:201199702 |
zh870524589 | 0:45ce241d316b | 8 | Date:14th May 2020 |
zh870524589 | 0:45ce241d316b | 9 | */ |
zh870524589 | 0:45ce241d316b | 10 | |
zh870524589 | 0:45ce241d316b | 11 | #include "list" |
zh870524589 | 0:45ce241d316b | 12 | #include "mbed.h" |
zh870524589 | 0:45ce241d316b | 13 | #include "N5110.h" |
zh870524589 | 0:45ce241d316b | 14 | #include "Gamepad.h" |
zh870524589 | 0:45ce241d316b | 15 | #include "ETank.h" |
zh870524589 | 0:45ce241d316b | 16 | #include "MyTank.h" |
zh870524589 | 0:45ce241d316b | 17 | #include "Bullet.h" |
zh870524589 | 0:45ce241d316b | 18 | #include "Map.h" |
zh870524589 | 0:45ce241d316b | 19 | #include "time.h" |
zh870524589 | 0:45ce241d316b | 20 | #include "string" |
zh870524589 | 0:45ce241d316b | 21 | using namespace std; |
zh870524589 | 0:45ce241d316b | 22 | // the levels for the game |
zh870524589 | 0:45ce241d316b | 23 | enum LV{ |
zh870524589 | 0:45ce241d316b | 24 | Menu, |
zh870524589 | 0:45ce241d316b | 25 | map_edit, |
zh870524589 | 0:45ce241d316b | 26 | Guide, |
zh870524589 | 0:45ce241d316b | 27 | Advice, |
zh870524589 | 0:45ce241d316b | 28 | level1, |
zh870524589 | 0:45ce241d316b | 29 | level2, |
zh870524589 | 0:45ce241d316b | 30 | level3, |
zh870524589 | 0:45ce241d316b | 31 | level4, |
zh870524589 | 0:45ce241d316b | 32 | level5, |
zh870524589 | 0:45ce241d316b | 33 | level6, |
zh870524589 | 0:45ce241d316b | 34 | level7, |
zh870524589 | 0:45ce241d316b | 35 | level8, |
zh870524589 | 0:45ce241d316b | 36 | level9, |
zh870524589 | 0:45ce241d316b | 37 | SHOP, |
zh870524589 | 0:45ce241d316b | 38 | GAME_OVER, |
zh870524589 | 0:45ce241d316b | 39 | Win |
zh870524589 | 0:45ce241d316b | 40 | }; |
zh870524589 | 0:45ce241d316b | 41 | |
zh870524589 | 0:45ce241d316b | 42 | // the graph of the menu |
zh870524589 | 0:45ce241d316b | 43 | const int menu_tank[28][28] = |
zh870524589 | 0:45ce241d316b | 44 | { |
zh870524589 | 0:45ce241d316b | 45 | {0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, |
zh870524589 | 0:45ce241d316b | 46 | {0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, |
zh870524589 | 0:45ce241d316b | 47 | {0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, |
zh870524589 | 0:45ce241d316b | 48 | {0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 49 | {0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1}, |
zh870524589 | 0:45ce241d316b | 50 | {0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 51 | {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0}, |
zh870524589 | 0:45ce241d316b | 52 | {0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0}, |
zh870524589 | 0:45ce241d316b | 53 | {0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,0,0,0}, |
zh870524589 | 0:45ce241d316b | 54 | {0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0}, |
zh870524589 | 0:45ce241d316b | 55 | {0,0,0,0,1,1,1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1,1,1,0,0}, |
zh870524589 | 0:45ce241d316b | 56 | {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0}, |
zh870524589 | 0:45ce241d316b | 57 | {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0}, |
zh870524589 | 0:45ce241d316b | 58 | {0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0}, |
zh870524589 | 0:45ce241d316b | 59 | {0,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1}, |
zh870524589 | 0:45ce241d316b | 60 | {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, |
zh870524589 | 0:45ce241d316b | 61 | {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, |
zh870524589 | 0:45ce241d316b | 62 | {0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,0,1}, |
zh870524589 | 0:45ce241d316b | 63 | {0,0,0,1,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0}, |
zh870524589 | 0:45ce241d316b | 64 | {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, |
zh870524589 | 0:45ce241d316b | 65 | {0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, |
zh870524589 | 0:45ce241d316b | 66 | }; |
zh870524589 | 0:45ce241d316b | 67 | |
zh870524589 | 0:45ce241d316b | 68 | DigitalIn button_A(PTB9); |
zh870524589 | 0:45ce241d316b | 69 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
zh870524589 | 0:45ce241d316b | 70 | N5110 lcd2; //Store map |
zh870524589 | 0:45ce241d316b | 71 | Gamepad pad; |
zh870524589 | 0:45ce241d316b | 72 | MyTank myTank; |
zh870524589 | 0:45ce241d316b | 73 | Map map; |
zh870524589 | 0:45ce241d316b | 74 | list<Bullet*>tbullet;//Store the bullet |
zh870524589 | 0:45ce241d316b | 75 | list<ETank*>etank;//Store the ETank(enemy) |
zh870524589 | 0:45ce241d316b | 76 | Vector2D v; |
zh870524589 | 0:45ce241d316b | 77 | |
zh870524589 | 0:45ce241d316b | 78 | int num_etank;// num_etank: represents the number of simultaneous occurrences on the screen |
zh870524589 | 0:45ce241d316b | 79 | int total_num;// total_num:It is the total number of ETank |
zh870524589 | 0:45ce241d316b | 80 | int et_move; //Computer tank's movement probability |
zh870524589 | 0:45ce241d316b | 81 | int attack_gap; |
zh870524589 | 0:45ce241d316b | 82 | int level_check;//for the shop to identify levels |
zh870524589 | 0:45ce241d316b | 83 | int num_buy_check;//for the shop to identify the number that user buy |
zh870524589 | 0:45ce241d316b | 84 | int pages = 0;// the page of guide |
zh870524589 | 0:45ce241d316b | 85 | |
zh870524589 | 0:45ce241d316b | 86 | const int fps = 8; |
zh870524589 | 0:45ce241d316b | 87 | /** run the main part of game |
zh870524589 | 0:45ce241d316b | 88 | * @param num_etank - represents the number of simultaneous occurrences on the screen |
zh870524589 | 0:45ce241d316b | 89 | * @param limit - represents the number of simultaneous occurrences allowed on the screen |
zh870524589 | 0:45ce241d316b | 90 | * @param total_num - total_num:It is the total number of ETank |
zh870524589 | 0:45ce241d316b | 91 | * @param attack_gap - attack interval |
zh870524589 | 0:45ce241d316b | 92 | */ |
zh870524589 | 0:45ce241d316b | 93 | void render(int &num_etank,int limit,int &total_num,int attack_gap); |
zh870524589 | 0:45ce241d316b | 94 | /** Initialize */ |
zh870524589 | 0:45ce241d316b | 95 | void init(); |
zh870524589 | 0:45ce241d316b | 96 | /** Create the ETank |
zh870524589 | 0:45ce241d316b | 97 | *@param etank - the enemy's tanks |
zh870524589 | 0:45ce241d316b | 98 | *@param lcd - Target Screen |
zh870524589 | 0:45ce241d316b | 99 | *@param pad - set the tone when the tank create |
zh870524589 | 0:45ce241d316b | 100 | *@param num_etank - represents the number of simultaneous occurrences on the screen |
zh870524589 | 0:45ce241d316b | 101 | *@param num_limit - Represents the number of simultaneous occurrences allowed on the screen |
zh870524589 | 0:45ce241d316b | 102 | *@param total_num - A total of enemy's tanks were created |
zh870524589 | 0:45ce241d316b | 103 | */ |
zh870524589 | 0:45ce241d316b | 104 | void createtank(list<ETank*>&etank,N5110 &lcd,Gamepad &pad,int &num_etank,int num_limit,int &total_num); |
zh870524589 | 0:45ce241d316b | 105 | /**the different level of the game to run |
zh870524589 | 0:45ce241d316b | 106 | *@param lv - the different level |
zh870524589 | 0:45ce241d316b | 107 | */ |
zh870524589 | 0:45ce241d316b | 108 | void play(LV &lv); |
zh870524589 | 0:45ce241d316b | 109 | /** design the main levels of the game |
zh870524589 | 0:45ce241d316b | 110 | *@param s - show the current level on the screen |
zh870524589 | 0:45ce241d316b | 111 | *@param num_etank - represents the number of simultaneous occurrences on the screen |
zh870524589 | 0:45ce241d316b | 112 | *@param limit - represents the number of simultaneous occurrences allowed on the screen |
zh870524589 | 0:45ce241d316b | 113 | *@param total_num - A total of enemy's tanks were created |
zh870524589 | 0:45ce241d316b | 114 | *@param attack_gap - attack interval |
zh870524589 | 0:45ce241d316b | 115 | *@param lv - To end the game |
zh870524589 | 0:45ce241d316b | 116 | */ |
zh870524589 | 0:45ce241d316b | 117 | void level_design(char* s,int num_etank,int limit,int total_num,int attack_gap,LV &lv); |
zh870524589 | 0:45ce241d316b | 118 | |
zh870524589 | 0:45ce241d316b | 119 | |
zh870524589 | 0:45ce241d316b | 120 | int main() |
zh870524589 | 0:45ce241d316b | 121 | |
zh870524589 | 0:45ce241d316b | 122 | { |
zh870524589 | 0:45ce241d316b | 123 | init(); |
zh870524589 | 0:45ce241d316b | 124 | srand(time(NULL)); |
zh870524589 | 0:45ce241d316b | 125 | |
zh870524589 | 0:45ce241d316b | 126 | LV lv = Menu; |
zh870524589 | 0:45ce241d316b | 127 | while(1) |
zh870524589 | 0:45ce241d316b | 128 | { |
zh870524589 | 0:45ce241d316b | 129 | play(lv); |
zh870524589 | 0:45ce241d316b | 130 | if(lv == Guide && pages == 5) |
zh870524589 | 0:45ce241d316b | 131 | { |
zh870524589 | 0:45ce241d316b | 132 | pages = 0; |
zh870524589 | 0:45ce241d316b | 133 | lv = Menu; |
zh870524589 | 0:45ce241d316b | 134 | } |
zh870524589 | 0:45ce241d316b | 135 | if(lv == GAME_OVER) |
zh870524589 | 0:45ce241d316b | 136 | { |
zh870524589 | 0:45ce241d316b | 137 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 138 | lcd.printString("YOU LOSE!",2,2); |
zh870524589 | 0:45ce241d316b | 139 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 140 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 141 | lv = Menu; |
zh870524589 | 0:45ce241d316b | 142 | |
zh870524589 | 0:45ce241d316b | 143 | } |
zh870524589 | 0:45ce241d316b | 144 | } |
zh870524589 | 0:45ce241d316b | 145 | |
zh870524589 | 0:45ce241d316b | 146 | |
zh870524589 | 0:45ce241d316b | 147 | } |
zh870524589 | 0:45ce241d316b | 148 | |
zh870524589 | 0:45ce241d316b | 149 | |
zh870524589 | 0:45ce241d316b | 150 | |
zh870524589 | 0:45ce241d316b | 151 | void init() |
zh870524589 | 0:45ce241d316b | 152 | { |
zh870524589 | 0:45ce241d316b | 153 | lcd.init(); |
zh870524589 | 0:45ce241d316b | 154 | pad.init(); |
zh870524589 | 0:45ce241d316b | 155 | |
zh870524589 | 0:45ce241d316b | 156 | } |
zh870524589 | 0:45ce241d316b | 157 | |
zh870524589 | 0:45ce241d316b | 158 | |
zh870524589 | 0:45ce241d316b | 159 | void createtank(list<ETank*>&etank,N5110 &lcd,Gamepad &pad,int &num_etank,int num_limit,int &total_num) |
zh870524589 | 0:45ce241d316b | 160 | { |
zh870524589 | 0:45ce241d316b | 161 | // when the number of ETank is smaller than the limit of the level,create ETank |
zh870524589 | 0:45ce241d316b | 162 | while(num_etank <num_limit) |
zh870524589 | 0:45ce241d316b | 163 | { |
zh870524589 | 0:45ce241d316b | 164 | ETank *et = new ETank(lcd,pad); |
zh870524589 | 0:45ce241d316b | 165 | etank.push_back(et); |
zh870524589 | 0:45ce241d316b | 166 | num_etank++;//the ETank showing in the screen increase |
zh870524589 | 0:45ce241d316b | 167 | total_num++;//total number increase |
zh870524589 | 0:45ce241d316b | 168 | } |
zh870524589 | 0:45ce241d316b | 169 | } |
zh870524589 | 0:45ce241d316b | 170 | |
zh870524589 | 0:45ce241d316b | 171 | |
zh870524589 | 0:45ce241d316b | 172 | void render(int &num_etank,int limit,int &total_num,int attack_gap) |
zh870524589 | 0:45ce241d316b | 173 | { |
zh870524589 | 0:45ce241d316b | 174 | // clear screen, re-draw and refresh |
zh870524589 | 0:45ce241d316b | 175 | myTank.read_input(pad); |
zh870524589 | 0:45ce241d316b | 176 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 177 | lcd = lcd2; |
zh870524589 | 0:45ce241d316b | 178 | myTank.draw(lcd); |
zh870524589 | 0:45ce241d316b | 179 | if(total_num <=15)//check if the total numbers of ETank in one level are bigger than 15 |
zh870524589 | 0:45ce241d316b | 180 | createtank(etank,lcd,pad,num_etank,limit,total_num); // create the ETank |
zh870524589 | 0:45ce241d316b | 181 | for(list<ETank*>::iterator it =etank.begin();it!=etank.end();) |
zh870524589 | 0:45ce241d316b | 182 | { |
zh870524589 | 0:45ce241d316b | 183 | (*it)->drawE(lcd); |
zh870524589 | 0:45ce241d316b | 184 | (*it)->direction(myTank); |
zh870524589 | 0:45ce241d316b | 185 | int et_attack = rand()%100; //Random attack |
zh870524589 | 0:45ce241d316b | 186 | (*it)->attack(v,pad,lcd,myTank,tbullet,et_attack,attack_gap); |
zh870524589 | 0:45ce241d316b | 187 | et_move = rand()%11; |
zh870524589 | 0:45ce241d316b | 188 | if(et_move > 3) //posibility of moving |
zh870524589 | 0:45ce241d316b | 189 | (*it)->update(lcd,myTank,etank); |
zh870524589 | 0:45ce241d316b | 190 | it++; |
zh870524589 | 0:45ce241d316b | 191 | } |
zh870524589 | 0:45ce241d316b | 192 | myTank.attack(tbullet,v,pad,lcd,etank,num_etank); |
zh870524589 | 0:45ce241d316b | 193 | myTank.update(etank); |
zh870524589 | 0:45ce241d316b | 194 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 195 | wait(1.0f/fps); |
zh870524589 | 0:45ce241d316b | 196 | } |
zh870524589 | 0:45ce241d316b | 197 | |
zh870524589 | 0:45ce241d316b | 198 | |
zh870524589 | 0:45ce241d316b | 199 | |
zh870524589 | 0:45ce241d316b | 200 | void level_design(char* s,int num_etank,int limit,int total_num,int attack_gap,LV &lv) |
zh870524589 | 0:45ce241d316b | 201 | { |
zh870524589 | 0:45ce241d316b | 202 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 203 | char buffer1[14]; |
zh870524589 | 0:45ce241d316b | 204 | sprintf(buffer1,"%s",s); |
zh870524589 | 0:45ce241d316b | 205 | lcd.printString(buffer1,5,1); |
zh870524589 | 0:45ce241d316b | 206 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 207 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 208 | |
zh870524589 | 0:45ce241d316b | 209 | while(1){ |
zh870524589 | 0:45ce241d316b | 210 | render(num_etank,limit,total_num,attack_gap); |
zh870524589 | 0:45ce241d316b | 211 | int HP = myTank.MT_HP(); |
zh870524589 | 0:45ce241d316b | 212 | if( num_etank == 0) |
zh870524589 | 0:45ce241d316b | 213 | break; |
zh870524589 | 0:45ce241d316b | 214 | if(HP == 0) |
zh870524589 | 0:45ce241d316b | 215 | { |
zh870524589 | 0:45ce241d316b | 216 | lv = GAME_OVER; |
zh870524589 | 0:45ce241d316b | 217 | break; |
zh870524589 | 0:45ce241d316b | 218 | } |
zh870524589 | 0:45ce241d316b | 219 | } |
zh870524589 | 0:45ce241d316b | 220 | |
zh870524589 | 0:45ce241d316b | 221 | } |
zh870524589 | 0:45ce241d316b | 222 | |
zh870524589 | 0:45ce241d316b | 223 | |
zh870524589 | 0:45ce241d316b | 224 | void play(LV &lv) // the whole game process |
zh870524589 | 0:45ce241d316b | 225 | { |
zh870524589 | 0:45ce241d316b | 226 | switch(lv) |
zh870524589 | 0:45ce241d316b | 227 | { |
zh870524589 | 0:45ce241d316b | 228 | case Menu: |
zh870524589 | 0:45ce241d316b | 229 | pad.init(); |
zh870524589 | 0:45ce241d316b | 230 | etank.clear(); |
zh870524589 | 0:45ce241d316b | 231 | tbullet.clear(); |
zh870524589 | 0:45ce241d316b | 232 | |
zh870524589 | 0:45ce241d316b | 233 | while(1){ |
zh870524589 | 0:45ce241d316b | 234 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 235 | lcd.drawSprite(25,2,28,28,(int *)menu_tank); |
zh870524589 | 0:45ce241d316b | 236 | lcd.printString("TANK",1,1); |
zh870524589 | 0:45ce241d316b | 237 | lcd.printString("WAR",60,1); |
zh870524589 | 0:45ce241d316b | 238 | lcd.printString("START-- Play",10,4); |
zh870524589 | 0:45ce241d316b | 239 | lcd.printString("X --- Guide",10,5); |
zh870524589 | 0:45ce241d316b | 240 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 241 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 242 | if(pad.check_event(Gamepad::START_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 243 | lv = Advice; |
zh870524589 | 0:45ce241d316b | 244 | break; |
zh870524589 | 0:45ce241d316b | 245 | } |
zh870524589 | 0:45ce241d316b | 246 | else if(pad.check_event(Gamepad::X_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 247 | lv = Guide; |
zh870524589 | 0:45ce241d316b | 248 | break; |
zh870524589 | 0:45ce241d316b | 249 | } |
zh870524589 | 0:45ce241d316b | 250 | } |
zh870524589 | 0:45ce241d316b | 251 | break; |
zh870524589 | 0:45ce241d316b | 252 | |
zh870524589 | 0:45ce241d316b | 253 | case Advice: |
zh870524589 | 0:45ce241d316b | 254 | while(1){ |
zh870524589 | 0:45ce241d316b | 255 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 256 | lcd.printString("read Guide",0,0); |
zh870524589 | 0:45ce241d316b | 257 | lcd.printString("before game",0,1); |
zh870524589 | 0:45ce241d316b | 258 | lcd.printString(" A--continue",0,2); |
zh870524589 | 0:45ce241d316b | 259 | lcd.printString("to map edited",0,3); |
zh870524589 | 0:45ce241d316b | 260 | lcd.printString(" B--return",0,4); |
zh870524589 | 0:45ce241d316b | 261 | lcd.printString("to the menu",0,5); |
zh870524589 | 0:45ce241d316b | 262 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 263 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 264 | if(pad.check_event(Gamepad::A_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 265 | lv = map_edit; |
zh870524589 | 0:45ce241d316b | 266 | break; |
zh870524589 | 0:45ce241d316b | 267 | } |
zh870524589 | 0:45ce241d316b | 268 | if(pad.check_event(Gamepad::B_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 269 | lv = Menu; |
zh870524589 | 0:45ce241d316b | 270 | break; |
zh870524589 | 0:45ce241d316b | 271 | } |
zh870524589 | 0:45ce241d316b | 272 | } |
zh870524589 | 0:45ce241d316b | 273 | break; |
zh870524589 | 0:45ce241d316b | 274 | |
zh870524589 | 0:45ce241d316b | 275 | case map_edit: |
zh870524589 | 0:45ce241d316b | 276 | map.init(lcd); |
zh870524589 | 0:45ce241d316b | 277 | while(1){ |
zh870524589 | 0:45ce241d316b | 278 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 279 | map.update(pad); // edit the map |
zh870524589 | 0:45ce241d316b | 280 | map.paddraw(lcd); |
zh870524589 | 0:45ce241d316b | 281 | map.drawmap(pad,lcd); |
zh870524589 | 0:45ce241d316b | 282 | if(pad.check_event(Gamepad::START_PRESSED)== true) |
zh870524589 | 0:45ce241d316b | 283 | { |
zh870524589 | 0:45ce241d316b | 284 | lcd2 = lcd;// store the map in lcd2 |
zh870524589 | 0:45ce241d316b | 285 | lv = level1;//finish edit and go to level1 |
zh870524589 | 0:45ce241d316b | 286 | break; |
zh870524589 | 0:45ce241d316b | 287 | } |
zh870524589 | 0:45ce241d316b | 288 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 289 | wait(1.0f/fps); |
zh870524589 | 0:45ce241d316b | 290 | } |
zh870524589 | 0:45ce241d316b | 291 | break; |
zh870524589 | 0:45ce241d316b | 292 | |
zh870524589 | 0:45ce241d316b | 293 | case Guide: |
zh870524589 | 0:45ce241d316b | 294 | while(pages == 0){ |
zh870524589 | 0:45ce241d316b | 295 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 296 | lcd.printString("Tank-attack:A",0,0); |
zh870524589 | 0:45ce241d316b | 297 | lcd.printString("--Press A",10,1); |
zh870524589 | 0:45ce241d316b | 298 | lcd.printString("Tank-Move:",0,2); |
zh870524589 | 0:45ce241d316b | 299 | lcd.printString("--joystick ",10,3); |
zh870524589 | 0:45ce241d316b | 300 | lcd.printString("Tank-HP: 3",0,4); |
zh870524589 | 0:45ce241d316b | 301 | lcd.printString(" B-NEXT",30,5); |
zh870524589 | 0:45ce241d316b | 302 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 303 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 304 | if(pad.check_event(Gamepad::B_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 305 | pages=1; //flip over |
zh870524589 | 0:45ce241d316b | 306 | break; |
zh870524589 | 0:45ce241d316b | 307 | } |
zh870524589 | 0:45ce241d316b | 308 | } |
zh870524589 | 0:45ce241d316b | 309 | |
zh870524589 | 0:45ce241d316b | 310 | while(pages == 1){ |
zh870524589 | 0:45ce241d316b | 311 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 312 | lcd.printString("Map edit:",0,1); |
zh870524589 | 0:45ce241d316b | 313 | lcd.printString("You can draw",0,2); |
zh870524589 | 0:45ce241d316b | 314 | lcd.printString("the map before",0,3); |
zh870524589 | 0:45ce241d316b | 315 | lcd.printString("game starting",0,4); |
zh870524589 | 0:45ce241d316b | 316 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 317 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 318 | if(pad.check_event(Gamepad::B_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 319 | pages=2;//flip over |
zh870524589 | 0:45ce241d316b | 320 | break; |
zh870524589 | 0:45ce241d316b | 321 | } |
zh870524589 | 0:45ce241d316b | 322 | } |
zh870524589 | 0:45ce241d316b | 323 | while(pages == 2){ |
zh870524589 | 0:45ce241d316b | 324 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 325 | lcd.printString("Square Move:",0,0); |
zh870524589 | 0:45ce241d316b | 326 | lcd.printString("--joystick ",10,1); |
zh870524589 | 0:45ce241d316b | 327 | lcd.printString("Draw:",0,2); |
zh870524589 | 0:45ce241d316b | 328 | lcd.printString("--Press X",10,3); |
zh870524589 | 0:45ce241d316b | 329 | lcd.printString("Finishi draw:",0,4); |
zh870524589 | 0:45ce241d316b | 330 | lcd.printString("--Press START",0,5); |
zh870524589 | 0:45ce241d316b | 331 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 332 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 333 | if(pad.check_event(Gamepad::B_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 334 | pages=3;//flip over |
zh870524589 | 0:45ce241d316b | 335 | break; |
zh870524589 | 0:45ce241d316b | 336 | } |
zh870524589 | 0:45ce241d316b | 337 | } |
zh870524589 | 0:45ce241d316b | 338 | |
zh870524589 | 0:45ce241d316b | 339 | while (pages == 3){ |
zh870524589 | 0:45ce241d316b | 340 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 341 | lcd.printString("The map drawn is",0,0); |
zh870524589 | 0:45ce241d316b | 342 | lcd.printString("--river",0,1); |
zh870524589 | 0:45ce241d316b | 343 | lcd.printString("The river:",0,2); |
zh870524589 | 0:45ce241d316b | 344 | lcd.printString("Stop movements",0,3); |
zh870524589 | 0:45ce241d316b | 345 | lcd.printString("but no effect",0,4); |
zh870524589 | 0:45ce241d316b | 346 | lcd.printString("to bullets",0,5); |
zh870524589 | 0:45ce241d316b | 347 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 348 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 349 | if(pad.check_event(Gamepad::B_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 350 | pages=4; //flip over |
zh870524589 | 0:45ce241d316b | 351 | break; |
zh870524589 | 0:45ce241d316b | 352 | } |
zh870524589 | 0:45ce241d316b | 353 | } |
zh870524589 | 0:45ce241d316b | 354 | |
zh870524589 | 0:45ce241d316b | 355 | while(pages == 4){ |
zh870524589 | 0:45ce241d316b | 356 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 357 | lcd.printString("SHOP:",0,0); |
zh870524589 | 0:45ce241d316b | 358 | lcd.printString("Every two",0,1); |
zh870524589 | 0:45ce241d316b | 359 | lcd.printString("levels,you can",0,2); |
zh870524589 | 0:45ce241d316b | 360 | lcd.printString("go shopping",0,3); |
zh870524589 | 0:45ce241d316b | 361 | lcd.printString("BACK-EXIT",10,5); |
zh870524589 | 0:45ce241d316b | 362 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 363 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 364 | if(pad.check_event(Gamepad::BACK_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 365 | pages = 5;//flip over |
zh870524589 | 0:45ce241d316b | 366 | break; |
zh870524589 | 0:45ce241d316b | 367 | } |
zh870524589 | 0:45ce241d316b | 368 | } |
zh870524589 | 0:45ce241d316b | 369 | |
zh870524589 | 0:45ce241d316b | 370 | break; |
zh870524589 | 0:45ce241d316b | 371 | |
zh870524589 | 0:45ce241d316b | 372 | |
zh870524589 | 0:45ce241d316b | 373 | case level1: |
zh870524589 | 0:45ce241d316b | 374 | myTank.init(pad,lcd); |
zh870524589 | 0:45ce241d316b | 375 | level_design("LV1",0,2,0,17,lv); |
zh870524589 | 0:45ce241d316b | 376 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 377 | lv = level2; // go to level2 |
zh870524589 | 0:45ce241d316b | 378 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 379 | break; |
zh870524589 | 0:45ce241d316b | 380 | |
zh870524589 | 0:45ce241d316b | 381 | case level2: |
zh870524589 | 0:45ce241d316b | 382 | level_check = 2; |
zh870524589 | 0:45ce241d316b | 383 | level_design("LV2",0,3,0,17,lv); |
zh870524589 | 0:45ce241d316b | 384 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 385 | lv = SHOP;// go to shop |
zh870524589 | 0:45ce241d316b | 386 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 387 | break; |
zh870524589 | 0:45ce241d316b | 388 | |
zh870524589 | 0:45ce241d316b | 389 | case level3: |
zh870524589 | 0:45ce241d316b | 390 | level_design("LV3",0,3,0,16,lv); |
zh870524589 | 0:45ce241d316b | 391 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 392 | lv = level4;// go to level4 |
zh870524589 | 0:45ce241d316b | 393 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 394 | break; |
zh870524589 | 0:45ce241d316b | 395 | |
zh870524589 | 0:45ce241d316b | 396 | case level4: |
zh870524589 | 0:45ce241d316b | 397 | level_check = 4; |
zh870524589 | 0:45ce241d316b | 398 | level_design("LV4",0,3,0,15,lv); |
zh870524589 | 0:45ce241d316b | 399 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 400 | lv = SHOP;// go to shop |
zh870524589 | 0:45ce241d316b | 401 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 402 | break; |
zh870524589 | 0:45ce241d316b | 403 | |
zh870524589 | 0:45ce241d316b | 404 | case level5: |
zh870524589 | 0:45ce241d316b | 405 | level_design("LV5",0,3,0,13,lv); |
zh870524589 | 0:45ce241d316b | 406 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 407 | lv = level6;// go to level6 |
zh870524589 | 0:45ce241d316b | 408 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 409 | break; |
zh870524589 | 0:45ce241d316b | 410 | |
zh870524589 | 0:45ce241d316b | 411 | case level6: |
zh870524589 | 0:45ce241d316b | 412 | level_check = 6; |
zh870524589 | 0:45ce241d316b | 413 | level_design("LV6",0,4,0,13,lv); |
zh870524589 | 0:45ce241d316b | 414 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 415 | lv = SHOP;// go to shop |
zh870524589 | 0:45ce241d316b | 416 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 417 | break; |
zh870524589 | 0:45ce241d316b | 418 | |
zh870524589 | 0:45ce241d316b | 419 | case level7: |
zh870524589 | 0:45ce241d316b | 420 | level_design("LV7",0,5,0,13,lv); |
zh870524589 | 0:45ce241d316b | 421 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 422 | lv = level8;// go to level28 |
zh870524589 | 0:45ce241d316b | 423 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 424 | break; |
zh870524589 | 0:45ce241d316b | 425 | |
zh870524589 | 0:45ce241d316b | 426 | case level8: |
zh870524589 | 0:45ce241d316b | 427 | level_check = 8; |
zh870524589 | 0:45ce241d316b | 428 | level_design("LV8",0,5,0,10,lv); |
zh870524589 | 0:45ce241d316b | 429 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 430 | lv = SHOP;// go to shop |
zh870524589 | 0:45ce241d316b | 431 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 432 | break; |
zh870524589 | 0:45ce241d316b | 433 | |
zh870524589 | 0:45ce241d316b | 434 | case level9: |
zh870524589 | 0:45ce241d316b | 435 | level_design("LV9",0,5,0,9,lv); |
zh870524589 | 0:45ce241d316b | 436 | if(lv!=GAME_OVER) |
zh870524589 | 0:45ce241d316b | 437 | lv = Win;// go to win |
zh870524589 | 0:45ce241d316b | 438 | wait(10.0f/fps); |
zh870524589 | 0:45ce241d316b | 439 | break; |
zh870524589 | 0:45ce241d316b | 440 | |
zh870524589 | 0:45ce241d316b | 441 | case Win: |
zh870524589 | 0:45ce241d316b | 442 | while(1){ |
zh870524589 | 0:45ce241d316b | 443 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 444 | lcd.printString("YOU WIN!",0,0); |
zh870524589 | 0:45ce241d316b | 445 | lcd.printString("B--return",0,4); |
zh870524589 | 0:45ce241d316b | 446 | lcd.printString("to the menu",0,5); |
zh870524589 | 0:45ce241d316b | 447 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 448 | wait(5.0f/fps); |
zh870524589 | 0:45ce241d316b | 449 | if(pad.check_event(Gamepad::B_PRESSED)== true){ |
zh870524589 | 0:45ce241d316b | 450 | lv = Menu;//back to the menu |
zh870524589 | 0:45ce241d316b | 451 | break; |
zh870524589 | 0:45ce241d316b | 452 | } |
zh870524589 | 0:45ce241d316b | 453 | } |
zh870524589 | 0:45ce241d316b | 454 | break; |
zh870524589 | 0:45ce241d316b | 455 | |
zh870524589 | 0:45ce241d316b | 456 | case SHOP: |
zh870524589 | 0:45ce241d316b | 457 | pad.tone(500.0,0.1); |
zh870524589 | 0:45ce241d316b | 458 | num_buy_check = 0; |
zh870524589 | 0:45ce241d316b | 459 | lcd.clear(); |
zh870524589 | 0:45ce241d316b | 460 | lcd.printString("-----SHOP-----",5,0); |
zh870524589 | 0:45ce241d316b | 461 | lcd.printString("Only pick one",0,1); |
zh870524589 | 0:45ce241d316b | 462 | lcd.printString("X-attack gap-1",0,3); |
zh870524589 | 0:45ce241d316b | 463 | lcd.printString("A-HP + 1",0,4); |
zh870524589 | 0:45ce241d316b | 464 | lcd.printString("BACK-EXIT",0,5); |
zh870524589 | 0:45ce241d316b | 465 | lcd.refresh(); |
zh870524589 | 0:45ce241d316b | 466 | if(pad.check_event(Gamepad::X_PRESSED)== true ) |
zh870524589 | 0:45ce241d316b | 467 | { |
zh870524589 | 0:45ce241d316b | 468 | myTank.attack_gap = myTank.attack_gap -1; |
zh870524589 | 0:45ce241d316b | 469 | num_buy_check = 1;// ensure the player can only buy one thing |
zh870524589 | 0:45ce241d316b | 470 | } |
zh870524589 | 0:45ce241d316b | 471 | if(pad.check_event(Gamepad::A_PRESSED)== true ) |
zh870524589 | 0:45ce241d316b | 472 | { |
zh870524589 | 0:45ce241d316b | 473 | if(myTank.totalHP<6) |
zh870524589 | 0:45ce241d316b | 474 | { |
zh870524589 | 0:45ce241d316b | 475 | myTank.totalHP++; |
zh870524589 | 0:45ce241d316b | 476 | myTank.setHP(pad);// add the HP |
zh870524589 | 0:45ce241d316b | 477 | } |
zh870524589 | 0:45ce241d316b | 478 | else |
zh870524589 | 0:45ce241d316b | 479 | { |
zh870524589 | 0:45ce241d316b | 480 | pad.leds_on(); |
zh870524589 | 0:45ce241d316b | 481 | myTank.setHP(pad);// add the HP |
zh870524589 | 0:45ce241d316b | 482 | } |
zh870524589 | 0:45ce241d316b | 483 | num_buy_check = 1; |
zh870524589 | 0:45ce241d316b | 484 | } |
zh870524589 | 0:45ce241d316b | 485 | if(pad.check_event(Gamepad::BACK_PRESSED)== true || num_buy_check==1) |
zh870524589 | 0:45ce241d316b | 486 | { |
zh870524589 | 0:45ce241d316b | 487 | if(level_check == 2) |
zh870524589 | 0:45ce241d316b | 488 | lv = level3;//go to level3 |
zh870524589 | 0:45ce241d316b | 489 | else if(level_check == 4) |
zh870524589 | 0:45ce241d316b | 490 | lv = level5;//go to level5 |
zh870524589 | 0:45ce241d316b | 491 | else if(level_check == 6) |
zh870524589 | 0:45ce241d316b | 492 | lv = level7;//go to level7 |
zh870524589 | 0:45ce241d316b | 493 | else if(level_check == 8) |
zh870524589 | 0:45ce241d316b | 494 | lv = level9;//go to level9 |
zh870524589 | 0:45ce241d316b | 495 | } |
zh870524589 | 0:45ce241d316b | 496 | } |
zh870524589 | 0:45ce241d316b | 497 | } |