submit

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Tue Apr 23 13:40:08 2019 +0000
Revision:
20:6ef06ba15666
Parent:
18:2ca7bd135b68
Child:
21:5861b5baf4b0
add  more description

Who changed what in which revision?

UserRevisionLine numberNew contents of line
694617778 1:b49c36604125 1 #include "Engine.h"
694617778 13:32772a08841e 2
694617778 20:6ef06ba15666 3 // nothing doing in the constructor and destructor
694617778 20:6ef06ba15666 4 Engine::Engine()
694617778 20:6ef06ba15666 5 {
694617778 20:6ef06ba15666 6
694617778 20:6ef06ba15666 7 }
694617778 20:6ef06ba15666 8
694617778 20:6ef06ba15666 9 Engine::~Engine()
694617778 20:6ef06ba15666 10 {
694617778 20:6ef06ba15666 11
694617778 20:6ef06ba15666 12 }
694617778 20:6ef06ba15666 13 // draw the pointer
694617778 12:684f3ff512d8 14 int sprite[8][8] = {
694617778 12:684f3ff512d8 15 {0,0,0,1,1,0,0,0},
694617778 12:684f3ff512d8 16 {0,0,1,1,0,0,0,0},
694617778 12:684f3ff512d8 17 {0,1,1,0,0,0,0,0},
694617778 12:684f3ff512d8 18 {1,1,1,1,1,1,1,0},
694617778 12:684f3ff512d8 19 {1,1,1,1,1,1,1,0},
694617778 12:684f3ff512d8 20 {0,1,1,0,0,0,0,0},
694617778 12:684f3ff512d8 21 {0,0,1,1,0,0,0,0},
694617778 12:684f3ff512d8 22 {0,0,0,1,1,0,0,0},
694617778 12:684f3ff512d8 23 };
694617778 20:6ef06ba15666 24 // initialise the components of engine
694617778 20:6ef06ba15666 25 void Engine::init() {
694617778 20:6ef06ba15666 26 direction = 0; //direction
694617778 20:6ef06ba15666 27 game = 0; // game number
694617778 20:6ef06ba15666 28 p = 1.0; // speed
694617778 20:6ef06ba15666 29 s = 1; // selection
694617778 1:b49c36604125 30 }
694617778 20:6ef06ba15666 31 // use Y and A make selection
694617778 8:b6ddc8e10da6 32 int Engine::get_select(Gamepad &pad) {
694617778 11:543c62bed764 33 if ( pad.check_event(Gamepad::Y_PRESSED) == true && s >0) {
694617778 11:543c62bed764 34 s--;
694617778 11:543c62bed764 35 }else if ( pad.check_event(Gamepad::A_PRESSED) == true && s < 2) {
694617778 11:543c62bed764 36 s++;
694617778 8:b6ddc8e10da6 37 }
694617778 8:b6ddc8e10da6 38 return s;
694617778 8:b6ddc8e10da6 39 }
694617778 20:6ef06ba15666 40 // display of menu and wait user to select game, setting and game
694617778 10:68076e3dcc33 41 void Engine::menu(Gamepad &pad, N5110 &lcd) {
694617778 11:543c62bed764 42 while(1){
694617778 10:68076e3dcc33 43 while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
694617778 10:68076e3dcc33 44 lcd.clear();
694617778 10:68076e3dcc33 45 s = get_select(pad);
694617778 10:68076e3dcc33 46 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 10:68076e3dcc33 47 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 11:543c62bed764 48 lcd.printString(" Press Y or A",2,1);
694617778 14:e1f7b4be1cf2 49 lcd.printString(" Guess",4,2);
694617778 14:e1f7b4be1cf2 50 lcd.printString(" Snake",4,3);
694617778 10:68076e3dcc33 51 lcd.printString(" Setting",4,4);
694617778 14:e1f7b4be1cf2 52 if ( s == 1 ) {
694617778 14:e1f7b4be1cf2 53 lcd.drawSprite(60,24,8,8,(int *)sprite);
694617778 10:68076e3dcc33 54 }else if ( s == 2) {
694617778 12:684f3ff512d8 55 lcd.drawSprite(60,32,8,8,(int *)sprite);
694617778 14:e1f7b4be1cf2 56 }else if( s == 0) {
694617778 14:e1f7b4be1cf2 57 lcd.drawSprite(60,16,8,8,(int *)sprite);
694617778 14:e1f7b4be1cf2 58 }
694617778 10:68076e3dcc33 59 lcd.refresh();
694617778 10:68076e3dcc33 60 }
694617778 14:e1f7b4be1cf2 61 if ( s == 1 ){
694617778 10:68076e3dcc33 62 select(pad,lcd);
694617778 14:e1f7b4be1cf2 63 game = 0;
694617778 11:543c62bed764 64 break;
694617778 10:68076e3dcc33 65 }else if ( s == 2){
694617778 11:543c62bed764 66 setting(pad,lcd);
694617778 14:e1f7b4be1cf2 67 }else if (s == 0){
694617778 14:e1f7b4be1cf2 68 game = 1;
694617778 14:e1f7b4be1cf2 69 break;
694617778 14:e1f7b4be1cf2 70 }
694617778 11:543c62bed764 71 }
694617778 10:68076e3dcc33 72 }
694617778 20:6ef06ba15666 73 // use to change the brightness and contrast of LED
694617778 11:543c62bed764 74 void Engine::setting(Gamepad &pad, N5110 &lcd) {
694617778 11:543c62bed764 75 while ( pad.check_event(Gamepad::BACK_PRESSED) == false ) {
694617778 11:543c62bed764 76 lcd.clear();
694617778 11:543c62bed764 77 int s = get_select(pad);
694617778 11:543c62bed764 78 float pot = pad.read_pot();
694617778 11:543c62bed764 79 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 11:543c62bed764 80 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 11:543c62bed764 81 lcd.printString(" Light",4,3);
694617778 11:543c62bed764 82 lcd.printString(" Contrast",4,4);
694617778 11:543c62bed764 83 if ( s == 1 || s == 0){
694617778 12:684f3ff512d8 84 lcd.drawSprite(60,23,8,8,(int *)sprite);
694617778 11:543c62bed764 85 lcd.drawRect(12,10,60,10,FILL_TRANSPARENT);
694617778 11:543c62bed764 86 lcd.drawRect(12,10,60*pot,10,FILL_BLACK);
694617778 11:543c62bed764 87 lcd.setBrightness(pot);
694617778 11:543c62bed764 88 }else if ( s == 2){
694617778 12:684f3ff512d8 89 lcd.drawSprite(60,32,8,8,(int *)sprite);
694617778 11:543c62bed764 90 lcd.drawRect(12,10,60,10,FILL_TRANSPARENT);
694617778 11:543c62bed764 91 lcd.drawRect(12,10,60*pot,10,FILL_BLACK);
694617778 11:543c62bed764 92 lcd.setContrast(pot);
694617778 11:543c62bed764 93 }
694617778 11:543c62bed764 94 lcd.refresh();
694617778 11:543c62bed764 95 }
694617778 11:543c62bed764 96 }
694617778 10:68076e3dcc33 97
694617778 20:6ef06ba15666 98 // simple splash screen of diffculty selection
694617778 8:b6ddc8e10da6 99 void Engine::select(Gamepad &pad, N5110 &lcd) {
694617778 8:b6ddc8e10da6 100 while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
694617778 8:b6ddc8e10da6 101 lcd.clear();
694617778 8:b6ddc8e10da6 102 int s = get_select(pad);
694617778 8:b6ddc8e10da6 103 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 8:b6ddc8e10da6 104 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 11:543c62bed764 105 lcd.printString(" Press Y or A",2,1);
694617778 12:684f3ff512d8 106 lcd.printString(" Easy",4,2);
694617778 12:684f3ff512d8 107 lcd.printString(" Normal",4,3);
694617778 12:684f3ff512d8 108 lcd.printString(" Hard",4,4);
694617778 8:b6ddc8e10da6 109 if ( s == 0) {
694617778 12:684f3ff512d8 110 lcd.drawSprite(63,15,8,8,(int *)sprite);
694617778 20:6ef06ba15666 111 p = 1.5; // change the speed
694617778 8:b6ddc8e10da6 112 }else if ( s == 1 ) {
694617778 12:684f3ff512d8 113 lcd.drawSprite(63,24,8,8,(int *)sprite);
694617778 8:b6ddc8e10da6 114 p = 1.0;
694617778 8:b6ddc8e10da6 115 }else if ( s == 2) {
694617778 12:684f3ff512d8 116 lcd.drawSprite(63,32,8,8,(int *)sprite);
694617778 8:b6ddc8e10da6 117 p = 0.5;
694617778 8:b6ddc8e10da6 118 }
694617778 8:b6ddc8e10da6 119 lcd.refresh();
694617778 8:b6ddc8e10da6 120 }
694617778 8:b6ddc8e10da6 121 }
694617778 9:18b059e5abb9 122
694617778 20:6ef06ba15666 123 // check the direction of the snake
694617778 1:b49c36604125 124 int Engine::get_direction(Gamepad &pad) {
694617778 1:b49c36604125 125 if (pad.check_event(Gamepad::A_PRESSED) == true && direction != 3){
694617778 1:b49c36604125 126 direction = 1;
694617778 1:b49c36604125 127 } else if (pad.check_event(Gamepad::B_PRESSED) == true && direction != 2){
694617778 1:b49c36604125 128 direction = 0;
694617778 1:b49c36604125 129 } else if (pad.check_event(Gamepad::X_PRESSED) == true && direction != 0){
694617778 1:b49c36604125 130 direction = 2;
694617778 1:b49c36604125 131 } else if (pad.check_event(Gamepad::Y_PRESSED) == true && direction != 1){
694617778 1:b49c36604125 132 direction = 3;
694617778 1:b49c36604125 133 }
694617778 20:6ef06ba15666 134 // use the joystick can also control the snake
694617778 9:18b059e5abb9 135 if (pad.get_direction() == S && direction != 3){
694617778 9:18b059e5abb9 136 direction = 1;
694617778 9:18b059e5abb9 137 } else if (pad.get_direction() == E && direction != 2){
694617778 9:18b059e5abb9 138 direction = 0;
694617778 9:18b059e5abb9 139 } else if (pad.get_direction() == W && direction != 0){
694617778 9:18b059e5abb9 140 direction = 2;
694617778 9:18b059e5abb9 141 } else if (pad.get_direction() == N && direction != 1){
694617778 9:18b059e5abb9 142 direction = 3;
694617778 9:18b059e5abb9 143 }
694617778 1:b49c36604125 144 return direction;
694617778 1:b49c36604125 145
694617778 3:1358cbb05ad3 146 }
694617778 3:1358cbb05ad3 147
694617778 3:1358cbb05ad3 148 // simple splash screen displayed on start-up
694617778 3:1358cbb05ad3 149 void Engine::welcome(Gamepad &pad, N5110 &lcd) {
694617778 5:82094591b4b4 150 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 5:82094591b4b4 151 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 4:323f42022d87 152 lcd.printString(" ###SNAKE### ",4,1);
694617778 4:323f42022d87 153 lcd.printString(" Qi Minghong ",4,2);
694617778 9:18b059e5abb9 154 lcd.printString(" 201089396 ",10,3);
694617778 5:82094591b4b4 155 lcd.printString(" Press Start ",4,4);
694617778 3:1358cbb05ad3 156 lcd.refresh();
694617778 3:1358cbb05ad3 157
694617778 3:1358cbb05ad3 158 // wait flashing LEDs until start button is pressed
694617778 8:b6ddc8e10da6 159 while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
694617778 3:1358cbb05ad3 160 pad.leds_on();
694617778 3:1358cbb05ad3 161 wait(0.1);
694617778 3:1358cbb05ad3 162 pad.leds_off();
694617778 3:1358cbb05ad3 163 wait(0.1);
694617778 3:1358cbb05ad3 164 }
694617778 10:68076e3dcc33 165 }
694617778 10:68076e3dcc33 166 // simple splash screen displayed on pasue
694617778 10:68076e3dcc33 167 void Engine::pause(Gamepad &pad, N5110 &lcd) {
694617778 10:68076e3dcc33 168 pad.tone(500.0,0.5);
694617778 10:68076e3dcc33 169 lcd.drawRect(22,13,40,14,FILL_TRANSPARENT);
694617778 10:68076e3dcc33 170 lcd.printString("PAUSE",28,2);
694617778 10:68076e3dcc33 171 lcd.refresh();
694617778 10:68076e3dcc33 172
694617778 10:68076e3dcc33 173 // wait flashing LEDs until start button is pressed
694617778 10:68076e3dcc33 174 while ( pad.check_event(Gamepad::BACK_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
694617778 10:68076e3dcc33 175 pad.leds_on();
694617778 10:68076e3dcc33 176 wait(0.1);
694617778 10:68076e3dcc33 177 pad.leds_off();
694617778 10:68076e3dcc33 178 wait(0.1);
694617778 10:68076e3dcc33 179 }
694617778 3:1358cbb05ad3 180 }
694617778 3:1358cbb05ad3 181
694617778 3:1358cbb05ad3 182 // simple splash screen of game over
694617778 11:543c62bed764 183 void Engine::gameover(Gamepad &pad, N5110 &lcd, int score,int hscore) {
694617778 4:323f42022d87 184 pad.tone(500.0,0.5);
694617778 12:684f3ff512d8 185 lcd.clear();
694617778 3:1358cbb05ad3 186 pad.init();
694617778 3:1358cbb05ad3 187 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 3:1358cbb05ad3 188 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 4:323f42022d87 189 lcd.printString(" GAME OVER ",4,1);
694617778 4:323f42022d87 190 // we need an array of chars to store the message
694617778 4:323f42022d87 191 char buffer[14]; // max screen witdth is 14
694617778 4:323f42022d87 192 // print message to buffer
694617778 12:684f3ff512d8 193 sprintf(buffer," Score : %d ",score);
694617778 11:543c62bed764 194 lcd.printString(buffer,2,2);
694617778 20:6ef06ba15666 195 char hbuffer[14];
694617778 18:2ca7bd135b68 196 sprintf(hbuffer,"Record : %d",hscore);
694617778 11:543c62bed764 197 lcd.printString(hbuffer,8,3);
694617778 12:684f3ff512d8 198 lcd.printString(" press back",8,4);
694617778 3:1358cbb05ad3 199 lcd.refresh();
694617778 3:1358cbb05ad3 200
694617778 3:1358cbb05ad3 201 // wait flashing LEDs until start button is pressed
694617778 12:684f3ff512d8 202 while ( pad.check_event(Gamepad::BACK_PRESSED) == false && pad.check_event(Gamepad::JOY_PRESSED) == false) {
694617778 3:1358cbb05ad3 203 pad.leds_on();
694617778 3:1358cbb05ad3 204 wait(0.1);
694617778 3:1358cbb05ad3 205 pad.leds_off();
694617778 3:1358cbb05ad3 206 wait(0.1);
694617778 3:1358cbb05ad3 207 }
694617778 3:1358cbb05ad3 208
694617778 1:b49c36604125 209 }