Final Submission. I have read and agreed with Statement of Academic Integrity.
Dependencies: mbed Gamepad N5110 Joystick
main.cpp@5:23a85b16ee54, 2019-05-09 (annotated)
- Committer:
- el16dlc
- Date:
- Thu May 09 08:33:26 2019 +0000
- Revision:
- 5:23a85b16ee54
- Parent:
- 4:0fc3441556e1
- Child:
- 8:264398d3c610
6;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el16dlc | 1:b9f3b7d869d8 | 1 | /* |
el16dlc | 1:b9f3b7d869d8 | 2 | ELEC2645 Embedded Systems Project |
el16dlc | 1:b9f3b7d869d8 | 3 | School of Electronic & Electrical Engineering |
el16dlc | 1:b9f3b7d869d8 | 4 | University of Leeds |
el16dlc | 1:b9f3b7d869d8 | 5 | |
el16dlc | 1:b9f3b7d869d8 | 6 | Name: DANIEL CROCKFORD |
el16dlc | 1:b9f3b7d869d8 | 7 | Username: el16dlc |
el16dlc | 1:b9f3b7d869d8 | 8 | Student ID Number: 201039580 |
el16dlc | 1:b9f3b7d869d8 | 9 | Date: 07/05/2019 |
el16dlc | 1:b9f3b7d869d8 | 10 | */ |
el16dlc | 1:b9f3b7d869d8 | 11 | |
el16dlc | 1:b9f3b7d869d8 | 12 | |
el16dlc | 1:b9f3b7d869d8 | 13 | #include "main.h" |
el16dlc | 1:b9f3b7d869d8 | 14 | #include "Snake.h" |
el16dlc | 1:b9f3b7d869d8 | 15 | #include "Game_engine.h" |
el16dlc | 1:b9f3b7d869d8 | 16 | |
el16dlc | 1:b9f3b7d869d8 | 17 | // Gamepad Peripherals |
el16dlc | 1:b9f3b7d869d8 | 18 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el16dlc | 1:b9f3b7d869d8 | 19 | |
el16dlc | 1:b9f3b7d869d8 | 20 | Gamepad gamepad; |
el16dlc | 1:b9f3b7d869d8 | 21 | |
el16dlc | 1:b9f3b7d869d8 | 22 | // Instances |
el16dlc | 1:b9f3b7d869d8 | 23 | GameEngine engine; |
el16dlc | 1:b9f3b7d869d8 | 24 | |
el16dlc | 5:23a85b16ee54 | 25 | bool game_cont = false; |
el16dlc | 5:23a85b16ee54 | 26 | |
el16dlc | 3:660de4311976 | 27 | |
el16dlc | 1:b9f3b7d869d8 | 28 | int main() { |
el16dlc | 5:23a85b16ee54 | 29 | //srand(time(NULL)); |
el16dlc | 1:b9f3b7d869d8 | 30 | Init(); |
el16dlc | 1:b9f3b7d869d8 | 31 | Welcome(); |
el16dlc | 2:0bd6711eae26 | 32 | engine.init(); |
el16dlc | 5:23a85b16ee54 | 33 | //bool game_cont = true; |
el16dlc | 1:b9f3b7d869d8 | 34 | |
el16dlc | 1:b9f3b7d869d8 | 35 | |
el16dlc | 5:23a85b16ee54 | 36 | while(engine.get_game_cont() == true) { |
el16dlc | 1:b9f3b7d869d8 | 37 | render(); |
el16dlc | 4:0fc3441556e1 | 38 | update(); |
el16dlc | 1:b9f3b7d869d8 | 39 | } |
el16dlc | 5:23a85b16ee54 | 40 | lcd.clear(); |
el16dlc | 5:23a85b16ee54 | 41 | lcd.refresh(); |
el16dlc | 1:b9f3b7d869d8 | 42 | } |
el16dlc | 1:b9f3b7d869d8 | 43 | |
el16dlc | 1:b9f3b7d869d8 | 44 | |
el16dlc | 1:b9f3b7d869d8 | 45 | // Initialise gamepad |
el16dlc | 1:b9f3b7d869d8 | 46 | void Init() { |
el16dlc | 1:b9f3b7d869d8 | 47 | lcd.init(); |
el16dlc | 1:b9f3b7d869d8 | 48 | gamepad.init(); |
el16dlc | 1:b9f3b7d869d8 | 49 | lcd.setContrast(0.5); |
el16dlc | 1:b9f3b7d869d8 | 50 | lcd.setBrightness(0.5); |
el16dlc | 1:b9f3b7d869d8 | 51 | gamepad.leds_off(); |
el16dlc | 1:b9f3b7d869d8 | 52 | gamepad.check_event(Gamepad::A_PRESSED); |
el16dlc | 1:b9f3b7d869d8 | 53 | gamepad.check_event(Gamepad::B_PRESSED); |
el16dlc | 1:b9f3b7d869d8 | 54 | gamepad.check_event(Gamepad::X_PRESSED); |
el16dlc | 1:b9f3b7d869d8 | 55 | gamepad.check_event(Gamepad::Y_PRESSED); |
el16dlc | 1:b9f3b7d869d8 | 56 | gamepad.check_event(Gamepad::L_PRESSED); |
el16dlc | 1:b9f3b7d869d8 | 57 | gamepad.check_event(Gamepad::R_PRESSED); |
el16dlc | 1:b9f3b7d869d8 | 58 | gamepad.check_event(Gamepad::BACK_PRESSED); |
el16dlc | 5:23a85b16ee54 | 59 | gamepad.check_event(Gamepad::START_PRESSED); |
el16dlc | 1:b9f3b7d869d8 | 60 | } |
el16dlc | 1:b9f3b7d869d8 | 61 | |
el16dlc | 1:b9f3b7d869d8 | 62 | // Prototype welcome screen |
el16dlc | 1:b9f3b7d869d8 | 63 | void Welcome() { |
el16dlc | 1:b9f3b7d869d8 | 64 | lcd.clear(); |
el16dlc | 1:b9f3b7d869d8 | 65 | lcd.printString("Welcome",2,1); |
el16dlc | 1:b9f3b7d869d8 | 66 | lcd.refresh(); |
el16dlc | 1:b9f3b7d869d8 | 67 | wait(2.0); |
el16dlc | 1:b9f3b7d869d8 | 68 | } |
el16dlc | 1:b9f3b7d869d8 | 69 | |
el16dlc | 1:b9f3b7d869d8 | 70 | // Draws each frame on screen |
el16dlc | 1:b9f3b7d869d8 | 71 | void render() { |
el16dlc | 1:b9f3b7d869d8 | 72 | lcd.clear(); |
el16dlc | 2:0bd6711eae26 | 73 | engine.draw(lcd); |
el16dlc | 1:b9f3b7d869d8 | 74 | lcd.refresh(); |
el16dlc | 4:0fc3441556e1 | 75 | } |
el16dlc | 4:0fc3441556e1 | 76 | void update() { |
el16dlc | 5:23a85b16ee54 | 77 | engine.check_wall_collision(); |
el16dlc | 5:23a85b16ee54 | 78 | engine.check_snake_collision(); |
el16dlc | 4:0fc3441556e1 | 79 | engine.get_dir(gamepad); |
el16dlc | 4:0fc3441556e1 | 80 | engine.snake_move(); |
el16dlc | 4:0fc3441556e1 | 81 | engine.food_move(); |
el16dlc | 4:0fc3441556e1 | 82 | engine.snake_body(lcd); |
el16dlc | 4:0fc3441556e1 | 83 | lcd.refresh(); |
el16dlc | 3:660de4311976 | 84 | wait(0.2); |
el16dlc | 1:b9f3b7d869d8 | 85 | } |