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:
- 10:58cf89dd878c
- Parent:
- 9:96969b1c6bde
- Child:
- 11:db27d3838514
--- a/main.cpp Wed Apr 24 10:18:45 2019 +0000
+++ b/main.cpp Mon May 06 10:11:42 2019 +0000
@@ -21,21 +21,20 @@
#include "MenuScreen.h"
#include "WinScreen.h"
-Timer game_timer;
-Timer music_timer;
////// Constants //////
-
-
+// The tick times for both timers
+const int MUSIC_TICK_TIME = 100; //100 microseconds
+const int GAME_TICK_TIME = 100; // 100 millieconds
/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
Game game;
Music music;
-
-
+Timer game_timer;
+Timer music_timer;
///////////// prototypes ///////////////
void init();
@@ -44,17 +43,16 @@
void how_to_play();
void you_win();
int main();
+
// initialies all classes and libraries
void init()
-
{
-
// need to initialise LCD and Gamepad
lcd.init();
lcd.setContrast(0.4);
pad.init();
// Initalises Music with data from SoundData.h and game
- music.init(data1,NUM_ELEMENTS);
+ music.init(sound_data,NUM_ELEMENTS);
game.init();
}
@@ -68,6 +66,8 @@
// wait flashing LEDs until start button is pressed
while (!pad.check_event(Gamepad::START_PRESSED)) {
+ lcd.setContrast(pad.read_pot());
+
pad.leds_on();
wait(0.1);
pad.leds_off();
@@ -78,19 +78,18 @@
void you_win()
{
-
// Draws Win Screen
lcd.drawSprite(0,0,48,84, (int *) winScreen);
lcd.refresh();
- // wait flashing LEDs until A button is pressed
- while (!pad.check_event(Gamepad::A_PRESSED)) {
+ // wait flashing LEDs until START button is pressed
+ while (!pad.check_event(Gamepad::START_PRESSED)) {
pad.leds_on();
wait(0.1);
pad.leds_off();
wait(0.1);
}
-
+
main();
}
@@ -107,48 +106,52 @@
lcd.printString("Get the flags",0,4);
lcd.printString("to win!",0,5);
lcd.refresh();
-
+
// Loop until A button pressed
while ( !pad.check_event(Gamepad::A_PRESSED) ) {}
-
+
// Return to menu
- menu();
+ menu(); // TRY it without this
}
void menu()
{
// Main Menu
- int menu_option = 1;
+ int menu_option = 0;
while ( !pad.check_event(Gamepad::A_PRESSED) ) {
-
- // Read the joystick value and relate it to a menu option
+ //Allow adjusting of the contrast
+ lcd.setContrast(pad.read_pot());
+
+ // Read the joystick value and relate it to a menu option
if (pad.get_coord().y > 0.7f) {
- menu_option = 1;
+ menu_option = 0;
}
if (pad.get_coord().y < -0.7f) {
- menu_option = 0;
+ menu_option = 1;
}
// Draws main menu background image
lcd.drawSprite(0,0,48,84, (int *) menuScreen);
-
+
// Draws the arrow on the menu
- if (menu_option == 1) {
+ if (menu_option == 0) {
lcd.drawSprite(40,10,7,4, (int *) arrow);
} else {
lcd.drawSprite(40,25,7,4, (int *) arrow);
}
-
+
lcd.refresh();
}
// If the "how to play" option is selected run its display function
- if (menu_option == 0) {
+ if (menu_option == 1) {
how_to_play();
}
}
+
+
int main()
{
@@ -156,40 +159,35 @@
welcome(); // waiting for the user to start
menu(); // main menu
- // game loop - read input, update the game state and render the display
-
- game.init();
-
- // Resets and starts the timers responsible for the game and music loop
+ // Resets and starts the timers responsible for the game and music loops
game_timer.reset();
music_timer.reset();
game_timer.start();
music_timer.start();
-
- // The tick times for both timers
- const int MUSIC_TICK_TIME = 100; //125 microseconds
- const int GAME_TICK_TIME = 100; // 100 millieconds
-
+
+
while (true) {
-
+
// If the game timer is above the game tick time then main game process
- // is ran
+ // is ran
if (game_timer.read_ms() > GAME_TICK_TIME) {
+ // Allows adjusting on the contrast
+ lcd.setContrast(pad.read_pot());
+ // Main update states
game.update(pad);
game.draw(lcd);
- game_timer.reset();
+
+ // If the games won goes to the you_win screen.
if (game.game_won()) {
you_win();
+
}
+ game_timer.reset();
}
-
// If the music timer is above the music tick time update the music object
if (music_timer.read_us() > MUSIC_TICK_TIME) {
- music.play_next();
- music_timer.reset();
- }
-
-
-
+ music.play_next();
+ music_timer.reset();
}
- }
\ No newline at end of file
+ }
+}
\ No newline at end of file