submit

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Sun Apr 14 07:42:50 2019 +0000
Revision:
5:82094591b4b4
Parent:
4:323f42022d87
Child:
6:feb6351e6f8e
ff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
694617778 1:b49c36604125 1 #include "Engine.h"
694617778 3:1358cbb05ad3 2 #include "snake.h"
694617778 1:b49c36604125 3
694617778 1:b49c36604125 4
694617778 1:b49c36604125 5 Engine::Engine()
694617778 1:b49c36604125 6 {
694617778 1:b49c36604125 7
694617778 1:b49c36604125 8 }
694617778 1:b49c36604125 9
694617778 1:b49c36604125 10 Engine::~Engine()
694617778 1:b49c36604125 11 {
694617778 1:b49c36604125 12
694617778 1:b49c36604125 13 }
694617778 1:b49c36604125 14
694617778 1:b49c36604125 15 int Engine::get_direction(Gamepad &pad) {
694617778 1:b49c36604125 16 if (pad.check_event(Gamepad::A_PRESSED) == true && direction != 3){
694617778 1:b49c36604125 17 direction = 1;
694617778 1:b49c36604125 18 } else if (pad.check_event(Gamepad::B_PRESSED) == true && direction != 2){
694617778 1:b49c36604125 19 direction = 0;
694617778 1:b49c36604125 20 } else if (pad.check_event(Gamepad::X_PRESSED) == true && direction != 0){
694617778 1:b49c36604125 21 direction = 2;
694617778 1:b49c36604125 22 } else if (pad.check_event(Gamepad::Y_PRESSED) == true && direction != 1){
694617778 1:b49c36604125 23 direction = 3;
694617778 1:b49c36604125 24 }
694617778 1:b49c36604125 25 return direction;
694617778 1:b49c36604125 26
694617778 3:1358cbb05ad3 27 }
694617778 3:1358cbb05ad3 28
694617778 3:1358cbb05ad3 29 // simple splash screen displayed on start-up
694617778 3:1358cbb05ad3 30 void Engine::welcome(Gamepad &pad, N5110 &lcd) {
694617778 3:1358cbb05ad3 31
694617778 5:82094591b4b4 32 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 5:82094591b4b4 33 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 4:323f42022d87 34 lcd.printString(" ###SNAKE### ",4,1);
694617778 4:323f42022d87 35 lcd.printString(" Qi Minghong ",4,2);
694617778 5:82094591b4b4 36 lcd.printString(" Press Start ",4,4);
694617778 3:1358cbb05ad3 37 lcd.refresh();
694617778 3:1358cbb05ad3 38
694617778 3:1358cbb05ad3 39 // wait flashing LEDs until start button is pressed
694617778 3:1358cbb05ad3 40 while ( pad.check_event(Gamepad::START_PRESSED) == false ) {
694617778 3:1358cbb05ad3 41 pad.leds_on();
694617778 3:1358cbb05ad3 42 wait(0.1);
694617778 3:1358cbb05ad3 43 pad.leds_off();
694617778 3:1358cbb05ad3 44 wait(0.1);
694617778 3:1358cbb05ad3 45 }
694617778 3:1358cbb05ad3 46
694617778 3:1358cbb05ad3 47 }
694617778 3:1358cbb05ad3 48
694617778 3:1358cbb05ad3 49 // simple splash screen of game over
694617778 4:323f42022d87 50 void Engine::gameover(Gamepad &pad, N5110 &lcd, int score) {
694617778 4:323f42022d87 51 pad.tone(500.0,0.5);
694617778 3:1358cbb05ad3 52 lcd.init();
694617778 3:1358cbb05ad3 53 pad.init();
694617778 3:1358cbb05ad3 54 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 3:1358cbb05ad3 55 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 4:323f42022d87 56 lcd.printString(" GAME OVER ",4,1);
694617778 4:323f42022d87 57 // we need an array of chars to store the message
694617778 4:323f42022d87 58 char buffer[14]; // max screen witdth is 14
694617778 4:323f42022d87 59 // print message to buffer
694617778 4:323f42022d87 60 sprintf(buffer," SCORE : %d ",score);
694617778 5:82094591b4b4 61 lcd.printString(buffer,2,2);
694617778 4:323f42022d87 62 lcd.printString(" thanks for ",2,3);
694617778 4:323f42022d87 63 lcd.printString(" playing ",4,4);
694617778 3:1358cbb05ad3 64 lcd.refresh();
694617778 3:1358cbb05ad3 65
694617778 3:1358cbb05ad3 66 // wait flashing LEDs until start button is pressed
694617778 3:1358cbb05ad3 67 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
694617778 3:1358cbb05ad3 68 pad.leds_on();
694617778 3:1358cbb05ad3 69 wait(0.1);
694617778 3:1358cbb05ad3 70 pad.leds_off();
694617778 3:1358cbb05ad3 71 wait(0.1);
694617778 3:1358cbb05ad3 72 }
694617778 3:1358cbb05ad3 73
694617778 1:b49c36604125 74 }