Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
main.cpp@70:7caab8069b9b, 2019-05-04 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Sat May 04 20:48:10 2019 +0000
- Revision:
- 70:7caab8069b9b
- Parent:
- 69:55e309da7efd
- Child:
- 72:4d49d2544cef
Thanks to my newly created test files, I removed some of the most uselessly redundant functions in my snake class relating to get pos. The test classes helped me debug the problems with the bulky code by letting me know what coordinates were saved.
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 | 69:55e309da7efd | 20 | #ifdef TEST_SNAKE |
AhmedPlaymaker | 68:b9cfd27987ac | 21 | # include "tests.h" |
AhmedPlaymaker | 68:b9cfd27987ac | 22 | #endif |
AhmedPlaymaker | 68:b9cfd27987ac | 23 | |
AhmedPlaymaker | 1:32e312688a65 | 24 | /////////////// objects /////////////// |
AhmedPlaymaker | 1:32e312688a65 | 25 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
AhmedPlaymaker | 1:32e312688a65 | 26 | Gamepad pad; |
AhmedPlaymaker | 1:32e312688a65 | 27 | FXOS8700CQ device(I2C_SDA,I2C_SCL); |
AhmedPlaymaker | 3:fbb1fa853f09 | 28 | StartScreen _start; |
AhmedPlaymaker | 66:e47333ffc6ca | 29 | Stats _stats; |
AhmedPlaymaker | 66:e47333ffc6ca | 30 | SnakevsBlock _game; |
AhmedPlaymaker | 32:3a3bdeffdf62 | 31 | AnalogIn noisy(PTB0); //This creates a random noise which I can use to seed the random numbers. |
AhmedPlaymaker | 33:249cf423fb18 | 32 | // Connections to SD card holder on K64F (SPI interface) |
AhmedPlaymaker | 33:249cf423fb18 | 33 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
AhmedPlaymaker | 33:249cf423fb18 | 34 | //Serial serial(USBTX, USBRX); // for PC debug |
AhmedPlaymaker | 1:32e312688a65 | 35 | |
AhmedPlaymaker | 7:48ba87cd79b5 | 36 | ///////////// prototypes ////////////// |
AhmedPlaymaker | 1:32e312688a65 | 37 | void init(); |
AhmedPlaymaker | 1:32e312688a65 | 38 | void refresh_game(); |
AhmedPlaymaker | 69:55e309da7efd | 39 | void menu(); |
AhmedPlaymaker | 67:39b9ba6019b0 | 40 | void read_write_stats(); |
AhmedPlaymaker | 67:39b9ba6019b0 | 41 | void _set_mode_speed(); |
AhmedPlaymaker | 69:55e309da7efd | 42 | void gameLoop(); |
AhmedPlaymaker | 68:b9cfd27987ac | 43 | //void deinit(); |
AhmedPlaymaker | 1:32e312688a65 | 44 | |
AhmedPlaymaker | 2:83e85dea3c89 | 45 | //Constants// |
AhmedPlaymaker | 46:dc7dccae9f9e | 46 | int fps = 40; // frames per second, this will be changed in menu. |
AhmedPlaymaker | 39:210ac915e0a0 | 47 | int g_mode = 1; //selects between joystick and motion control. |
AhmedPlaymaker | 54:20abd16c7d74 | 48 | int back; //this allows us to use the back key to exit the game loop; |
AhmedPlaymaker | 2:83e85dea3c89 | 49 | |
AhmedPlaymaker | 1:32e312688a65 | 50 | ///////////// MAIN //////////////// |
AhmedPlaymaker | 1:32e312688a65 | 51 | int main() |
AhmedPlaymaker | 1:32e312688a65 | 52 | { |
AhmedPlaymaker | 69:55e309da7efd | 53 | #ifdef TEST_SNAKE |
AhmedPlaymaker | 68:b9cfd27987ac | 54 | int number_of_failures = run_all_tests(); |
AhmedPlaymaker | 68:b9cfd27987ac | 55 | |
AhmedPlaymaker | 68:b9cfd27987ac | 56 | if(number_of_failures > 0) return number_of_failures; |
AhmedPlaymaker | 68:b9cfd27987ac | 57 | #endif |
AhmedPlaymaker | 68:b9cfd27987ac | 58 | |
AhmedPlaymaker | 1:32e312688a65 | 59 | init(); |
AhmedPlaymaker | 66:e47333ffc6ca | 60 | _start.titleScreen(lcd, pad); |
AhmedPlaymaker | 32:3a3bdeffdf62 | 61 | while(1) { //This loop is created for Play/Continue configuration |
AhmedPlaymaker | 70:7caab8069b9b | 62 | menu(); //pops up the menu by calling the StartScreen Classes. |
AhmedPlaymaker | 70:7caab8069b9b | 63 | |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 64 | // start the game |
AhmedPlaymaker | 67:39b9ba6019b0 | 65 | _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 | 62:ebf6ecf8a6d5 | 66 | wait(1.0f/fps); |
AhmedPlaymaker | 69:55e309da7efd | 67 | // snakeVSblock game loop - detect input respect to the menu options, and update data and refresh screen |
AhmedPlaymaker | 69:55e309da7efd | 68 | gameLoop(); |
AhmedPlaymaker | 1:32e312688a65 | 69 | } |
AhmedPlaymaker | 1:32e312688a65 | 70 | } |
AhmedPlaymaker | 1:32e312688a65 | 71 | |
AhmedPlaymaker | 70:7caab8069b9b | 72 | //MAIN_FUNCTIONS |
AhmedPlaymaker | 70:7caab8069b9b | 73 | |
AhmedPlaymaker | 70:7caab8069b9b | 74 | //INIT |
AhmedPlaymaker | 1:32e312688a65 | 75 | void init() |
AhmedPlaymaker | 1:32e312688a65 | 76 | { |
AhmedPlaymaker | 62:ebf6ecf8a6d5 | 77 | // need to initialise LCD and Gamepad |
AhmedPlaymaker | 56:142e9fdb77a8 | 78 | lcd.init(); //init for the N5110 Library. |
AhmedPlaymaker | 56:142e9fdb77a8 | 79 | device.init(); //init for the FXOS8700CQ Library. |
AhmedPlaymaker | 56:142e9fdb77a8 | 80 | pad.init(); //init for the Gamepad Library. |
AhmedPlaymaker | 66:e47333ffc6ca | 81 | _game.init(); //init for the SnakeVSBlock Class. |
AhmedPlaymaker | 56:142e9fdb77a8 | 82 | _start.init(); //init for the Menu Class --> StartScreen. |
AhmedPlaymaker | 56:142e9fdb77a8 | 83 | srand(100000*noisy.read_u16()); //seeds the random number generator with a random noise from the K64F. |
AhmedPlaymaker | 1:32e312688a65 | 84 | } |
AhmedPlaymaker | 1:32e312688a65 | 85 | |
AhmedPlaymaker | 70:7caab8069b9b | 86 | //MENU |
AhmedPlaymaker | 69:55e309da7efd | 87 | void menu() |
AhmedPlaymaker | 69:55e309da7efd | 88 | { |
AhmedPlaymaker | 69:55e309da7efd | 89 | read_write_stats(); //inside menu because used in a menu function for displaying the highest level saved. |
AhmedPlaymaker | 69:55e309da7efd | 90 | _start.menu(lcd, pad); // this takes us to main menu inside startscreen, and connects automatically to all other menu functions. |
AhmedPlaymaker | 69:55e309da7efd | 91 | _set_mode_speed(); //takes all the data collected from the menu to configure the game. |
AhmedPlaymaker | 69:55e309da7efd | 92 | } |
AhmedPlaymaker | 69:55e309da7efd | 93 | |
AhmedPlaymaker | 70:7caab8069b9b | 94 | //REFRESH GAME. |
AhmedPlaymaker | 1:32e312688a65 | 95 | void refresh_game() |
AhmedPlaymaker | 1:32e312688a65 | 96 | { |
AhmedPlaymaker | 56:142e9fdb77a8 | 97 | lcd.clear(); //clears the N5110 screen for the next frame |
AhmedPlaymaker | 66:e47333ffc6ca | 98 | _game.draw(lcd, pad); //draws the next game frame |
AhmedPlaymaker | 66:e47333ffc6ca | 99 | _game.get_pos(); //takes the game object coordinates and saves it privately to use later for implementing collisions. |
AhmedPlaymaker | 56:142e9fdb77a8 | 100 | lcd.refresh(); //refreshes the N5110 screen to display the frame. |
AhmedPlaymaker | 67:39b9ba6019b0 | 101 | } |
AhmedPlaymaker | 67:39b9ba6019b0 | 102 | |
AhmedPlaymaker | 70:7caab8069b9b | 103 | //READ AND WRITE STATS. |
AhmedPlaymaker | 67:39b9ba6019b0 | 104 | void read_write_stats() |
AhmedPlaymaker | 67:39b9ba6019b0 | 105 | { |
AhmedPlaymaker | 67:39b9ba6019b0 | 106 | _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 | 107 | _stats.write(1, sd); //this tells the stats class that the game has started from level 1; |
AhmedPlaymaker | 67:39b9ba6019b0 | 108 | } |
AhmedPlaymaker | 67:39b9ba6019b0 | 109 | |
AhmedPlaymaker | 70:7caab8069b9b | 110 | //SET MODE AND SPEED. |
AhmedPlaymaker | 67:39b9ba6019b0 | 111 | void _set_mode_speed() |
AhmedPlaymaker | 67:39b9ba6019b0 | 112 | { |
AhmedPlaymaker | 67:39b9ba6019b0 | 113 | fps = _start.fps; // sets the frames per second required, selected from the game speed menu. |
AhmedPlaymaker | 67:39b9ba6019b0 | 114 | 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 | 68:b9cfd27987ac | 115 | |
AhmedPlaymaker | 67:39b9ba6019b0 | 116 | if (g_mode == 2) { //show instructions to handle motion control. |
AhmedPlaymaker | 67:39b9ba6019b0 | 117 | _start.motionControlInstructions(lcd); //this only comes up on the screen is the user selects motion control from menu options. |
AhmedPlaymaker | 67:39b9ba6019b0 | 118 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 119 | } |
AhmedPlaymaker | 69:55e309da7efd | 120 | |
AhmedPlaymaker | 70:7caab8069b9b | 121 | //GAME LOOP. |
AhmedPlaymaker | 69:55e309da7efd | 122 | void gameLoop() |
AhmedPlaymaker | 69:55e309da7efd | 123 | { |
AhmedPlaymaker | 69:55e309da7efd | 124 | while (1) { |
AhmedPlaymaker | 70:7caab8069b9b | 125 | refresh_game(); |
AhmedPlaymaker | 69:55e309da7efd | 126 | _game.read_input(pad, device, g_mode); //this reads the angle or joystick direction, on the condition of either of them being selected. |
AhmedPlaymaker | 69:55e309da7efd | 127 | _game.update(lcd, pad); //updates the game screen and checks for any collisions. |
AhmedPlaymaker | 69:55e309da7efd | 128 | |
AhmedPlaymaker | 69:55e309da7efd | 129 | //the int back stores the value 1 if back is pressed inside the update function of snakevsblock, |
AhmedPlaymaker | 69:55e309da7efd | 130 | //This function also handles level progression and level failure operations by using the class WinLoose. |
AhmedPlaymaker | 69:55e309da7efd | 131 | back = _game.CheckGameProgression(lcd, pad, sd); //and also sends relevant data to the sd card to implement stats functionality. |
AhmedPlaymaker | 69:55e309da7efd | 132 | |
AhmedPlaymaker | 69:55e309da7efd | 133 | if(back) { |
AhmedPlaymaker | 69:55e309da7efd | 134 | break; //and this allows us to return to main menu by using the keyword break. |
AhmedPlaymaker | 69:55e309da7efd | 135 | } |
AhmedPlaymaker | 69:55e309da7efd | 136 | |
AhmedPlaymaker | 69:55e309da7efd | 137 | wait(1.0f/fps); |
AhmedPlaymaker | 69:55e309da7efd | 138 | } |
AhmedPlaymaker | 69:55e309da7efd | 139 | } |
AhmedPlaymaker | 69:55e309da7efd | 140 | |
AhmedPlaymaker | 68:b9cfd27987ac | 141 | /* |
AhmedPlaymaker | 68:b9cfd27987ac | 142 | void deinit() |
AhmedPlaymaker | 68:b9cfd27987ac | 143 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 144 | // need to deinitialise to quit. |
AhmedPlaymaker | 68:b9cfd27987ac | 145 | lcd.~N5110(); //deinit for the N5110 Library. |
AhmedPlaymaker | 68:b9cfd27987ac | 146 | pad.~Gamepad(); //deinit for the Gamepad Library. |
AhmedPlaymaker | 68:b9cfd27987ac | 147 | device.~FXOS8700CQ(); //deinit for the FXOS8700CQ Library. |
AhmedPlaymaker | 68:b9cfd27987ac | 148 | |
AhmedPlaymaker | 68:b9cfd27987ac | 149 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 150 | */ |