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