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