Josh Davy / Mbed 2 deprecated Flip

Dependencies:   mbed el17jd

Committer:
joshdavy
Date:
Fri Apr 19 17:54:09 2019 +0000
Revision:
8:21b6d4dbce44
Parent:
7:68e06dda79f7
Child:
9:96969b1c6bde
Main menu added.;

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 1:37802772843e 10 const int fps = 15;
joshdavy 1:37802772843e 11
joshdavy 3:b34685dbdb8d 12
joshdavy 2:b62e8be35a5d 13
joshdavy 1:37802772843e 14
joshdavy 1:37802772843e 15 ///////// pre-processor directives ////////
joshdavy 1:37802772843e 16 #include "mbed.h"
joshdavy 1:37802772843e 17 #include "Gamepad.h"
joshdavy 1:37802772843e 18 #include "N5110.h"
joshdavy 1:37802772843e 19 #include "Sprite.h"
joshdavy 1:37802772843e 20 #include "Game.h"
joshdavy 5:b9cf407bcc63 21 #include "Music.h"
joshdavy 5:b9cf407bcc63 22 #include "SoundData.h"
joshdavy 8:21b6d4dbce44 23 #include "SplashScreen.h"
joshdavy 8:21b6d4dbce44 24 #include "MenuScreen.h"
joshdavy 8:21b6d4dbce44 25 Timer game_timer;
joshdavy 8:21b6d4dbce44 26 Timer music_timer;
joshdavy 1:37802772843e 27
joshdavy 2:b62e8be35a5d 28 ////// Constants //////
joshdavy 3:b34685dbdb8d 29
joshdavy 2:b62e8be35a5d 30
joshdavy 2:b62e8be35a5d 31
joshdavy 2:b62e8be35a5d 32
joshdavy 1:37802772843e 33 /////////////// objects ///////////////
joshdavy 1:37802772843e 34 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
joshdavy 1:37802772843e 35 Gamepad pad;
joshdavy 1:37802772843e 36 Game game;
joshdavy 7:68e06dda79f7 37 Music play;
joshdavy 1:37802772843e 38
joshdavy 1:37802772843e 39
joshdavy 2:b62e8be35a5d 40
joshdavy 1:37802772843e 41 ///////////// prototypes ///////////////
joshdavy 1:37802772843e 42 void init();
joshdavy 1:37802772843e 43 void welcome();
joshdavy 1:37802772843e 44
joshdavy 1:37802772843e 45 // initialies all classes and libraries
joshdavy 1:37802772843e 46 void init()
joshdavy 2:b62e8be35a5d 47
joshdavy 1:37802772843e 48 {
joshdavy 8:21b6d4dbce44 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 8:21b6d4dbce44 54 play.init(data1,NUM_ELEMENTS);
joshdavy 7:68e06dda79f7 55 game.init();
joshdavy 3:b34685dbdb8d 56
joshdavy 8:21b6d4dbce44 57
joshdavy 1:37802772843e 58
joshdavy 1:37802772843e 59 }
joshdavy 1:37802772843e 60 // simple splash screen displayed on start-up
joshdavy 8:21b6d4dbce44 61 void welcome()
joshdavy 8:21b6d4dbce44 62 {
joshdavy 8:21b6d4dbce44 63
joshdavy 8:21b6d4dbce44 64
joshdavy 8:21b6d4dbce44 65 lcd.drawSprite(0,0,48,84, (int *) splashScreen);
joshdavy 1:37802772843e 66 lcd.refresh();
joshdavy 8:21b6d4dbce44 67
joshdavy 8:21b6d4dbce44 68 // wait flashing LEDs until start button is pressed
joshdavy 1:37802772843e 69 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
joshdavy 1:37802772843e 70 pad.leds_on();
joshdavy 1:37802772843e 71 wait(0.1);
joshdavy 1:37802772843e 72 pad.leds_off();
joshdavy 1:37802772843e 73 wait(0.1);
joshdavy 2:b62e8be35a5d 74 printf("Welcome\n");
joshdavy 1:37802772843e 75 }
joshdavy 2:b62e8be35a5d 76
joshdavy 1:37802772843e 77 }
joshdavy 1:37802772843e 78
joshdavy 8:21b6d4dbce44 79 void how_to_play()
joshdavy 8:21b6d4dbce44 80 {
joshdavy 8:21b6d4dbce44 81
joshdavy 8:21b6d4dbce44 82
joshdavy 8:21b6d4dbce44 83 }
joshdavy 8:21b6d4dbce44 84
joshdavy 8:21b6d4dbce44 85 void menu()
joshdavy 8:21b6d4dbce44 86 {
joshdavy 8:21b6d4dbce44 87 int menu_option = 1;
joshdavy 8:21b6d4dbce44 88
joshdavy 8:21b6d4dbce44 89
joshdavy 8:21b6d4dbce44 90 while ( !pad.check_event(Gamepad::A_PRESSED) ) {
joshdavy 8:21b6d4dbce44 91
joshdavy 8:21b6d4dbce44 92 if (pad.get_coord().y> 0.7f) {
joshdavy 8:21b6d4dbce44 93 menu_option = 1;
joshdavy 8:21b6d4dbce44 94 }
joshdavy 8:21b6d4dbce44 95 if (pad.get_coord().y < -0.7f) {
joshdavy 8:21b6d4dbce44 96 menu_option = 0;
joshdavy 8:21b6d4dbce44 97 }
joshdavy 8:21b6d4dbce44 98 lcd.drawSprite(0,0,48,84, (int *) menuScreen);
joshdavy 8:21b6d4dbce44 99 if (menu_option == 1) {
joshdavy 8:21b6d4dbce44 100 lcd.drawSprite(40,10,7,4, (int *) arrow);
joshdavy 8:21b6d4dbce44 101 } else {
joshdavy 8:21b6d4dbce44 102 lcd.drawSprite(40,25,7,4, (int *) arrow);
joshdavy 8:21b6d4dbce44 103 }
joshdavy 8:21b6d4dbce44 104 lcd.refresh();
joshdavy 8:21b6d4dbce44 105
joshdavy 8:21b6d4dbce44 106 }
joshdavy 8:21b6d4dbce44 107 if (menu_option == 1) {
joshdavy 8:21b6d4dbce44 108 how_to_play();
joshdavy 8:21b6d4dbce44 109 }
joshdavy 8:21b6d4dbce44 110 }
joshdavy 8:21b6d4dbce44 111
joshdavy 5:b9cf407bcc63 112
joshdavy 1:37802772843e 113 int main()
joshdavy 1:37802772843e 114 {
joshdavy 8:21b6d4dbce44 115
joshdavy 1:37802772843e 116 init(); // initialise and then display welcome screen...
joshdavy 1:37802772843e 117 welcome(); // waiting for the user to start
joshdavy 8:21b6d4dbce44 118 menu(); // main menu
joshdavy 1:37802772843e 119
joshdavy 1:37802772843e 120 // game loop - read input, update the game state and render the display
joshdavy 7:68e06dda79f7 121
joshdavy 7:68e06dda79f7 122 game.init();
joshdavy 8:21b6d4dbce44 123
joshdavy 8:21b6d4dbce44 124 game_timer.reset();
joshdavy 8:21b6d4dbce44 125 music_timer.reset();
joshdavy 8:21b6d4dbce44 126 game_timer.start();
joshdavy 8:21b6d4dbce44 127 music_timer.start();
joshdavy 7:68e06dda79f7 128
joshdavy 8:21b6d4dbce44 129 const int MUSIC_TICK_TIME = 100; //125 microseconds
joshdavy 8:21b6d4dbce44 130 const int GAME_TICK_TIME = 100; // 100 millieconds
joshdavy 8:21b6d4dbce44 131
joshdavy 8:21b6d4dbce44 132 while (1) {
joshdavy 8:21b6d4dbce44 133 if (game_timer.read_ms() > GAME_TICK_TIME) {
joshdavy 8:21b6d4dbce44 134 game.read_input(pad);
joshdavy 8:21b6d4dbce44 135 game.update(pad);
joshdavy 8:21b6d4dbce44 136 game.draw(lcd);
joshdavy 8:21b6d4dbce44 137 game_timer.reset();
joshdavy 8:21b6d4dbce44 138 }
joshdavy 2:b62e8be35a5d 139
joshdavy 8:21b6d4dbce44 140 if (music_timer.read_us() > MUSIC_TICK_TIME) {
joshdavy 8:21b6d4dbce44 141 play.play_next();
joshdavy 8:21b6d4dbce44 142 music_timer.reset();
joshdavy 8:21b6d4dbce44 143 }
joshdavy 1:37802772843e 144
joshdavy 8:21b6d4dbce44 145
joshdavy 8:21b6d4dbce44 146
joshdavy 8:21b6d4dbce44 147 }
joshdavy 8:21b6d4dbce44 148 }