submit
Dependencies: mbed Gamepad N5110
main.cpp@15:4123abb17291, 2019-04-22 (annotated)
- Committer:
- 694617778
- Date:
- Mon Apr 22 09:59:39 2019 +0000
- Revision:
- 15:4123abb17291
- Parent:
- 14:e1f7b4be1cf2
- Child:
- 16:a52b2441c5ae
gg
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
694617778 | 0:b67614a0c4cf | 1 | ///////// pre-processor directives //////// |
694617778 | 0:b67614a0c4cf | 2 | #include "mbed.h" |
694617778 | 0:b67614a0c4cf | 3 | #include "Gamepad.h" |
694617778 | 0:b67614a0c4cf | 4 | #include "N5110.h" |
694617778 | 1:b49c36604125 | 5 | #include "snake.h" |
694617778 | 1:b49c36604125 | 6 | #include "Engine.h" |
694617778 | 13:32772a08841e | 7 | #include "finger.h" |
694617778 | 0:b67614a0c4cf | 8 | |
694617778 | 0:b67614a0c4cf | 9 | /////////////// objects /////////////// |
694617778 | 0:b67614a0c4cf | 10 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
694617778 | 0:b67614a0c4cf | 11 | Gamepad pad; |
694617778 | 1:b49c36604125 | 12 | snake snake; |
694617778 | 1:b49c36604125 | 13 | Engine engine; |
694617778 | 13:32772a08841e | 14 | finger finger; |
694617778 | 0:b67614a0c4cf | 15 | ///////////// prototypes /////////////// |
694617778 | 0:b67614a0c4cf | 16 | void init(); |
694617778 | 0:b67614a0c4cf | 17 | void run(); |
694617778 | 4:323f42022d87 | 18 | void over(); |
694617778 | 10:68076e3dcc33 | 19 | |
694617778 | 0:b67614a0c4cf | 20 | ///////////// functions //////////////// |
694617778 | 0:b67614a0c4cf | 21 | int main() |
694617778 | 0:b67614a0c4cf | 22 | { |
694617778 | 0:b67614a0c4cf | 23 | int fps = 8; // frames per second |
694617778 | 11:543c62bed764 | 24 | snake.hscore = 0; |
694617778 | 11:543c62bed764 | 25 | lcd.init(); |
694617778 | 9:18b059e5abb9 | 26 | while(1){ |
694617778 | 15:4123abb17291 | 27 | init(); |
694617778 | 15:4123abb17291 | 28 | pad.tone(1500.0,0.5); |
694617778 | 9:18b059e5abb9 | 29 | engine.welcome(pad,lcd); // show welcome display, waiting for the user to start |
694617778 | 10:68076e3dcc33 | 30 | engine.menu(pad,lcd); // show the select display, waiting for the user to select |
694617778 | 4:323f42022d87 | 31 | // game loop - read input, update the game state and render the display |
694617778 | 14:e1f7b4be1cf2 | 32 | if(engine.game == 0){ |
694617778 | 9:18b059e5abb9 | 33 | while (snake.over == 0) { |
694617778 | 9:18b059e5abb9 | 34 | run(); // run the game |
694617778 | 10:68076e3dcc33 | 35 | while (pad.check_event(Gamepad::START_PRESSED) == true){ |
694617778 | 10:68076e3dcc33 | 36 | engine.pause(pad,lcd); |
694617778 | 13:32772a08841e | 37 | } |
694617778 | 9:18b059e5abb9 | 38 | wait(engine.p/fps); // and wait for one frame period |
694617778 | 9:18b059e5abb9 | 39 | } |
694617778 | 13:32772a08841e | 40 | over(); // show gameover display, waiting for the user to restart |
694617778 | 14:e1f7b4be1cf2 | 41 | }else if(engine.game == 1){ |
694617778 | 14:e1f7b4be1cf2 | 42 | finger.run(lcd,pad); |
694617778 | 14:e1f7b4be1cf2 | 43 | } |
694617778 | 2:934daa65f73d | 44 | } |
694617778 | 0:b67614a0c4cf | 45 | } |
694617778 | 0:b67614a0c4cf | 46 | |
694617778 | 0:b67614a0c4cf | 47 | // this function draws each frame on the LCD |
694617778 | 0:b67614a0c4cf | 48 | void run() |
694617778 | 0:b67614a0c4cf | 49 | { |
694617778 | 0:b67614a0c4cf | 50 | // clear screen, re-draw and refresh |
694617778 | 0:b67614a0c4cf | 51 | lcd.clear(); |
694617778 | 1:b49c36604125 | 52 | int length = snake.get_length(); |
694617778 | 6:feb6351e6f8e | 53 | int direction = engine.get_direction(pad); |
694617778 | 2:934daa65f73d | 54 | snake.check_eat(pad); |
694617778 | 1:b49c36604125 | 55 | snake.draw(lcd,length); |
694617778 | 1:b49c36604125 | 56 | snake.update(direction,length); |
694617778 | 2:934daa65f73d | 57 | snake.check_over(lcd); |
694617778 | 0:b67614a0c4cf | 58 | lcd.refresh(); |
694617778 | 0:b67614a0c4cf | 59 | } |
694617778 | 0:b67614a0c4cf | 60 | |
694617778 | 0:b67614a0c4cf | 61 | // initialies all classes and libraries |
694617778 | 0:b67614a0c4cf | 62 | void init() |
694617778 | 0:b67614a0c4cf | 63 | { |
694617778 | 0:b67614a0c4cf | 64 | // need to initialise LCD and Gamepad |
694617778 | 11:543c62bed764 | 65 | //lcd.init(); |
694617778 | 1:b49c36604125 | 66 | pad.init(); |
694617778 | 2:934daa65f73d | 67 | snake.init(2,3); |
694617778 | 6:feb6351e6f8e | 68 | engine.init(); |
694617778 | 13:32772a08841e | 69 | finger.init(); |
694617778 | 11:543c62bed764 | 70 | lcd.clear(); |
694617778 | 0:b67614a0c4cf | 71 | } |
694617778 | 0:b67614a0c4cf | 72 | |
694617778 | 4:323f42022d87 | 73 | void over() |
694617778 | 4:323f42022d87 | 74 | { |
694617778 | 9:18b059e5abb9 | 75 | int s = snake.get_score(); // get teh scores to show at the gameover display |
694617778 | 11:543c62bed764 | 76 | int hs = snake.hscore; |
694617778 | 11:543c62bed764 | 77 | engine.gameover(pad,lcd,s,hs); |
694617778 | 1:b49c36604125 | 78 | |
694617778 | 4:323f42022d87 | 79 | } |