Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
main.cpp@67:39b9ba6019b0, 2019-05-04 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Sat May 04 12:28:04 2019 +0000
- Revision:
- 67:39b9ba6019b0
- Parent:
- 66:e47333ffc6ca
- Child:
- 68:b9cfd27987ac
Made the startscreen class much more organised
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 0:4b15c2d4aa58 | 1 | /* |
AhmedPlaymaker | 0:4b15c2d4aa58 | 2 | ELEC2645 Embedded Systems Project |
AhmedPlaymaker | 0:4b15c2d4aa58 | 3 | School of Electronic & Electrical Engineering |
AhmedPlaymaker | 0:4b15c2d4aa58 | 4 | University of Leeds |
AhmedPlaymaker | 0:4b15c2d4aa58 | 5 | Name: Ahmed Nomaan Adamjee |
AhmedPlaymaker | 56:142e9fdb77a8 | 6 | Username: el17ana |
AhmedPlaymaker | 0:4b15c2d4aa58 | 7 | Student ID Number: 201161436 |
AhmedPlaymaker | 0:4b15c2d4aa58 | 8 | Date: |
AhmedPlaymaker | 0:4b15c2d4aa58 | 9 | */ |
AhmedPlaymaker | 0:4b15c2d4aa58 | 10 | |
AhmedPlaymaker | 1:32e312688a65 | 11 | ///////// pre-processor directives //////// |
AhmedPlaymaker | 0:4b15c2d4aa58 | 12 | #include "mbed.h" |
AhmedPlaymaker | 1:32e312688a65 | 13 | #include "Gamepad.h" |
AhmedPlaymaker | 1:32e312688a65 | 14 | #include "N5110.h" |
AhmedPlaymaker | 1:32e312688a65 | 15 | #include "FXOS8700CQ.h" |
AhmedPlaymaker | 3:fbb1fa853f09 | 16 | #include "StartScreen.h" |
AhmedPlaymaker | 7:48ba87cd79b5 | 17 | #include "SnakevsBlock.h" |
AhmedPlaymaker | 33:249cf423fb18 | 18 | #include "SDFileSystem.h" |
AhmedPlaymaker | 6:3ffab44ed49c | 19 | |
AhmedPlaymaker | 1:32e312688a65 | 20 | /////////////// objects /////////////// |
AhmedPlaymaker | 1:32e312688a65 | 21 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
AhmedPlaymaker | 1:32e312688a65 | 22 | Gamepad pad; |
AhmedPlaymaker | 1:32e312688a65 | 23 | FXOS8700CQ device(I2C_SDA,I2C_SCL); |
AhmedPlaymaker | 3:fbb1fa853f09 | 24 | StartScreen _start; |
AhmedPlaymaker | 66:e47333ffc6ca | 25 | Stats _stats; |
AhmedPlaymaker | 66:e47333ffc6ca | 26 | SnakevsBlock _game; |
AhmedPlaymaker | 32:3a3bdeffdf62 | 27 | AnalogIn noisy(PTB0); //This creates a random noise which I can use to seed the random numbers. |
AhmedPlaymaker | 33:249cf423fb18 | 28 | // Connections to SD card holder on K64F (SPI interface) |
AhmedPlaymaker | 33:249cf423fb18 | 29 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
AhmedPlaymaker | 33:249cf423fb18 | 30 | //Serial serial(USBTX, USBRX); // for PC debug |
AhmedPlaymaker | 1:32e312688a65 | 31 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 32 | ///////////// prototypes ////////////// |
AhmedPlaymaker | 1:32e312688a65 | 33 | void init(); |
AhmedPlaymaker | 1:32e312688a65 | 34 | void refresh_game(); |
AhmedPlaymaker | 67:39b9ba6019b0 | 35 | void read_write_stats(); |
AhmedPlaymaker | 67:39b9ba6019b0 | 36 | void _set_mode_speed(); |
AhmedPlaymaker | 1:32e312688a65 | 37 | |
AhmedPlaymaker | 2:83e85dea3c89 | 38 | //Constants// |
AhmedPlaymaker | 46:dc7dccae9f9e | 39 | int fps = 40; // frames per second, this will be changed in menu. |
AhmedPlaymaker | 39:210ac915e0a0 | 40 | int g_mode = 1; //selects between joystick and motion control. |
AhmedPlaymaker | 54:20abd16c7d74 | 41 | int back; //this allows us to use the back key to exit the game loop; |
AhmedPlaymaker | 2:83e85dea3c89 | 42 | |
AhmedPlaymaker | 1:32e312688a65 | 43 | ///////////// MAIN //////////////// |
AhmedPlaymaker | 1:32e312688a65 | 44 | int main() |
AhmedPlaymaker | 1:32e312688a65 | 45 | { |
AhmedPlaymaker | 1:32e312688a65 | 46 | init(); |
AhmedPlaymaker | 66:e47333ffc6ca | 47 | _start.titleScreen(lcd, pad); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 48 | while(1) { //This loop is created for Play/Continue configuration |
AhmedPlaymaker | 67:39b9ba6019b0 | 49 | read_write_stats(); |
AhmedPlaymaker | 67:39b9ba6019b0 | 50 | _start.menu(lcd, pad); // this takes us to main menu inside startscreen, and connects automatically to all other menu functions. |
AhmedPlaymaker | 67:39b9ba6019b0 | 51 | _set_mode_speed(); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 52 | // start the game |
AhmedPlaymaker | 67:39b9ba6019b0 | 53 | _start.credits(lcd); // this is after the menu to allow us to hide credits if we want to play the game without wasting any time. |
AhmedPlaymaker | 1:32e312688a65 | 54 | refresh_game(); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 55 | wait(1.0f/fps); |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 56 | |
AhmedPlaymaker | 25:e827f1a8fadc | 57 | // snakeVSblock - detect input respect to the menu options, and update data and refresh screen |
AhmedPlaymaker | 25:e827f1a8fadc | 58 | while (1) { |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 59 | |
AhmedPlaymaker | 66:e47333ffc6ca | 60 | _game.read_input(pad, device, g_mode); //this reads the angle or joystick direction, on the condition of either of them being selected. |
AhmedPlaymaker | 66:e47333ffc6ca | 61 | _game.update(lcd, pad); //updates the game screen and checks for any collisions. |
AhmedPlaymaker | 67:39b9ba6019b0 | 62 | refresh_game(); |
AhmedPlaymaker | 67:39b9ba6019b0 | 63 | |
AhmedPlaymaker | 67:39b9ba6019b0 | 64 | //the int back stores the value 1 if back is pressed inside the update function of snakevsblock, |
AhmedPlaymaker | 63:205f0ca48473 | 65 | //This function also handles level progression and level failure operations by using the class WinLoose. |
AhmedPlaymaker | 67:39b9ba6019b0 | 66 | back = _game.CheckGameProgression(lcd, pad, sd); //and also sends relevant data to the sd card to implement stats functionality. |
AhmedPlaymaker | 67:39b9ba6019b0 | 67 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 68 | if(back) { |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 69 | break; //and this allows us to return to main menu by using the keyword break. |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 70 | } |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 71 | |
AhmedPlaymaker | 25:e827f1a8fadc | 72 | wait(1.0f/fps); |
AhmedPlaymaker | 25:e827f1a8fadc | 73 | } |
AhmedPlaymaker | 1:32e312688a65 | 74 | } |
AhmedPlaymaker | 1:32e312688a65 | 75 | } |
AhmedPlaymaker | 1:32e312688a65 | 76 | |
AhmedPlaymaker | 1:32e312688a65 | 77 | void init() |
AhmedPlaymaker | 1:32e312688a65 | 78 | { |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 79 | // need to initialise LCD and Gamepad |
AhmedPlaymaker | 56:142e9fdb77a8 | 80 | lcd.init(); //init for the N5110 Library. |
AhmedPlaymaker | 56:142e9fdb77a8 | 81 | device.init(); //init for the FXOS8700CQ Library. |
AhmedPlaymaker | 56:142e9fdb77a8 | 82 | pad.init(); //init for the Gamepad Library. |
AhmedPlaymaker | 66:e47333ffc6ca | 83 | _game.init(); //init for the SnakeVSBlock Class. |
AhmedPlaymaker | 56:142e9fdb77a8 | 84 | _start.init(); //init for the Menu Class --> StartScreen. |
AhmedPlaymaker | 56:142e9fdb77a8 | 85 | srand(100000*noisy.read_u16()); //seeds the random number generator with a random noise from the K64F. |
AhmedPlaymaker | 1:32e312688a65 | 86 | } |
AhmedPlaymaker | 1:32e312688a65 | 87 | |
AhmedPlaymaker | 1:32e312688a65 | 88 | void refresh_game() |
AhmedPlaymaker | 1:32e312688a65 | 89 | { |
AhmedPlaymaker | 56:142e9fdb77a8 | 90 | lcd.clear(); //clears the N5110 screen for the next frame |
AhmedPlaymaker | 66:e47333ffc6ca | 91 | _game.draw(lcd, pad); //draws the next game frame |
AhmedPlaymaker | 66:e47333ffc6ca | 92 | _game.get_pos(); //takes the game object coordinates and saves it privately to use later for implementing collisions. |
AhmedPlaymaker | 56:142e9fdb77a8 | 93 | lcd.refresh(); //refreshes the N5110 screen to display the frame. |
AhmedPlaymaker | 67:39b9ba6019b0 | 94 | } |
AhmedPlaymaker | 67:39b9ba6019b0 | 95 | |
AhmedPlaymaker | 67:39b9ba6019b0 | 96 | void read_write_stats() |
AhmedPlaymaker | 67:39b9ba6019b0 | 97 | { |
AhmedPlaymaker | 67:39b9ba6019b0 | 98 | _start.read_stats(sd); //this is to save the current highest level in the stats class that can be used in menu. |
AhmedPlaymaker | 67:39b9ba6019b0 | 99 | _stats.write(1, sd); //this tells the stats class that the game has started from level 1; |
AhmedPlaymaker | 67:39b9ba6019b0 | 100 | } |
AhmedPlaymaker | 67:39b9ba6019b0 | 101 | |
AhmedPlaymaker | 67:39b9ba6019b0 | 102 | void _set_mode_speed() |
AhmedPlaymaker | 67:39b9ba6019b0 | 103 | { |
AhmedPlaymaker | 67:39b9ba6019b0 | 104 | fps = _start.fps; // sets the frames per second required, selected from the game speed menu. |
AhmedPlaymaker | 67:39b9ba6019b0 | 105 | g_mode = _start.g_mode;// allows us to pass this information on to the snakevsblock class, to set the controls to either joystick or motion control. |
AhmedPlaymaker | 67:39b9ba6019b0 | 106 | |
AhmedPlaymaker | 67:39b9ba6019b0 | 107 | if (g_mode == 2) { //show instructions to handle motion control. |
AhmedPlaymaker | 67:39b9ba6019b0 | 108 | _start.motionControlInstructions(lcd); //this only comes up on the screen is the user selects motion control from menu options. |
AhmedPlaymaker | 67:39b9ba6019b0 | 109 | } |
AhmedPlaymaker | 0:4b15c2d4aa58 | 110 | } |