// Implementation of the keyboard event callback function prototype void GameScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) { log("Key with keycode %d pressed", keyCode); if(keyCode==EventKeyboard::KeyCode::KEY_LEFT_ARROW) snakeMove(DIR_DEF::LEFT); else if(keyCode==EventKeyboard::KeyCode::KEY_UP_ARROW) snakeMove(DIR_DEF::UP); else if(keyCode==EventKeyboard::KeyCode::KEY_RIGHT_ARROW) snakeMove(DIR_DEF::RIGHT); else if(keyCode==EventKeyboard::KeyCode::KEY_DOWN_ARROW) snakeMove(DIR_DEF::DOWN); }
void GameScene::onTouchEnded( Touch* touch, Event* event ) { Point touchPoint = touch->getLocation(); int x = (int)touchPoint.x; int y = (int)touchPoint.y; int x1=touch_x; int y1=touch_y; int dir; if (abs(y - y1) > abs(x - x1)) { if (y > y1) dir = DIR_DEF::UP; else dir = DIR_DEF::DOWN; } else { if (x > x1) dir = DIR_DEF::RIGHT; else dir = DIR_DEF::LEFT; } snakeMove(dir); }
void *mainGameLoop() { setFood(); do { situation = judge(direction);//judge if knock the wall or facing a food. debugPrint(1); if(situation == 1)//If not hit the wall & not have a food, print out the screen. { snakeMove(direction);//Draw the screen array. printScreen(screen,speed);//Print out the screen for 2 seconds. //debugPrint(2); } if(situation == 2)//If facing a food, longer the snake by change food into the head of the snake. { snakeEat(direction); setFood(); printScreen(screen,speed); } else ; } while (situation != -1); return ((void *)0); }