submit

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Sat Apr 13 10:07:50 2019 +0000
Revision:
1:b49c36604125
Parent:
0:b67614a0c4cf
Child:
2:934daa65f73d
test2

Who changed what in which revision?

UserRevisionLine numberNew 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 /////////////// structs /////////////////
694617778 0:b67614a0c4cf 9 struct UserInput {
694617778 0:b67614a0c4cf 10 Direction d;
694617778 0:b67614a0c4cf 11 float mag;
694617778 0:b67614a0c4cf 12 };
694617778 1:b49c36604125 13
694617778 0:b67614a0c4cf 14 /////////////// objects ///////////////
694617778 0:b67614a0c4cf 15 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
694617778 0:b67614a0c4cf 16 Gamepad pad;
694617778 1:b49c36604125 17 snake snake;
694617778 1:b49c36604125 18 Engine engine;
694617778 0:b67614a0c4cf 19
694617778 0:b67614a0c4cf 20 ///////////// prototypes ///////////////
694617778 0:b67614a0c4cf 21 void init();
694617778 0:b67614a0c4cf 22 void update_game(UserInput input);
694617778 0:b67614a0c4cf 23 void run();
694617778 0:b67614a0c4cf 24 void welcome();
694617778 0:b67614a0c4cf 25
694617778 0:b67614a0c4cf 26 ///////////// functions ////////////////
694617778 0:b67614a0c4cf 27 int main()
694617778 0:b67614a0c4cf 28 {
694617778 0:b67614a0c4cf 29 int fps = 8; // frames per second
694617778 0:b67614a0c4cf 30
694617778 0:b67614a0c4cf 31 init();
694617778 0:b67614a0c4cf 32 welcome(); // show welcome display, waiting for the user to start
694617778 0:b67614a0c4cf 33
694617778 1:b49c36604125 34
694617778 1:b49c36604125 35 while (1) {
694617778 1:b49c36604125 36
694617778 1:b49c36604125 37 run();
694617778 1:b49c36604125 38 wait(5.0f/fps); // and wait for one frame period
694617778 1:b49c36604125 39 }
694617778 0:b67614a0c4cf 40
694617778 0:b67614a0c4cf 41 // game loop - read input, update the game state and render the display
694617778 1:b49c36604125 42
694617778 0:b67614a0c4cf 43 }
694617778 0:b67614a0c4cf 44
694617778 0:b67614a0c4cf 45 // simple splash screen displayed on start-up
694617778 0:b67614a0c4cf 46 void welcome() {
694617778 0:b67614a0c4cf 47
694617778 0:b67614a0c4cf 48 lcd.printString(" ###SNAKE### ",0,1);
694617778 0:b67614a0c4cf 49 lcd.printString(" Press Start ",0,4);
694617778 0:b67614a0c4cf 50 lcd.refresh();
694617778 0:b67614a0c4cf 51
694617778 0:b67614a0c4cf 52 // wait flashing LEDs until start button is pressed
694617778 0:b67614a0c4cf 53 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
694617778 0:b67614a0c4cf 54 pad.leds_on();
694617778 0:b67614a0c4cf 55 wait(0.1);
694617778 0:b67614a0c4cf 56 pad.leds_off();
694617778 0:b67614a0c4cf 57 wait(0.1);
694617778 0:b67614a0c4cf 58 }
694617778 0:b67614a0c4cf 59
694617778 0:b67614a0c4cf 60 }
694617778 0:b67614a0c4cf 61
694617778 0:b67614a0c4cf 62 // this function draws each frame on the LCD
694617778 0:b67614a0c4cf 63 void run()
694617778 0:b67614a0c4cf 64 {
694617778 0:b67614a0c4cf 65 // clear screen, re-draw and refresh
694617778 0:b67614a0c4cf 66 lcd.clear();
694617778 1:b49c36604125 67 int length = snake.get_length();
694617778 1:b49c36604125 68 snake.draw(lcd,length);
694617778 1:b49c36604125 69 int direction = engine.get_direction(pad);
694617778 1:b49c36604125 70 snake.update(direction,length);
694617778 0:b67614a0c4cf 71 lcd.refresh();
694617778 0:b67614a0c4cf 72 }
694617778 0:b67614a0c4cf 73
694617778 0:b67614a0c4cf 74 // initialies all classes and libraries
694617778 0:b67614a0c4cf 75 void init()
694617778 0:b67614a0c4cf 76 {
694617778 0:b67614a0c4cf 77 // need to initialise LCD and Gamepad
694617778 0:b67614a0c4cf 78 lcd.init();
694617778 1:b49c36604125 79 pad.init();
694617778 1:b49c36604125 80 snake.init(1,3);
694617778 0:b67614a0c4cf 81
694617778 0:b67614a0c4cf 82 }
694617778 0:b67614a0c4cf 83
694617778 1:b49c36604125 84
694617778 1:b49c36604125 85