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: TSI TextLCD mbed
Fork of CarGame by
Revision 2:f5abfdfe9e0b, committed 2013-05-03
- Comitter:
- mengelcke
- Date:
- Fri May 03 11:19:48 2013 +0000
- Parent:
- 1:d55966246784
- Commit message:
- revised with more comments
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r d55966246784 -r f5abfdfe9e0b main.cpp
--- a/main.cpp Fri May 03 10:52:36 2013 +0000
+++ b/main.cpp Fri May 03 11:19:48 2013 +0000
@@ -1,14 +1,22 @@
+/* CarGame has the aim of trying to avoid obstacles on two lanes for as long as possbile.
+The speed increases as the game progresses and when you die you are able to see your score. */
+
#include "mbed.h"
#include "TextLCD.h"
#include "TSISensor.h"
-TextLCD lcd(PTD1,PTD3,PTD2,PTD0,PTD5,PTA13); // rs, e, d4-d7
+// initialise LCD display
+TextLCD lcd(PTA1,PTA2,PTD4,PTA12,PTA4,PTA5); // rs, e, d4-d7
+
// number of seconds to wait between each frame movement
float frameTime = 1;
+
// number of frames since speed increase
int steps = 0;
+
// number of frames in total
int score = 0;
+
// size of course
int courseSize = 48;
@@ -18,12 +26,13 @@
char course[courseSize][2];
// blank course to start with
- for(int i = 0; i < courseSize; i++) {
+ for(int i = 0; i < courseSize; i++)
+ {
course[i][0] = ' ';
course[i][1] = ' ';
}
- // obstacles
+ // obstacle course
course[3][0] = '0';
course[6][1] = '0';
course[9][1] = '0';
@@ -47,20 +56,21 @@
// set up touch sensor to control game
TSISensor tsi;
- int gameover = 0;
+ int gameover = 0; // define a 'gameover' flag
// game loop
- while(1) {
+ while(1)
+ {
// print current course on lcd
- for(int i = 15; i >= 0; i--) {
+ for(int i = 15; i >= 0; i--)
+ {
lcd.locate(i,0);
lcd.printf("%c",course[i][0]);
lcd.locate(i,1);
lcd.printf("%c",course[i][1]);
}
-
// create next frame of course
// course loops round infinitely
@@ -70,11 +80,10 @@
char toprow2 = course[courseSize-1][1];
// shift each row up 1
- for(int i = courseSize-1; i >= 0; i--) {
-
+ for(int i = courseSize-1; i >= 0; i--)
+ {
course[i][0] = course[i-1][0];
course[i][1] = course[i-1][1];
-
}
// set bottom rows as the top rows that have been pushed off
@@ -84,7 +93,8 @@
// game control
// what to do if controller is on right
- if (tsi.readPercentage() >= 0.5) { // if on one side of touchpad
+ if (tsi.readPercentage() >= 0.5)
+ { // if on one side of touchpad
// go to column
lcd.locate(15,1);
// print game character
@@ -102,7 +112,8 @@
}
// when controller is on left
- else {
+ else
+ {
// go to column
lcd.locate(15,0);
// print game character
@@ -119,26 +130,27 @@
}
}
- // delay between each frame
- wait(frameTime);
- // add to steps
- steps++;
- // add to score
- score++;
- // check whether enough steps completed to speed game up
- if(steps > 5) {
+ // delay between each frame
+ wait(frameTime);
+ // add to steps
+ steps++;
+ // add to score
+ score++;
+
+ // check whether enough steps completed to speed game up
+ if(steps > 5) {
frameTime -= 0.05;
// reset number of steps
steps = 0;
- }
+ }
- if(gameover == 1)
- {
- wait(10);
- gameover = 0;
- score = 0;
- frameTime = 1;
+ // return all variables to initial values when game is lost
+ if(gameover == 1)
+ {
+ wait(10);
+ gameover = 0;
+ score = 0;
+ frameTime = 1;
+ }
}
- }
-
}
