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.
Diff: main.cpp
- Revision:
- 132:a98cfea2d292
- Parent:
- 128:b57930585692
- Child:
- 133:05bb8de3c630
diff -r c227bbfb38b0 -r a98cfea2d292 main.cpp
--- a/main.cpp Thu May 09 07:32:56 2019 +0000
+++ b/main.cpp Thu May 09 08:45:03 2019 +0000
@@ -76,6 +76,9 @@
title_screen(); // run the title screen
}
+
+
+
void init() // initialies all classes and libraries
{
// need to initialise LCD and Gamepad
@@ -92,6 +95,10 @@
lcd.refresh();
}
+
+//////////// MENUING & GAME LOOP FUNCTIONS //////////////////
+
+
void title_screen()
{
tilt = false; // reset the tilt variable so that on start, joystick is default
@@ -130,8 +137,6 @@
void main_menu()
{
- bool menu_toggle = false; // added to delay the while loop condition toggle to prevent button bounce from causing the game to start immediately
-
lcd.clear();
lcd.printString(" START ",0,1); // start game with default as joystick
lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt
@@ -139,11 +144,11 @@
lcd.printString("HI-SCORE: ",0,5);
print_hi_score(55,5);
lcd.refresh();
- wait(0.4); // load initial frame (delay helps button bounce)
+ wait(0.3); // load initial frame (delay helps button bounce)
int pointer = 1;
- while (menu_toggle == false) {
+ while (pad.check_event(Gamepad::A_PRESSED)) {
lcd.clear();
lcd.printString(" START ",0,1); // start game with default as joystick
lcd.printString(" SETTINGS ",0,2); // choose between joystick and tilt
@@ -156,7 +161,7 @@
if (pad.get_direction() == N && pointer > 1) { // if L is pressed and pointer isnt already on START, move it up one line
pointer -= 1;
pad.tone(750.0,0.3);
- wait(0.1); // large wait time to reduce button bounce errors
+ wait(0.1);
} else if (pad.get_direction() == S && pointer < 3) { // if R is pressed and pointer isnt already on HOW TO PLAY, move it down one line
pointer += 1;
pad.tone(750.0,0.3);
@@ -165,9 +170,6 @@
if (pad.check_event(Gamepad::X_PRESSED) & pad.check_event(Gamepad::Y_PRESSED)) {
save_hi_score(0); // resets hi score
}
- if (pad.check_event(Gamepad::A_PRESSED)) {
- menu_toggle = !menu_toggle;
- }
//printf("Pointer 1 = %d",pointer);
}
@@ -186,14 +188,14 @@
void settings()
{
- lcd.clear();
+ lcd.clear(); // load initial frame
lcd.printString(" JOYSTICK ",0,1); // choose joystick
lcd.printString(" TILT ",0,2); // choose tilt
lcd.printString("SENS :",0,4);
lcd.drawRect(42,30, 40,10,FILL_TRANSPARENT);
lcd.drawRect(42,30,40 * pad.read_pot() + 1,10,FILL_BLACK); // represents the sensitivity which is changed by turning the pot
lcd.refresh();
- wait(0.4); // load initial frame (delay helps button bounce)
+ wait(0.1);
int pointer = 1; // init the pointer
@@ -248,7 +250,7 @@
lcd.printString(" BRICKS. ",0,3);
arrowdown.render(lcd, 38, 43);
lcd.refresh();
- wait(0.4); // load initial frame (delay helps button bounce)
+ wait(0.1); // load initial frame
while (pad.check_event(Gamepad::B_PRESSED) == false) { // while B is not pressed to return to main menu, display instruction on how to interact with the game
@@ -278,7 +280,7 @@
}
-void main_game()
+void main_game() // the Game loop
{
int fps = 8; // frames per second
bool pause = false; // set pause screen to false
@@ -495,17 +497,19 @@
wait(1); // wait 1 second
}
+
+//////////////// SD CARD FUNCTIONS ////////////////////////
+
void save_hi_score(int hi_score) // save score to SD card
{
serial.baud(115200); // max speed
FILE *fp; // file pointer
fp = fopen("/sd/hi_score.txt", "w");
if (fp == NULL) { // if it can't open the file then print error message
- serial.printf("Error! Unable to open file!\n");
+ serial.printf("Error\n");
} else { // opened file so can write
- serial.printf("Writing to file....");
fprintf(fp, "%d",hi_score);
- serial.printf("Done.\n");
+ serial.printf("Written to file.\n");
fclose(fp); // close the file after writing
}
}
@@ -517,7 +521,7 @@
fp = fopen("/sd/hi_score.txt", "r");
int stored_hi_score = NULL;
if (fp == NULL) { // if it can't open the file then print error message
- serial.printf("Error! Unable to open file!\n");
+ serial.printf("Error\n");
} else { // opened file so can write
fscanf(fp, "%d",&stored_hi_score);
serial.printf("Read %d from file.\n",stored_hi_score);