Code for the space evader game.
Dependencies: N5110 PowerControl mbed
Diff: main.cpp
- Revision:
- 1:225522d0dd77
- Parent:
- 0:dd6685f1343e
--- a/main.cpp Sun May 10 16:48:46 2015 +0000 +++ b/main.cpp Mon May 11 11:44:51 2015 +0000 @@ -12,7 +12,7 @@ //external variables printFlag,joystick,pollJoystick,serial and button used //button(p18),xPot(p19),yPot(p20) used with external joystick #include "PowerControl/PowerControl.h" -#include "PowerControl/EthernetPowerControl.h" +#include "PowerControl/EthernetPowerControl.h" int main() { init(); @@ -21,166 +21,124 @@ introInit(); timer.attach(&timerExpired,0.01); while(1) { - - if(menuMode&timerFlag) { + if(timerFlag) { timerFlag = 0; - if(counter==20) { - menuSet();//runs the menu every 20 counts - } if(soundOn) { soundActivate(); //activates sound } - if(counter == 20) { - counter=0;//reset the counter - } - counter++; - //timer.detach(); - //timer.attach(&timerExpired,0.01);//attaching the timer for the next frame - } else if(gameOverMode&timerFlag) { - //game over mode - timerFlag=0; - if(counter==20) { - gameOverSet();//sets the game over frame every 20 counts - } - if(soundOn) { - soundActivate(); //acitvates sound - } - if(counter == 20) { - counter=0;//reset the counter + if(menuMode) { + if(counter==20) { + menuSet();//runs the menu every 20 counts + } + } else if(gameOverMode) { + //game over mode + if(counter==20) { + gameOverSet();//sets the game over frame every 20 counts + } + if(soundOn) { + soundActivate(); //acitvates sound + } + } else if(introMode) { + introSet(); //sets the intro frame every count + timer.detach(); + timer.attach(&timerExpired,0.01); //attaching the timer for the next frame + } else if(helpMode) { + if(buttonFlag) { + menuInit(); //if button pressed go back to the menu + } + buttonFlag = 0; } - counter++; - //timer.detach(); - //timer.attach(&timerExpired,0.01);//attaching the timer for the next frame - } else if(introMode&timerFlag) { - introSet(); //sets the intro frame every count - timer.detach(); - timer.attach(&timerExpired,0.01); //attaching the timer for the next frame - } else if(helpMode&timerFlag) { - timerFlag = 0; - if(buttonFlag) { - menuInit(); //if button pressed go back to the menu - } - buttonFlag = 0; - if(counter == 20) { - counter=0;//reset the counter - } - counter++; - //timer.detach(); - //timer.attach(&timerExpired,0.01); - } - else if(highScoreMode&timerFlag) { - if(counter==20) { - timerFlag = 0; - if(buttonFlag) { - menuInit(); //if button pressed bring player back to menu + else if(highScoreMode) { + if(counter==20) { + if(buttonFlag) { + menuInit(); //if button pressed bring player back to menu + } + } + } else if(settingsMode) { //runs settingsMode + if(counter==20) { + settingsSet();//runs the setting frame + } + } else if(gameMode) { //game run mode + if(roundFlag) { + //if new round flag is set start the next round + roundFlag =0; + roundInit(); + } + if(item.active) { + item.addSelf();//if item has selected add the item to the screen + } + if(speedState==0) { + //controlling the rate of movement of the players ship and asteroids depending on the speedState + if(counter == 5||counter == 10||counter == 15||counter == 20) {// speed x1.0 + if(shipExplodeFlag) { + shipExplode();//if is in explotion stage play explotion frame + } else { + //otherwise move the ship + ship.moveSelf(joystick.direction[0],joystick.direction[1]); + } + for(int i=0; i<8; i++) { + if(asteroid[i].active) { + //move the asteroids that are active + asteroid[i].moveSelf(); + } + } + } + } else if(speedState==1) {// x1.25 + if(counter == 4||counter == 8||counter == 12||counter == 16||counter == 20) { + if(shipExplodeFlag) { + shipExplode(); + } else { + ship.moveSelf(joystick.direction[0],joystick.direction[1]); + } + for(int i=0; i<8; i++) { + if(asteroid[i].active) { + asteroid[i].moveSelf(); + } + } + } + } else if(speedState==2) { //x1.33 + if(counter == 3||counter == 6||counter == 9||counter == 12||counter == 15||counter == 18||counter == 20) { + if(shipExplodeFlag) { + shipExplode(); + } else { + ship.moveSelf(joystick.direction[0],joystick.direction[1]); + } + for(int i=0; i<8; i++) { + if(asteroid[i].active) { + asteroid[i].moveSelf(); + } + } + } + } else if(speedState==3) { //x2.5 + if(counter == 2||counter == 4||counter == 6||counter == 8||counter == 10||counter == 12||counter == 14||counter == 16||counter == 18||counter == 20) { + if(shipExplodeFlag) { + shipExplode(); + } else { + ship.moveSelf(joystick.direction[0],joystick.direction[1]); + } + for(int i=0; i<8; i++) { + if(asteroid[i].active) { + asteroid[i].moveSelf(); + } + } + } + } + + if(buttonFlag&laserClear) { // fire button pressed + laserActivate(); + } + buttonFlag = 0; //reset the buttonFlag after everytime used + laserMove(); //move the laser depending on that which are active + check(); //checks the logic of the ships position against asteroids, asteroids against each other and walls, lasers against asteroids + if(counter ==3||counter == 6|| counter ==9||counter == 12||counter == 15||counter == 18||counter == 20) { + setScreen(); // refresh the screen once every three 'counts' } } if(counter == 20) { counter=0;//reset the counter } counter++; - //timer.detach(); - //timer.attach(&timerExpired,0.01); - } else if(settingsMode&timerFlag) { //runs settingsMode - timerFlag=0; - if(counter==20) { - settingsSet();//runs the setting frame - } - if(soundOn) { - soundActivate(); - } - if(counter == 20) { - counter=0;//reset the counter - } - counter++; - //timer.detach(); - //timer.attach(&timerExpired,0.01); - } else if(timerFlag&gameMode) { //game run mode - timerFlag = 0; - if(roundFlag) { - //if new round flag is set start the next round - roundFlag =0; - roundInit(); - } - if(soundOn) { - soundActivate();//activates sound - } - if(item.active) { - item.addSelf();//if item has selected add the item to the screen - } - if(speedState==0) { - //controlling the rate of movement of the players ship and asteroids depending on the speedState - if(counter == 5||counter == 10||counter == 15||counter == 20) { - if(shipExplodeFlag) { - shipExplode();//if is in explotion stage play explotion frame - } else { - //otherwise move the ship - ship.moveSelf(joystick.direction[0],joystick.direction[1]); - } - for(int i=0; i<8; i++) { - if(asteroidM[i].active) { - //move the asteroids that are active - asteroidM[i].moveSelf(); - } - } - } - } else if(speedState==1) { - if(counter == 4||counter == 8||counter == 12||counter == 16||counter == 20) { - if(shipExplodeFlag) { - shipExplode(); - } else { - ship.moveSelf(joystick.direction[0],joystick.direction[1]); - } - for(int i=0; i<8; i++) { - if(asteroidM[i].active) { - asteroidM[i].moveSelf(); - } - } - } - } else if(speedState==2) { - if(counter == 3||counter == 6||counter == 9||counter == 12||counter == 15||counter == 18||counter == 20) { - if(shipExplodeFlag) { - shipExplode(); - } else { - ship.moveSelf(joystick.direction[0],joystick.direction[1]); - } - for(int i=0; i<8; i++) { - if(asteroidM[i].active) { - asteroidM[i].moveSelf(); - } - } - } - } else if(speedState==3) { - if(counter == 2||counter == 4||counter == 6||counter == 8||counter == 10||counter == 12||counter == 14||counter == 16||counter == 18||counter == 20) { - if(shipExplodeFlag) { - shipExplode(); - } else { - ship.moveSelf(joystick.direction[0],joystick.direction[1]); - } - for(int i=0; i<8; i++) { - if(asteroidM[i].active) { - asteroidM[i].moveSelf(); - } - } - } - } - - if(buttonFlag&laserClear) { // fire button pressed - laserActivate(); - } - buttonFlag = 0; //reset the buttonFlag after everytime used - laserMove(); //move the laser depending on that which are active - check(); //checks the logic of the ships position against asteroids, asteroids against each other and walls, lasers against asteroids - if(counter ==3||counter == 6|| counter ==9||counter == 12||counter == 15||counter == 18||counter == 20) { - setScreen(); // refresh the screen once every three 'counts' - } - if(counter == 20) { - counter=0;//reset the counter - } - counter++; - //timer.detach(); - //timer.attach(&timerExpired,0.005);//timerExpired function increased rate for the game } sleep();// put the mbed to sleep otherwise } @@ -222,7 +180,7 @@ laserCoolDown = 70; rapidFire = 0; abilityCounter = 0; - titleSelected = 0; + optionSelected = 0; introMode = 1; lcd.init(); srand(time(NULL));//seedin the random function with the current time for more random functions @@ -501,17 +459,7 @@ gameMode = 0; gameOverMode =1; menuMode = 0; - speedState = 0; - //http://developer.mbed.org/questions/249/float-or-integer-to-char-or-string-conve/ - char scoreString[10]; - //converts score to string - sprintf(scoreString,"%d",score); lcd.clear(); - lcd.printString("Game Over",10,0); - lcd.printString("Your score was",0,1); - //score is printed - lcd.printString(scoreString,10,2); - lcd.printString("AAA",10,4); buttonFlag = 0; timer.detach(); timer.attach(&timerExpired,0.01); @@ -521,12 +469,12 @@ //ship position check against asteroids for (int j = 0; j<8; j++) { //loops through every asteroid - if((((asteroidM[j].position[0])>(ship.position[0]-5))&((asteroidM[j].position[0])<(ship.position[0]+5)) - &((asteroidM[j].position[1])>(ship.position[1]-5))&((asteroidM[j].position[1])<(ship.position[1]+5)))&asteroidM[j].active) { + if((((asteroid[j].position[0])>(ship.position[0]-5))&((asteroid[j].position[0])<(ship.position[0]+5)) + &((asteroid[j].position[1])>(ship.position[1]-5))&((asteroid[j].position[1])<(ship.position[1]+5)))&asteroid[j].active) { //if an asteroid overlaps with a ships position if(ship.shield) {//if ship shields are up - asteroidM[j].active = 0; - asteroidM[j].deleteSelf(); //deactivating the asteroid + asteroid[j].active = 0; + asteroid[j].deleteSelf(); //deactivating the asteroid asteroids--; if(score>50) {//player looses 50 points score = score - 50; @@ -540,8 +488,8 @@ } else if((!shipExplodeFlag)&!ship.shield) { shipExplodeFlag = 1; explotionStage = 1; - asteroidM[j].active = 0; - asteroidM[j].deleteSelf(); + asteroid[j].active = 0; + asteroid[j].deleteSelf(); initSound(); soundType =2; } @@ -549,26 +497,26 @@ } // checking the asteroids position off each other so they can bounce off each other for(int i = 0; i<8; i++) { - if(asteroidM[i].active) { //looping through all the active asteroids + if(asteroid[i].active) { //looping through all the active asteroids for(int j = 0; j<8; j++) { if(i==j) { //if asteroid checked is the actual asteroid of the first for loop move to the next one j++; } - if(asteroidM[j].active) { - if((asteroidM[i].position[0]>(asteroidM[j].position[0]-5))&(asteroidM[i].position[0]<(asteroidM[j].position[0]+5)) - &(asteroidM[i].position[1]>(asteroidM[j].position[1]-5))&(asteroidM[i].position[1]<(asteroidM[j].position[1]+5))) { + if(asteroid[j].active) { + if((asteroid[i].position[0]>(asteroid[j].position[0]-5))&(asteroid[i].position[0]<(asteroid[j].position[0]+5)) + &(asteroid[i].position[1]>(asteroid[j].position[1]-5))&(asteroid[i].position[1]<(asteroid[j].position[1]+5))) { // if asteroids collide initSound(); soundType =3;// produce a sound - if(asteroidM[i].position[0]>asteroidM[j].position[0]) {//if asteroids x position is greater than the other move forwards - asteroidM[i].xDirection = 1; - } else if(asteroidM[i].position[0]<=asteroidM[j].position[0]) { //if asteroid is behind the other asteroid in the x direction move to the left - asteroidM[i].xDirection = -1; + if(asteroid[i].position[0]>asteroid[j].position[0]) {//if asteroids x position is greater than the other move forwards + asteroid[i].xDirection = 1; + } else if(asteroid[i].position[0]<=asteroid[j].position[0]) { //if asteroid is behind the other asteroid in the x direction move to the left + asteroid[i].xDirection = -1; } - if(asteroidM[i].position[1]>asteroidM[j].position[1]) {//if asteroid is below the other asteroid move down - asteroidM[i].yDirection = 1; - } else if(asteroidM[i].position[1]<=asteroidM[j].position[1])//if asteroid is above the other asteroid move up - asteroidM[i].yDirection = -1; + if(asteroid[i].position[1]>asteroid[j].position[1]) {//if asteroid is below the other asteroid move down + asteroid[i].yDirection = 1; + } else if(asteroid[i].position[1]<=asteroid[j].position[1])//if asteroid is above the other asteroid move up + asteroid[i].yDirection = -1; } } } @@ -577,13 +525,13 @@ //checking the laser positions against the asteroids for(int i = 0; i<4; i++) { for (int j = 0; j<8; j++) { - if(((asteroidM[j].position[0]>(laser[i].position[0]-5))&(asteroidM[j].position[0]<(laser[i].position[0]+5)) - &(asteroidM[j].position[1]>(laser[i].position[1]-5))&(asteroidM[j].position[1]<(laser[i].position[1]+5))&asteroidM[j].active&laser[i].active)) { + if(((asteroid[j].position[0]>(laser[i].position[0]-5))&(asteroid[j].position[0]<(laser[i].position[0]+5)) + &(asteroid[j].position[1]>(laser[i].position[1]-5))&(asteroid[j].position[1]<(laser[i].position[1]+5))&asteroid[j].active&laser[i].active)) { //if both laser and asteroid is active and they collide inititiate destruction initSound(); soundType =2;//play sound - asteroidM[j].active = 0; - asteroidM[j].deleteSelf(); //deactivating the asteroid + asteroid[j].active = 0; + asteroid[j].deleteSelf(); //deactivating the asteroid asteroids--;//marking one less asteroid score = score + 10;//player gains point laser[i].active = 0; // deactiviating the laser @@ -593,8 +541,8 @@ if(x==0) { //puts the item to the asteroids position item.active = 1; - item.position[0] = asteroidM[j].position[0]; - item.position[1] = asteroidM[j].position[1]; + item.position[0] = asteroid[j].position[0]; + item.position[1] = asteroid[j].position[1]; item.addSelf(); } } @@ -686,7 +634,7 @@ void gameStart() { for(int i = 0; i<8; i++) { - asteroidM[i].active = 0; + asteroid[i].active = 0; } item.active = 0; clearArray(); @@ -723,12 +671,12 @@ void menuSet() { //sets the option selected depending on the joystick y direction - titleSelected = titleSelected + joystick.direction[1]; + optionSelected = optionSelected + joystick.direction[1]; //loops around the menu - if(titleSelected>3) { - titleSelected = 0; - } else if(titleSelected<0) { - titleSelected = 3; + if(optionSelected>3) { + optionSelected = 0; + } else if(optionSelected<0) { + optionSelected = 3; } if((joystick.direction[1]==1)||(joystick.direction[1]==-1)) { //if joystick moved initiate sound @@ -746,17 +694,17 @@ lcd.printString("HighScores",10,3); lcd.printString("Help",10,4); lcd.printString("Settings",10,5); - lcd.printString(">",0,titleSelected+2); // array set according to which titleSelected - if(buttonFlag&(titleSelected == 0)) { + lcd.printString(">",0,optionSelected+2); // array set according to which optionSelected + if(buttonFlag&(optionSelected == 0)) { //start option selected gameStart(); - } else if(buttonFlag&(titleSelected == 1)) { + } else if(buttonFlag&(optionSelected == 1)) { //instruction option selected highScoreSet(); - } else if(buttonFlag&(titleSelected == 2)) { + } else if(buttonFlag&(optionSelected == 2)) { //instruction option selected helpInit(); - } else if(buttonFlag&(titleSelected == 3)) { + } else if(buttonFlag&(optionSelected == 3)) { //settings option selected settingsInit(); } @@ -773,6 +721,7 @@ } char scoreString[10]; //stores the score sprintf(scoreString,"%d",score);//converts the users score from an int to a string + ////http://developer.mbed.org/questions/249/float-or-integer-to-char-or-string-conve/ char alphabet [27] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; //alphabet matrix defined cSelected = cSelected +joystick.direction[0]; //character selected moved depending on the joystick if(cSelected>2) { //loops through the characters selected at the boundaries @@ -814,11 +763,11 @@ //sets the characters lcd.printString("Game Over",10,0); lcd.printString("Your score was",0,1); - lcd.printString(scoreString,25,2); - lcd.printString(name,25,4); + lcd.printString(scoreString,35,2); + lcd.printString(name,35,4); for(int i = 0; i<5; i++) { ///draws a line under the selected char - lcd.setPixel(25+i+cSelected*6,40); + lcd.setPixel(35+i+cSelected*6,40); } lcd.refresh(); if(buttonFlag) { @@ -839,7 +788,7 @@ //initialise the spawn position, although spawn positions are not needed this function is required to initialise an asteroid object initSpawn(); ship.init(); - asteroidM[0].init(); + asteroid[0].init(); item.init(); menuMode = 0; gameOverMode = 0; @@ -855,9 +804,9 @@ ship.position[0] = 0; ship.position[1] = 8; ship.addSelf(); - asteroidM[0].position[0] = 0; - asteroidM[0].position[1] = 16; - asteroidM[0].addSelf(); + asteroid[0].position[0] = 0; + asteroid[0].position[1] = 16; + asteroid[0].addSelf(); item.position[0] = 0; item.position[1] = 24; item.addSelf(); @@ -879,11 +828,6 @@ ship.position[1] = 2; ship.addSelf(); setScreen(); - lcd.printString("Space Evader",0,0); - lcd.printString("Start Game",10,2); - lcd.printString("HighScores",10,3); - lcd.printString("Help",10,4); - lcd.printString("Settings",10,5); settingsMode = 0; gameOverMode = 0; gameMode = 0; @@ -948,8 +892,8 @@ } initSpawn(); // initialises spawn positions for(int i = 0; i<roundNumber; i++) { - asteroidM[i].init();//initiates asteroids equal to the round number - asteroidM[i].active = 1; + asteroid[i].init();//initiates asteroids equal to the round number + asteroid[i].active = 1; asteroids++; // add one to the counter } roundNumber++; //increment roundNumber @@ -968,11 +912,11 @@ void settingsSet() { //sets the title selected with the joystick - titleSelected = titleSelected + joystick.direction[1]; - if(titleSelected>3) { - titleSelected = 0; - } else if(titleSelected<0) { - titleSelected = 3; + optionSelected = optionSelected + joystick.direction[1]; + if(optionSelected>3) { + optionSelected = 0; + } else if(optionSelected<0) { + optionSelected = 3; } if((joystick.direction[1]==1)||(joystick.direction[1]==-1)) { soundType =3; //sound when joystick moved @@ -994,7 +938,7 @@ sprintf(myChar,"%dx",1+speedState); //speed marked lcd.printString(myChar,60,1); lcd.printString("Walls",7,2); - lcd.printString(">",0,titleSelected); //selector depending on option selected + lcd.printString(">",0,optionSelected); //selector depending on option selected char bufferDate[14]; //presents the time and date char bufferTime[14]; lcd.printString("Back",7,3); @@ -1004,19 +948,19 @@ strftime(bufferTime,14,"%T", localtime(&seconds)); lcd.printString(bufferDate,7,4); lcd.printString(bufferTime,7,5); - if(buttonFlag&(titleSelected == 0)) { + if(buttonFlag&(optionSelected == 0)) { sound = !sound; - } else if(buttonFlag&(titleSelected == 1)) { + } else if(buttonFlag&(optionSelected == 1)) { //increments the speed counter if that option is selected speedState++; if(speedState>3) { //reset speedState if to high speedState=0; } - } else if(buttonFlag&(titleSelected == 3)) { + } else if(buttonFlag&(optionSelected == 3)) { //back to menu menuInit(); - } else if(buttonFlag&(titleSelected == 2)) { + } else if(buttonFlag&(optionSelected == 2)) { //turn on/off the walls walls = !walls; } @@ -1033,7 +977,7 @@ //initialises the setting screen menuMode = 0; settingsMode = 1; - titleSelected = 0; + optionSelected = 0; buttonFlag =0; settingsSet(); timer.detach(); @@ -1042,11 +986,11 @@ void serialISR() { //sets UNIX time - set_time(1430595271); + set_time(1431348000); } void buttonPressed() // toggle direction in ISR { - if(debounce.read_ms()>150) {//only set flag 150 ms after the last flag was set to filter out the 'bounces' + if(debounce.read_ms()>200) {//only set flag 200 ms after the last flag was set to filter out the 'bounces' //buttonFlag set when button is pressed buttonFlag = 1; debounce.reset();//reset the debounce timer