submit

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Sat Apr 13 15:45:03 2019 +0000
Revision:
2:934daa65f73d
Parent:
1:b49c36604125
Child:
3:1358cbb05ad3
t3

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 /////////////// 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 0:b67614a0c4cf 17 void welcome();
694617778 0:b67614a0c4cf 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 0:b67614a0c4cf 23
694617778 0:b67614a0c4cf 24 init();
694617778 0:b67614a0c4cf 25 welcome(); // show welcome display, waiting for the user to start
694617778 0:b67614a0c4cf 26
694617778 2:934daa65f73d 27 // game loop - read input, update the game state and render the display
694617778 2:934daa65f73d 28 while (snake.over == 0) {
694617778 1:b49c36604125 29 run();
694617778 2:934daa65f73d 30 wait(1.0f/fps); // and wait for one frame period
694617778 2:934daa65f73d 31 }
694617778 1:b49c36604125 32
694617778 2:934daa65f73d 33
694617778 0:b67614a0c4cf 34 }
694617778 0:b67614a0c4cf 35
694617778 0:b67614a0c4cf 36 // simple splash screen displayed on start-up
694617778 0:b67614a0c4cf 37 void welcome() {
694617778 0:b67614a0c4cf 38
694617778 0:b67614a0c4cf 39 lcd.printString(" ###SNAKE### ",0,1);
694617778 0:b67614a0c4cf 40 lcd.printString(" Press Start ",0,4);
694617778 0:b67614a0c4cf 41 lcd.refresh();
694617778 0:b67614a0c4cf 42
694617778 0:b67614a0c4cf 43 // wait flashing LEDs until start button is pressed
694617778 0:b67614a0c4cf 44 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
694617778 0:b67614a0c4cf 45 pad.leds_on();
694617778 0:b67614a0c4cf 46 wait(0.1);
694617778 0:b67614a0c4cf 47 pad.leds_off();
694617778 0:b67614a0c4cf 48 wait(0.1);
694617778 0:b67614a0c4cf 49 }
694617778 0:b67614a0c4cf 50
694617778 0:b67614a0c4cf 51 }
694617778 0:b67614a0c4cf 52
694617778 0:b67614a0c4cf 53 // this function draws each frame on the LCD
694617778 0:b67614a0c4cf 54 void run()
694617778 0:b67614a0c4cf 55 {
694617778 0:b67614a0c4cf 56 // clear screen, re-draw and refresh
694617778 0:b67614a0c4cf 57 lcd.clear();
694617778 1:b49c36604125 58 int length = snake.get_length();
694617778 2:934daa65f73d 59 snake.check_eat(pad);
694617778 1:b49c36604125 60 snake.draw(lcd,length);
694617778 1:b49c36604125 61 int direction = engine.get_direction(pad);
694617778 1:b49c36604125 62 snake.update(direction,length);
694617778 2:934daa65f73d 63 snake.check_over(lcd);
694617778 0:b67614a0c4cf 64 lcd.refresh();
694617778 0:b67614a0c4cf 65 }
694617778 0:b67614a0c4cf 66
694617778 0:b67614a0c4cf 67 // initialies all classes and libraries
694617778 0:b67614a0c4cf 68 void init()
694617778 0:b67614a0c4cf 69 {
694617778 0:b67614a0c4cf 70 // need to initialise LCD and Gamepad
694617778 0:b67614a0c4cf 71 lcd.init();
694617778 1:b49c36604125 72 pad.init();
694617778 2:934daa65f73d 73 snake.init(2,3);
694617778 0:b67614a0c4cf 74
694617778 0:b67614a0c4cf 75 }
694617778 0:b67614a0c4cf 76
694617778 1:b49c36604125 77
694617778 1:b49c36604125 78