Albert Tan Mulligan Submission
Dependencies: mbed
Diff: main.cpp
- Revision:
- 10:71ced616a64f
- Parent:
- 9:62fe47a1374f
- Child:
- 11:e60b40963095
--- a/main.cpp Sat May 23 13:26:19 2020 +0000 +++ b/main.cpp Sun May 24 21:20:40 2020 +0000 @@ -30,21 +30,28 @@ N5110 lcd; Character p1; vector<Bullets> shots; -volatile int enemy_number = 1; vector<Enemy> enemies; +Direction holder; +int melody[46] = {277, 330, 311, 277, 330, 311, 330, 311, 370, 330, 415, 330, 277, 330, 311, 277, 330, 311, 330, 311, 370, 330, 311, 247, 277, 330, 311, 277, 330, 311, 330, 311, 370, 330, 415, 440, 330, 415, 440, 494, 554, 415, 440, 494, 622, 659}; // #c = 277, d# =311, f# = 370, g# = 415 +int durations[46] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}; //Variables -State fsm[3] = { +State fsm[4] = { {0,{1,1}}, - {1,{2,2}}, - {2,{1,0}} + {1,{2,3}}, + {2,{1,0}}, + {3,{0,0}} }; volatile int Current_State = 0; volatile int timer = 0; +volatile int score = 0; //PROTOTYPES void menu(); void GameRun(); void Pause(); -void LevelUp(int i); +void AddEnemy(); +void PlayerDead(); +void animation(); +void reset(); //FUNCTIONS int main() { @@ -52,7 +59,9 @@ //SETUPS pad.init(); lcd.init(); + lcd.setContrast(0.4+(pad.read_pot1()/2.0)); lcd.clear(); + lcd.refresh(); //testing CoolTerm //pc.printf("CoolTerm is Connected\n"); //pc.printf("CurrentState = %d\n", Current_State); @@ -64,22 +73,27 @@ else if (Current_State == 1){ GameRun(); } - else if (Current_State ==2){ + else if (Current_State == 2){ Pause(); } + else if (Current_State == 3){ + PlayerDead(); + } } } void menu(){ //pc.printf("Menu Open"); - lcd.printString(" BoxHead ",0,1); - lcd.printString(" Choose your",0,3); - lcd.printString(" Weapon",0,4); + lcd.clear(); + lcd.printString("BoxHead",21,1); + lcd.printString(" Press A to ",0,3); + lcd.printString("Start",27,4); lcd.refresh(); while (1) { //pc.printf("While Loop + %d", g_pad.Button_A_flag); if (pad.A_pressed()) { //pc.printf("Button_A"); + animation(); Current_State = fsm[0].next_state[0]; enemies.push_back(Enemy(timer)); p1.init(40,22); @@ -93,14 +107,29 @@ //pc.printf("Game Open"); Direction dir = pad.get_direction(); float mag = pad.get_mag(); + pad.play_melody(46, melody,durations,180.0,true); while(1){ timer++; + if(timer%100==0){ + AddEnemy(); + } //movement code dir = pad.get_direction(); mag = pad.get_mag(); - + if(dir == N){ + holder = dir; + } + else if(dir == S){ + holder = dir; + } + else if(dir == E){ + holder = dir; + } + else if(dir == W){ + holder = dir; + } if (pad.A_pressed()) { - shots.push_back(Bullets(p1.get_x()+2, p1.get_y()+2, dir)); + shots.push_back(Bullets(p1.get_x()+2, p1.get_y()+2, holder)); } p1.update(dir, mag); for(int i = 0; i<shots.size(); i++){ @@ -113,10 +142,15 @@ } for(int i = 0; i<enemies.size(); i++){ enemies.at(i).update(p1.get_x()+1, p1.get_y()+1); + if(enemies.at(i).get_x()>= p1.get_x()-1 & enemies.at(i).get_x()<= p1.get_x()+3&enemies.at(i).get_y()>= p1.get_y()-1 & enemies.at(i).get_y()<= p1.get_y()+3){ + Current_State = fsm[Current_State].next_state[1]; + return; + } for(int j = 0; j<shots.size(); j++){ if(shots.at(j).get_x() >= enemies.at(i).get_x() & shots.at(j).get_x()<= enemies.at(i).get_x()+3 & shots.at(j).get_y() >= enemies.at(i).get_y() & shots.at(j).get_y()<= enemies.at(i).get_y()+3){ shots.at(j).dead(); - LevelUp(i); + enemies.at(i).reset(timer); + score++; } } } @@ -139,7 +173,7 @@ } } void Pause(){ - lcd.printString(" PAUSED ",0,1); + lcd.printString(" PAUSED",0,1); lcd.refresh(); // put the MCU to sleep until an interrupt wakes it up if (pad.start_pressed()) { @@ -155,9 +189,46 @@ } sleep(); } -void LevelUp(int i){ - - enemies.at(i).reset(timer); - enemies.push_back(Enemy(timer*timer)); +void AddEnemy(){ + enemies.push_back(Enemy(timer)); return; } +void PlayerDead(){ + lcd.clear(); + lcd.printString("YOU DIED",18,1); + char buffer[14]; + if(score>10){ + sprintf(buffer, " Score:%d", score); + lcd.printString(buffer,0,2); + }else{ + sprintf(buffer, "Score:0%d", score); + lcd.printString(buffer,18,2); + } + lcd.printString(" Press A to ",0,4); + lcd.printString("Restart",21,5); + lcd.refresh(); + while(1){ + if(pad.A_pressed()){ + Current_State =fsm[Current_State].next_state[1]; + return; + } + sleep(); + } + } + +void animation(){ + for(int i = 1; i<=42; i++){ + lcd.drawRect(0,0,i,48,FILL_BLACK); + lcd.drawRect(84-i,0,i,48,FILL_BLACK); + lcd.refresh(); + wait(0.005); + } + for(int i = 1; i<=42; i++){ + lcd.drawRect(0,0,i,48,FILL_WHITE); + lcd.drawRect(84-i,0,i,48,FILL_WHITE); + lcd.refresh(); + wait(0.005); + } + + return; + }