Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DebounceIn N5110 PowerControl mbed
Revision 2:8d28a2f491eb, committed 2015-05-08
- Comitter:
- wray2303
- Date:
- Fri May 08 11:30:43 2015 +0000
- Parent:
- 1:71da2fca5e97
- Child:
- 3:39c95d65bee5
- Commit message:
- revision 3
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| main.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri May 08 09:39:33 2015 +0000
+++ b/main.cpp Fri May 08 11:30:43 2015 +0000
@@ -52,7 +52,8 @@
int GenAsteroidFlag = 0; /// asteroid generation flag
int AsteroidCreatedFlag = 0; /// flag to note asteroid is created
int BulletExistsFlag = 0; /// flag to note bullet has been fired and is on screen
-int PauseNumber = 0;
+int PauseNumber = 0; /// pause flag
+int HighScore = 0; /// integer set for highscore
int main()
{
@@ -79,8 +80,8 @@
break;
case 2:
- lcd.clear();
- PauseDisplay();
+ lcd.clear(); /// clears display
+ PauseDisplay(); /// applies pause screen
break;
case 3:
@@ -92,6 +93,22 @@
}
}
+
+void Read()
+{
+ FILE *fp = fopen("/local/HSCORES.txt","r"); /// open file or create then open if not already made. and read value
+ fscanf(fp, "%i", &HighScore); /// look in the local file system
+ fclose(fp); /// close file
+}
+
+void Write()
+{
+ FILE *fp = fopen("/local/HSCORES.txt","w"); /// open file or create then open if not already made. make ready for writing to the file
+ fprintf(fp, "%i\r\n", HighScore); /// print value to the local file
+ fclose(fp); /// close file
+}
+
+
void startUpDisplay()
{
lcd.init(); /// initialise the display
@@ -114,13 +131,14 @@
ship.shape(); /// the ships shape is drawn and and the game can begin
}
+
void Game()
{
lcd.drawLine(0,0,0,47,1); /// draw outline repeatedly when asteroid is falling
lcd.drawLine(83,0,83,47,1);
lcd.drawLine(0,0,83,0,1);
lcd.drawLine(0,47,83,47,1);
-
+
if (printFlag) { /// if print flag set, clear flag and use joysticks horizontal position
printFlag = 0;
@@ -142,7 +160,6 @@
} else {}
-
if(BulletExistsFlag==0) { /// check if there is already a bullet on the screen, a rule of the game one bullet at a time!
if(buttonS) { /// if shoot button is pressed
@@ -157,10 +174,9 @@
readShoot.detach();
bullet.clearBullet();
BulletExistsFlag=0;
-
/** if bullet colision happens during sound being made then
- buzzer doesnt stop, thisis because of the interrupt the button makes
- here we reset the buzzer manually */
+ *buzzer doesnt stop, thisis because of the interrupt the button makes
+ *here we reset the buzzer manually */
shootSoundtimer.stop(); /// stop the timer
shootSoundtimer.reset(); /// reset the timer
PWM1 = 0.0; /// turn of the PWM
@@ -168,11 +184,11 @@
else if(bullet.collision()==false) { /// if bullet co-ordinates are not off the screen
bullet.createBullet(); /// create the bullet
- readShoot.attach(&ReadBullet,0.05); /// set ticker to read the position of the bullet
+ readShoot.attach(&ReadBullet,0.045); /// set ticker to read the position of the bullet
} else {}
if(AsteroidCreatedFlag==1) { /// check if an asteroid has been created
- readTheAsteroid.attach(&ReadAsteroid,0.05); /// set ticker for asteroid movement
+ readTheAsteroid.attach(&ReadAsteroid,0.045); /// set ticker for asteroid movement
} else {}
} else {}
}
@@ -184,16 +200,14 @@
AsteroidCreatedFlag = 1; /// set flag to state an asteroid has been created and is on screen
GenAsteroidFlag=0; /// reset Generate asteroid flag so no more asteroid are created
if(AsteroidCreatedFlag==1) { /// if asteroid has been created
- readTheAsteroid.attach(&ReadAsteroid,0.05); /// set ticker to read and move asteroid
+ readTheAsteroid.attach(&ReadAsteroid,0.045); /// set ticker to read and move asteroid
} else {}
+ } else {}
+}
- } else {}
-
-}
void ReadAsteroid()
{
-
bullet.collision(); /// check if the bullet has made a collision this must mean an asteroid has been shot
if(bullet.collision()==false) { /// if no collision and the asteroid y position is less than the end of the screen move the asteroid
asteroid.ya=asteroid.ya+1; /// move asteroids y position down the screen
@@ -211,6 +225,13 @@
AsteroidCreatedFlag=0; /// reset Asteroid created Flag to state there is no longer an asteroid on screen
GenAsteroidFlag=1; /// set the Generate asteroid flag
asteroid.destroyAsteroid(); /// destroy on screen asteroid
+
+ /** if bullet colision happens during sound being made then
+ buzzer doesnt stop, thisis because of the interrupt the button makes
+ here we reset the buzzer manually*/
+ shootSoundtimer.stop(); /// stop the timer
+ shootSoundtimer.reset(); /// reset the timer
+ PWM1 = 0.0; /// turn of the PWM
} else {}
asteroid.armageddon(); /// check if asteroid has reached the bottom of the screen
@@ -223,13 +244,13 @@
state=3; /// go to state 3 (end screen)
} else {}
- lcd.drawLine(0,0,83,0,1);
-
+ lcd.drawLine(0,0,83,0,1); /// keep re-drawing top line as this is damaged while asteroid moves accross it
}
+
void ReadBullet()
{
- if(shootSoundtimer.read() > 0.1) { /// check when the sound timer reaches 0.1 sec
+ if(shootSoundtimer.read() > 0.1) { /// check when the sound timer reaches 0.1 sec
shootSoundtimer.stop(); /// stop the timer
shootSoundtimer.reset(); /// reset the timer
PWM1 = 0.0; /// turn of the PWM
@@ -264,7 +285,6 @@
bullet.erasePrevBullet(); /// erase previous position
////////// double check this seems to help rid some bugs ////////////
-
bullet.offScreen(); /// check if the bullet is off screen
bullet.collision(); /// check if the bullet has collided with anything
if (bullet.collision() == true||bullet.offScreen()==true) { /// if a collision is made or the bullet goes off the screen
@@ -273,7 +293,6 @@
readShoot.detach(); /// detach the ticker
} else {}
-
if (printFlag) { /// here we are checking the position of the joystick, so the user can move while the bullet is shooting else where
printFlag = 0;
if (joystick.direction == LEFT && ship.xs>=7) {
@@ -285,6 +304,7 @@
}
}
+
void EndDisplay()
{
lcd.clear(); /// clear entire display to make way for end screen
@@ -292,14 +312,24 @@
char buffer[14]; /// set buffer to 14, screen 84 pixels long , 6 pixel per char , 84/6 = 14
int score = sprintf(buffer,"Score: %d",asteroid.nscore); /// print formatted data to buffer
if (score <= 14) { /// if string will fit on display
- lcd.printString(buffer,10,5); /// display on screen
+ lcd.printString(buffer,20,5); /// display on screen
}
-
+ Read(); /// read file to compare values
+ if(asteroid.nscore>HighScore) { /// compare current score against highscore
+ HighScore = asteroid.nscore; /// if current score is larger than highscore = current score
+ Write(); /// write this to file
+ }
+ Read(); /// read the file to then print to lcd
+ char buffer1[14]; /// set buffer to 14, screen 84 pixels long , 6 pixel per char , 84/6 = 14
+ int Hscore = sprintf(buffer1,"HighScore:%d",HighScore); /// print formatted data to buffer
- lcd.printString("Game Over !",12,1); /// closing message
- lcd.printString("Press S4 to",11,2);
- lcd.printString("Play again",12,3);
+ if (Hscore <= 14) { /// if string will fit on display
+ lcd.printString(buffer1,7,4); /// display on screen
+ }
+ lcd.printString("game over !",12,0); /// closing message
+ lcd.printString("Press S4 to",10,1);
+ lcd.printString("Play again",12,2);
lcd.drawLine(0,0,0,47,1); /// draw outline
lcd.drawLine(83,0,83,47,1);
lcd.drawLine(0,0,83,0,1);
@@ -314,6 +344,7 @@
asteroid.nscore=0; /// reset the score
}
+
void PauseDisplay()
{
lcd.printString("PAUSE",28,1); /// print pause display message
@@ -336,6 +367,7 @@
}
}
+
void calibrateJoystick()
{
button.mode(PullDown);
@@ -344,6 +376,7 @@
joystick.y0 = yPot;
}
+
void updateJoystick()
{
/// reading the joystick position 0.0 is centered returns a value in the range -0.5 and 0.5
@@ -367,7 +400,6 @@
} else {
joystick.direction = UNKNOWN;
}
-
/// set flag for use of the joystick position
printFlag = 1;
}
\ No newline at end of file
--- a/main.h Fri May 08 09:39:33 2015 +0000
+++ b/main.h Fri May 08 11:30:43 2015 +0000
@@ -15,6 +15,12 @@
#define DIRECTION_TOLERANCE 0.05
/**
+@namespace LocalFileSystem
+@brief defines a local filesystem
+*/
+LocalFileSystem local("local"); // create local filesystem
+
+/**
@namespace button
@brief represents button input pin from joystick
*/
@@ -341,4 +347,16 @@
*/
void PauseDisplay();
+/**
+*funtion to read from the local file system created
+*used in highscore
+*/
+void Read();
+
+/**
+*funtion to write to the local file system created
+*used in highscore
+*/
+void Write();
+
#endif
\ No newline at end of file