Josh Davy / Mbed 2 deprecated Flip

Dependencies:   mbed el17jd

Committer:
joshdavy
Date:
Mon May 06 10:11:42 2019 +0000
Revision:
10:58cf89dd878c
Parent:
9:96969b1c6bde
Child:
11:db27d3838514
Main game done. Documentation to be done next.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joshdavy 0:4916a63a6cbf 1 /*
joshdavy 0:4916a63a6cbf 2 ELEC2645 Embedded Systems Project
joshdavy 0:4916a63a6cbf 3 School of Electronic & Electrical Engineering
joshdavy 0:4916a63a6cbf 4 University of Leeds
joshdavy 0:4916a63a6cbf 5 Name: Joshua Davy
joshdavy 0:4916a63a6cbf 6 Username: el17jd
joshdavy 0:4916a63a6cbf 7 Student ID Number: 201148379
joshdavy 0:4916a63a6cbf 8 Date: 12/03/2019
joshdavy 1:37802772843e 9 */
joshdavy 2:b62e8be35a5d 10
joshdavy 1:37802772843e 11
joshdavy 1:37802772843e 12 ///////// pre-processor directives ////////
joshdavy 1:37802772843e 13 #include "mbed.h"
joshdavy 1:37802772843e 14 #include "Gamepad.h"
joshdavy 1:37802772843e 15 #include "N5110.h"
joshdavy 1:37802772843e 16 #include "Sprite.h"
joshdavy 1:37802772843e 17 #include "Game.h"
joshdavy 5:b9cf407bcc63 18 #include "Music.h"
joshdavy 5:b9cf407bcc63 19 #include "SoundData.h"
joshdavy 8:21b6d4dbce44 20 #include "SplashScreen.h"
joshdavy 8:21b6d4dbce44 21 #include "MenuScreen.h"
joshdavy 9:96969b1c6bde 22 #include "WinScreen.h"
joshdavy 9:96969b1c6bde 23
joshdavy 1:37802772843e 24
joshdavy 2:b62e8be35a5d 25 ////// Constants //////
joshdavy 3:b34685dbdb8d 26
joshdavy 10:58cf89dd878c 27 // The tick times for both timers
joshdavy 10:58cf89dd878c 28 const int MUSIC_TICK_TIME = 100; //100 microseconds
joshdavy 10:58cf89dd878c 29 const int GAME_TICK_TIME = 100; // 100 millieconds
joshdavy 2:b62e8be35a5d 30
joshdavy 1:37802772843e 31 /////////////// objects ///////////////
joshdavy 1:37802772843e 32 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
joshdavy 1:37802772843e 33 Gamepad pad;
joshdavy 1:37802772843e 34 Game game;
joshdavy 9:96969b1c6bde 35 Music music;
joshdavy 10:58cf89dd878c 36 Timer game_timer;
joshdavy 10:58cf89dd878c 37 Timer music_timer;
joshdavy 2:b62e8be35a5d 38
joshdavy 1:37802772843e 39 ///////////// prototypes ///////////////
joshdavy 1:37802772843e 40 void init();
joshdavy 1:37802772843e 41 void welcome();
joshdavy 9:96969b1c6bde 42 void menu();
joshdavy 9:96969b1c6bde 43 void how_to_play();
joshdavy 9:96969b1c6bde 44 void you_win();
joshdavy 9:96969b1c6bde 45 int main();
joshdavy 10:58cf89dd878c 46
joshdavy 1:37802772843e 47 // initialies all classes and libraries
joshdavy 1:37802772843e 48 void init()
joshdavy 1:37802772843e 49 {
joshdavy 8:21b6d4dbce44 50 // need to initialise LCD and Gamepad
joshdavy 1:37802772843e 51 lcd.init();
joshdavy 2:b62e8be35a5d 52 lcd.setContrast(0.4);
joshdavy 1:37802772843e 53 pad.init();
joshdavy 9:96969b1c6bde 54 // Initalises Music with data from SoundData.h and game
joshdavy 10:58cf89dd878c 55 music.init(sound_data,NUM_ELEMENTS);
joshdavy 7:68e06dda79f7 56 game.init();
joshdavy 3:b34685dbdb8d 57
joshdavy 1:37802772843e 58 }
joshdavy 1:37802772843e 59 // simple splash screen displayed on start-up
joshdavy 8:21b6d4dbce44 60 void welcome()
joshdavy 8:21b6d4dbce44 61 {
joshdavy 8:21b6d4dbce44 62
joshdavy 9:96969b1c6bde 63 // Draws Splash Screen
joshdavy 8:21b6d4dbce44 64 lcd.drawSprite(0,0,48,84, (int *) splashScreen);
joshdavy 1:37802772843e 65 lcd.refresh();
joshdavy 8:21b6d4dbce44 66
joshdavy 8:21b6d4dbce44 67 // wait flashing LEDs until start button is pressed
joshdavy 9:96969b1c6bde 68 while (!pad.check_event(Gamepad::START_PRESSED)) {
joshdavy 10:58cf89dd878c 69 lcd.setContrast(pad.read_pot());
joshdavy 10:58cf89dd878c 70
joshdavy 9:96969b1c6bde 71 pad.leds_on();
joshdavy 9:96969b1c6bde 72 wait(0.1);
joshdavy 9:96969b1c6bde 73 pad.leds_off();
joshdavy 9:96969b1c6bde 74 wait(0.1);
joshdavy 9:96969b1c6bde 75 }
joshdavy 9:96969b1c6bde 76
joshdavy 9:96969b1c6bde 77 }
joshdavy 9:96969b1c6bde 78
joshdavy 9:96969b1c6bde 79 void you_win()
joshdavy 9:96969b1c6bde 80 {
joshdavy 9:96969b1c6bde 81 // Draws Win Screen
joshdavy 9:96969b1c6bde 82 lcd.drawSprite(0,0,48,84, (int *) winScreen);
joshdavy 9:96969b1c6bde 83 lcd.refresh();
joshdavy 9:96969b1c6bde 84
joshdavy 10:58cf89dd878c 85 // wait flashing LEDs until START button is pressed
joshdavy 10:58cf89dd878c 86 while (!pad.check_event(Gamepad::START_PRESSED)) {
joshdavy 1:37802772843e 87 pad.leds_on();
joshdavy 1:37802772843e 88 wait(0.1);
joshdavy 1:37802772843e 89 pad.leds_off();
joshdavy 1:37802772843e 90 wait(0.1);
joshdavy 1:37802772843e 91 }
joshdavy 10:58cf89dd878c 92
joshdavy 9:96969b1c6bde 93 main();
joshdavy 2:b62e8be35a5d 94
joshdavy 1:37802772843e 95 }
joshdavy 1:37802772843e 96
joshdavy 9:96969b1c6bde 97
joshdavy 8:21b6d4dbce44 98 void how_to_play()
joshdavy 8:21b6d4dbce44 99 {
joshdavy 9:96969b1c6bde 100 // Gives instructions on how to play
joshdavy 9:96969b1c6bde 101 lcd.clear();
joshdavy 9:96969b1c6bde 102 lcd.printString("Use the joy -",0,0);
joshdavy 9:96969b1c6bde 103 lcd.printString("stick to move.",0,1);
joshdavy 9:96969b1c6bde 104 lcd.printString("Press A to ",0,2);
joshdavy 9:96969b1c6bde 105 lcd.printString("flip gravity.",0,3);
joshdavy 9:96969b1c6bde 106 lcd.printString("Get the flags",0,4);
joshdavy 9:96969b1c6bde 107 lcd.printString("to win!",0,5);
joshdavy 9:96969b1c6bde 108 lcd.refresh();
joshdavy 10:58cf89dd878c 109
joshdavy 9:96969b1c6bde 110 // Loop until A button pressed
joshdavy 9:96969b1c6bde 111 while ( !pad.check_event(Gamepad::A_PRESSED) ) {}
joshdavy 10:58cf89dd878c 112
joshdavy 9:96969b1c6bde 113 // Return to menu
joshdavy 10:58cf89dd878c 114 menu(); // TRY it without this
joshdavy 8:21b6d4dbce44 115
joshdavy 8:21b6d4dbce44 116 }
joshdavy 8:21b6d4dbce44 117
joshdavy 8:21b6d4dbce44 118 void menu()
joshdavy 8:21b6d4dbce44 119 {
joshdavy 9:96969b1c6bde 120 // Main Menu
joshdavy 10:58cf89dd878c 121 int menu_option = 0;
joshdavy 8:21b6d4dbce44 122 while ( !pad.check_event(Gamepad::A_PRESSED) ) {
joshdavy 10:58cf89dd878c 123 //Allow adjusting of the contrast
joshdavy 10:58cf89dd878c 124 lcd.setContrast(pad.read_pot());
joshdavy 10:58cf89dd878c 125
joshdavy 10:58cf89dd878c 126 // Read the joystick value and relate it to a menu option
joshdavy 9:96969b1c6bde 127 if (pad.get_coord().y > 0.7f) {
joshdavy 10:58cf89dd878c 128 menu_option = 0;
joshdavy 8:21b6d4dbce44 129 }
joshdavy 8:21b6d4dbce44 130 if (pad.get_coord().y < -0.7f) {
joshdavy 10:58cf89dd878c 131 menu_option = 1;
joshdavy 8:21b6d4dbce44 132 }
joshdavy 9:96969b1c6bde 133 // Draws main menu background image
joshdavy 8:21b6d4dbce44 134 lcd.drawSprite(0,0,48,84, (int *) menuScreen);
joshdavy 10:58cf89dd878c 135
joshdavy 9:96969b1c6bde 136 // Draws the arrow on the menu
joshdavy 10:58cf89dd878c 137 if (menu_option == 0) {
joshdavy 8:21b6d4dbce44 138 lcd.drawSprite(40,10,7,4, (int *) arrow);
joshdavy 8:21b6d4dbce44 139 } else {
joshdavy 8:21b6d4dbce44 140 lcd.drawSprite(40,25,7,4, (int *) arrow);
joshdavy 8:21b6d4dbce44 141 }
joshdavy 10:58cf89dd878c 142
joshdavy 8:21b6d4dbce44 143 lcd.refresh();
joshdavy 8:21b6d4dbce44 144
joshdavy 8:21b6d4dbce44 145 }
joshdavy 9:96969b1c6bde 146 // If the "how to play" option is selected run its display function
joshdavy 10:58cf89dd878c 147 if (menu_option == 1) {
joshdavy 8:21b6d4dbce44 148 how_to_play();
joshdavy 8:21b6d4dbce44 149 }
joshdavy 8:21b6d4dbce44 150 }
joshdavy 8:21b6d4dbce44 151
joshdavy 5:b9cf407bcc63 152
joshdavy 10:58cf89dd878c 153
joshdavy 10:58cf89dd878c 154
joshdavy 1:37802772843e 155 int main()
joshdavy 1:37802772843e 156 {
joshdavy 8:21b6d4dbce44 157
joshdavy 1:37802772843e 158 init(); // initialise and then display welcome screen...
joshdavy 1:37802772843e 159 welcome(); // waiting for the user to start
joshdavy 8:21b6d4dbce44 160 menu(); // main menu
joshdavy 1:37802772843e 161
joshdavy 10:58cf89dd878c 162 // Resets and starts the timers responsible for the game and music loops
joshdavy 8:21b6d4dbce44 163 game_timer.reset();
joshdavy 8:21b6d4dbce44 164 music_timer.reset();
joshdavy 8:21b6d4dbce44 165 game_timer.start();
joshdavy 8:21b6d4dbce44 166 music_timer.start();
joshdavy 10:58cf89dd878c 167
joshdavy 10:58cf89dd878c 168
joshdavy 9:96969b1c6bde 169 while (true) {
joshdavy 10:58cf89dd878c 170
joshdavy 9:96969b1c6bde 171 // If the game timer is above the game tick time then main game process
joshdavy 10:58cf89dd878c 172 // is ran
joshdavy 8:21b6d4dbce44 173 if (game_timer.read_ms() > GAME_TICK_TIME) {
joshdavy 10:58cf89dd878c 174 // Allows adjusting on the contrast
joshdavy 10:58cf89dd878c 175 lcd.setContrast(pad.read_pot());
joshdavy 10:58cf89dd878c 176 // Main update states
joshdavy 8:21b6d4dbce44 177 game.update(pad);
joshdavy 8:21b6d4dbce44 178 game.draw(lcd);
joshdavy 10:58cf89dd878c 179
joshdavy 10:58cf89dd878c 180 // If the games won goes to the you_win screen.
joshdavy 9:96969b1c6bde 181 if (game.game_won()) {
joshdavy 9:96969b1c6bde 182 you_win();
joshdavy 10:58cf89dd878c 183
joshdavy 9:96969b1c6bde 184 }
joshdavy 10:58cf89dd878c 185 game_timer.reset();
joshdavy 8:21b6d4dbce44 186 }
joshdavy 9:96969b1c6bde 187 // If the music timer is above the music tick time update the music object
joshdavy 8:21b6d4dbce44 188 if (music_timer.read_us() > MUSIC_TICK_TIME) {
joshdavy 10:58cf89dd878c 189 music.play_next();
joshdavy 10:58cf89dd878c 190 music_timer.reset();
joshdavy 8:21b6d4dbce44 191 }
joshdavy 10:58cf89dd878c 192 }
joshdavy 10:58cf89dd878c 193 }