
Final Commit
Dependencies: mbed
main.cpp@27:bd0f69a75d8b, 2018-05-08 (annotated)
- Committer:
- JRM1986
- Date:
- Tue May 08 12:32:46 2018 +0000
- Revision:
- 27:bd0f69a75d8b
- Parent:
- 26:23301f48c1ed
Final Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JRM1986 | 3:50f01159c61d | 1 | #include "mbed.h" |
JRM1986 | 3:50f01159c61d | 2 | #include "Gamepad.h" |
JRM1986 | 3:50f01159c61d | 3 | #include "N5110.h" |
JRM1986 | 6:f3f508cea1c4 | 4 | #include "SnakeEngine.h" |
JRM1986 | 6:f3f508cea1c4 | 5 | |
JRM1986 | 25:f03439ee32c6 | 6 | |
JRM1986 | 25:f03439ee32c6 | 7 | |
JRM1986 | 8:a2b431b9b3f7 | 8 | #define FOOD_X 20 |
JRM1986 | 8:a2b431b9b3f7 | 9 | #define FOOD_Y 20 |
JRM1986 | 20:277e88a8185f | 10 | #define SNAKE_P_X 42 |
JRM1986 | 20:277e88a8185f | 11 | #define SNAKE_P_Y 24 |
JRM1986 | 20:277e88a8185f | 12 | #define IN E |
JRM1986 | 20:277e88a8185f | 13 | #define CUR E |
JRM1986 | 25:f03439ee32c6 | 14 | #define SPAWN_TIME 200 |
JRM1986 | 25:f03439ee32c6 | 15 | |
JRM1986 | 25:f03439ee32c6 | 16 | #ifndef WITH_TESTING |
JRM1986 | 25:f03439ee32c6 | 17 | #include "tests.h" |
JRM1986 | 25:f03439ee32c6 | 18 | //#endif |
JRM1986 | 13:72bc2579e85e | 19 | |
JRM1986 | 6:f3f508cea1c4 | 20 | struct UserInput |
JRM1986 | 6:f3f508cea1c4 | 21 | { |
JRM1986 | 6:f3f508cea1c4 | 22 | |
JRM1986 | 6:f3f508cea1c4 | 23 | Direction d; |
JRM1986 | 6:f3f508cea1c4 | 24 | |
JRM1986 | 6:f3f508cea1c4 | 25 | }; |
JRM1986 | 6:f3f508cea1c4 | 26 | |
JRM1986 | 12:9f2f64016f56 | 27 | |
JRM1986 | 0:53ee0f689634 | 28 | /* |
JRM1986 | 0:53ee0f689634 | 29 | ELEC2645 Embedded Systems Project |
JRM1986 | 7:c38800a428a6 | 30 | School of Electronic & Electrical Engineering |
JRM1986 | 0:53ee0f689634 | 31 | University of Leeds |
JRM1986 | 0:53ee0f689634 | 32 | Name: Joshua Robert Marshall |
JRM1986 | 0:53ee0f689634 | 33 | Username: ll13jrm |
JRM1986 | 0:53ee0f689634 | 34 | Student ID Number: 200764543 |
JRM1986 | 0:53ee0f689634 | 35 | Date: 28/02/2018 |
JRM1986 | 3:50f01159c61d | 36 | */ |
JRM1986 | 3:50f01159c61d | 37 | |
JRM1986 | 6:f3f508cea1c4 | 38 | // ----- Objects ----- |
JRM1986 | 6:f3f508cea1c4 | 39 | |
JRM1986 | 6:f3f508cea1c4 | 40 | SnakeEngine snake_engine; |
JRM1986 | 6:f3f508cea1c4 | 41 | Gamepad pad; |
JRM1986 | 10:62d8cb7742c3 | 42 | Snake snake; |
JRM1986 | 6:f3f508cea1c4 | 43 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
JRM1986 | 25:f03439ee32c6 | 44 | //Testing test1; |
JRM1986 | 3:50f01159c61d | 45 | |
JRM1986 | 6:f3f508cea1c4 | 46 | // ----- Prototypes ----- |
JRM1986 | 6:f3f508cea1c4 | 47 | |
JRM1986 | 6:f3f508cea1c4 | 48 | void init(); |
JRM1986 | 6:f3f508cea1c4 | 49 | void update_game(UserInput input); |
JRM1986 | 6:f3f508cea1c4 | 50 | void render(); |
JRM1986 | 25:f03439ee32c6 | 51 | void start_screen(); |
JRM1986 | 6:f3f508cea1c4 | 52 | |
JRM1986 | 3:50f01159c61d | 53 | int main() { |
JRM1986 | 8:a2b431b9b3f7 | 54 | |
JRM1986 | 25:f03439ee32c6 | 55 | //#ifdef WITH_TESTING |
JRM1986 | 25:f03439ee32c6 | 56 | int number_of_failures = run_tests(); |
JRM1986 | 25:f03439ee32c6 | 57 | |
JRM1986 | 25:f03439ee32c6 | 58 | if(number_of_failures > 0) return number_of_failures; |
JRM1986 | 25:f03439ee32c6 | 59 | #endif |
JRM1986 | 8:a2b431b9b3f7 | 60 | init(); |
JRM1986 | 25:f03439ee32c6 | 61 | start_screen(); |
JRM1986 | 13:72bc2579e85e | 62 | render(); |
JRM1986 | 20:277e88a8185f | 63 | |
JRM1986 | 21:63c5590cb2c2 | 64 | wait(0.2); |
JRM1986 | 20:277e88a8185f | 65 | |
JRM1986 | 8:a2b431b9b3f7 | 66 | while(1) { |
JRM1986 | 25:f03439ee32c6 | 67 | |
JRM1986 | 25:f03439ee32c6 | 68 | |
JRM1986 | 13:72bc2579e85e | 69 | snake_engine.get_input(pad); |
JRM1986 | 10:62d8cb7742c3 | 70 | snake_engine.update(pad); |
JRM1986 | 9:561e5681b7a6 | 71 | render(); |
JRM1986 | 16:85ca9feccf3f | 72 | |
JRM1986 | 25:f03439ee32c6 | 73 | wait(0.15); |
JRM1986 | 10:62d8cb7742c3 | 74 | |
JRM1986 | 16:85ca9feccf3f | 75 | } |
JRM1986 | 6:f3f508cea1c4 | 76 | } |
JRM1986 | 3:50f01159c61d | 77 | |
JRM1986 | 6:f3f508cea1c4 | 78 | // initialies all classes and libraries |
JRM1986 | 6:f3f508cea1c4 | 79 | void init() |
JRM1986 | 6:f3f508cea1c4 | 80 | { |
JRM1986 | 6:f3f508cea1c4 | 81 | // need to initialise LCD and Gamepad |
JRM1986 | 6:f3f508cea1c4 | 82 | lcd.init(); |
JRM1986 | 6:f3f508cea1c4 | 83 | pad.init(); |
JRM1986 | 6:f3f508cea1c4 | 84 | |
JRM1986 | 6:f3f508cea1c4 | 85 | // initialise the game with food, snake... |
JRM1986 | 25:f03439ee32c6 | 86 | snake_engine.init(IN, CUR, SNAKE_P_X, SNAKE_P_Y, SPAWN_TIME); |
JRM1986 | 6:f3f508cea1c4 | 87 | |
JRM1986 | 6:f3f508cea1c4 | 88 | } |
JRM1986 | 6:f3f508cea1c4 | 89 | |
JRM1986 | 6:f3f508cea1c4 | 90 | // this function draws each frame on the LCD |
JRM1986 | 6:f3f508cea1c4 | 91 | void render() |
JRM1986 | 6:f3f508cea1c4 | 92 | { |
JRM1986 | 6:f3f508cea1c4 | 93 | // clear screen, re-draw and refresh |
JRM1986 | 8:a2b431b9b3f7 | 94 | lcd.clear(); |
JRM1986 | 8:a2b431b9b3f7 | 95 | snake_engine.draw(lcd); |
JRM1986 | 6:f3f508cea1c4 | 96 | lcd.refresh(); |
JRM1986 | 8:a2b431b9b3f7 | 97 | |
JRM1986 | 6:f3f508cea1c4 | 98 | } |
JRM1986 | 6:f3f508cea1c4 | 99 | |
JRM1986 | 25:f03439ee32c6 | 100 | void start_screen() |
JRM1986 | 16:85ca9feccf3f | 101 | { |
JRM1986 | 25:f03439ee32c6 | 102 | int i = 0; |
JRM1986 | 25:f03439ee32c6 | 103 | lcd.setContrast(0.5); |
JRM1986 | 25:f03439ee32c6 | 104 | for(i = 0; i <= 4; ++i) { |
JRM1986 | 25:f03439ee32c6 | 105 | |
JRM1986 | 25:f03439ee32c6 | 106 | lcd.printString(" NOTENDO ", 0,i); |
JRM1986 | 25:f03439ee32c6 | 107 | lcd.refresh(); |
JRM1986 | 25:f03439ee32c6 | 108 | wait(0.3); |
JRM1986 | 25:f03439ee32c6 | 109 | lcd.clear(); |
JRM1986 | 25:f03439ee32c6 | 110 | |
JRM1986 | 25:f03439ee32c6 | 111 | |
JRM1986 | 25:f03439ee32c6 | 112 | if(i == 4) { |
JRM1986 | 25:f03439ee32c6 | 113 | |
JRM1986 | 25:f03439ee32c6 | 114 | lcd.printString(" NOTENDO ", 0,5); |
JRM1986 | 25:f03439ee32c6 | 115 | lcd.refresh(); |
JRM1986 | 25:f03439ee32c6 | 116 | pad.tone(0,1.75); |
JRM1986 | 25:f03439ee32c6 | 117 | wait(1.5); |
JRM1986 | 25:f03439ee32c6 | 118 | lcd.clear(); |
JRM1986 | 25:f03439ee32c6 | 119 | |
JRM1986 | 25:f03439ee32c6 | 120 | } |
JRM1986 | 25:f03439ee32c6 | 121 | |
JRM1986 | 25:f03439ee32c6 | 122 | } |
JRM1986 | 25:f03439ee32c6 | 123 | |
JRM1986 | 25:f03439ee32c6 | 124 | while ( pad.check_event(Gamepad::START_PRESSED) == false ) { |
JRM1986 | 25:f03439ee32c6 | 125 | |
JRM1986 | 25:f03439ee32c6 | 126 | |
JRM1986 | 25:f03439ee32c6 | 127 | lcd.printString(" Welcome ", 0, 1); |
JRM1986 | 25:f03439ee32c6 | 128 | lcd.printString(" Press Start ", 0, 3); |
JRM1986 | 25:f03439ee32c6 | 129 | lcd.refresh(); |
JRM1986 | 25:f03439ee32c6 | 130 | |
JRM1986 | 25:f03439ee32c6 | 131 | } |
JRM1986 | 25:f03439ee32c6 | 132 | |
JRM1986 | 6:f3f508cea1c4 | 133 | } |