本文整理汇总了C++中Snake类的典型用法代码示例。如果您正苦于以下问题:C++ Snake类的具体用法?C++ Snake怎么用?C++ Snake使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Snake类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addFood
void ItemFactory::addFood()
{
//check if the food still exist
if (m_pFood)
return;
//check the empty grid
Snake* snake = dynamic_cast<Snake*>(getParent()->getChildByTag(eID_Snake));
if (!snake)
return;
int left = m_pSnakeMap->getMovableNumbers() - snake->getLength();
//no empty grids left, you win
if (left <= 0)
return;
//set the food index
int index = (int)(rand() % left + 1);
auto mapIndex = m_pSnakeMap->getEmptyGridIndex(index);
//create food model
m_pFood = Food::create();
this->addChild(m_pFood, 1, eID_Food);
m_pFood->setPosition(VisibleRect::getVisibleRect().origin + VisibleRect::getHalfGridVec() + VisibleRect::getGridLength()*mapIndex);
m_pFood->setIndex(mapIndex);
//set the grid type
m_pSnakeMap->setGridType(mapIndex, eType_Food);
}
示例2: main
int main()
{
sf::RenderWindow Window;
Window.create(sf::VideoMode(900, 900), "Snake", sf::Style::Default);
Window.setKeyRepeatEnabled(false);
Snake *snake = new Snake();
while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent(Event))
{
switch (Event.type)
{
case(sf::Event::Closed):
Window.close();
break;
}
}
Window.clear();
snake->Draw(Window);
Window.display();
}
system("pause");
return 0;
}
示例3: ofDrawBitmapString
//--------------------------------------------------------------
void ofApp::draw(){
ofDrawBitmapString(state, 100, 100);
ofDrawBitmapString(snake.countEatFood, 800, 100);
if (state == S_BEGIN) {
ofDrawBitmapString(ofToString(state) + " Start game", 300, 300);
}
if (state == S_GAME) {
ofDrawBitmapString(ofToString(state) + " Game in state game", 300, 300);
showSnake();
showFood();
}
if (state == S_WIN) {
ofDrawBitmapString(ofToString(state) + " Game win!", 300, 300);
snake.rePosition();
}
if (state == S_LOOSE) {
ofDrawBitmapString(ofToString(state) + " Game loose!", 300, 300);
snake.rePosition();
}
}
示例4: if
int NCurseGui::printGame(const Snake& snake, const Apple & apple)
{
std::deque<std::pair<int, int> > s = snake.getSnake();
std::deque<std::pair<int, int> >::const_iterator it = s.begin();
int apple_color;
if (apple.getAge() <= apple.getBonusAge())
apple_color = 4;
else if (apple.getAge() > apple.getRottenAge())
apple_color = 5;
else
apple_color = 1;
wborder(_win, '|', '|', '_', '_', ' ', ' ', '|', '|');
wattron(_win, COLOR_PAIR(3));
mvwprintw(_win, it->first + 1, it->second * 2 + 1, " ");
wattroff(_win, COLOR_PAIR(3));
while (++it != s.end()) {
wattron(_win, COLOR_PAIR(2));
mvwprintw(_win, it->first + 1, it->second * 2 + 1, " ");
wattroff(_win, COLOR_PAIR(2));
}
wattron(_win, COLOR_PAIR(apple_color));
mvwprintw(_win, apple.getApple().first + 1, apple.getApple().second * 2 + 1, " ");
wattroff(_win, COLOR_PAIR(apple_color));
std::pair<size_t, size_t> lastLink = snake.getLastChain();
mvwprintw(_win, lastLink.first + 1, lastLink.second * 2 + 1, lastLink.first + 1 == _height ? "__" : " ");
wrefresh(_win);
return 0;
}
示例5: main
int main(int argc,char *argv)
{
system("color 4A");
Snake sna;
Apple fd;
fd=product_food(fd);;
//сно╥я╜╩╥
while(1)
{
if(sna.is_dead())
{
system("cls");
GameOver();
break;
}
if(sna.is_eat(fd))
{
fd=product_food(fd);
}
if(sna.is_win())
{
break;
}
sna.Move();
}
return 0;
}
示例6: switch
int Food::hasBeenEatenBy(Snake& aSnake) {
switch(type) {
case addPoint:
return 1;
break;
case minusThreePoints:
return -3;
break;
case increaseSpeed:
if (aSnake.speed > 1) {
aSnake.speed--;
}
break;
case decreaseSpeed:
aSnake.speed++;
break;
case increaseLength:
aSnake.growSnake();
break;
case decreaseLength:
aSnake.shrinkSnake();
break;
}
return 1;
}
示例7: Input
/* Controla la entrada de teclado del usuario
****************************************************************************************/
bool Input(Snake &serpiente){
bool result = false;
SDL_Event event;
if( SDL_PollEvent( &event ) ){ //Recogemos el evento
if( event.type == SDL_KEYDOWN ){ //Se ha pulsado una tecla
switch( event.key.keysym.sym ){
case SDLK_ESCAPE:
result = true; //Escape: Salimos del programa
break;
case SDLK_UP:
serpiente.SetVelocidad(-1, 0);
break;
case SDLK_DOWN:
serpiente.SetVelocidad(1, 0);
break;
case SDLK_LEFT:
serpiente.SetVelocidad(0, -1);
break;
case SDLK_RIGHT:
serpiente.SetVelocidad(0, 1);
break;
}
}else if( event.type == SDL_QUIT ){
//Se quita el programa
result = true;
}
}
return result;
}
示例8: Return
/*
Method is responsible for runtime checks:
- Collision detection
- Food eaten
- Drawing
Return(true) -> GameOver
Return(false)-> Keep running
*/
bool GameController::performGameRuntimeChecks(CInputManager &inputManager,
unsigned long &score, CollisionDetector &collisionDetector,
DrawEngine drawEngine, int key, Snake &snake, vector<Cell> &foodList,
FoodProvider &foodProvider, Cell lastCell)
{
//If collision detected
if(collisionDetector.collisionDetected(snake))
{
char message[] = "GAME OVER! <Press any key>";
drawEngine.showMessage(message);
inputManager.GetNextKeyWait(&key);
return true;
}
//Food eaten
else if(collisionDetector.foodEaten(snake, foodList))
{
//Setting new score
score += snake.getCells().size()*2;
//Draw score
drawEngine.updateScore(score);
//Add cell
snake.addCell();
//Recieving new foodList
vector<Cell> &list = foodProvider.provideFood(snake, drawEngine.getWindownWidth(),
drawEngine.getMenuBorderHeight());
foodList = list;
}
//Draw snake
drawEngine.drawSnake(snake, lastCell);
//Draw food
drawEngine.drawFood(foodList);
return false;
}
示例9: Snake
void Game::loop(void)
{
Map *map;
Snake *snake;
map = this->getMap();
snake = new Snake(map->getWidth() / 2, map->getHeight() / 2);
map->setSnake(snake);
while (!this->_shouldExit)
{
Time::update();
/* events */
this->_handleInputs(this->_dlib->getInput());
/* update */
if (!this->_isPaused)
this->update();
/* refresh display */
this->_dlib->draw(map);
Time::sleep(200 - snake->getSpeed() * 10);
}
this->_dlib->close();
printf("Final score: %d\n", this->_score);
}
示例10: file
Snake* Snake::Load(const char* file_name) {
Snake* snake = NULL;
ifstream file(file_name);
if (!file) return NULL;
int dir, x, y;
// Create a new snake
snake = new Snake;
snake->head = snake->tail = NULL;
// Load moving direction
file >> dir;
snake->dir = (Direction) dir;
// Load the body
while(file >> x >> y) {
Node* node = new Node;
node->x = x;
node->y = y;
node->prevNode = NULL;
mvaddch(node->y, node->x, '*');
if (snake->head == NULL) {
snake->head = node;
snake->tail = node;
} else {
snake->InsertAtHead(node);
}
}
return snake;
}
示例11: main
int main(int argc, const char *argv[])
{
Snake snakeGame;
snakeGame.start();
return 0;
}
示例12: AddBody
void GameLayer::AddBody(){
head->setPosition(food->getPosition());
Snake* node = Snake::create();
node->setNode(Sprite::create("Snake.png"));
node->setPosition(lastbodyposi);
body.pushBack(node);
this->addChild(node);
}
示例13: main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Snake w;
w.show();
return a.exec();
}
示例14: getMap
void Game::_handleCollisions(void)
{
Snake *snake;
Map *map;
map = getMap();
snake = map->getSnake();
// with the edge
if (snake->_head->getX() < 0 || snake->_head->getX() > map->getWidth() - 1
|| snake->_head->getY() < 0 || snake->_head->getY() > map->getHeight() - 1) {
printf("GAME OVER! (bang the wall)\n");
this->_shouldExit = TRUE;
return ;
}
// with himself
for (std::list<GameEntity *>::iterator it = snake->_body.begin(); it != snake->_body.end(); it++) {
if ((*it)->getX() == snake->_head->getX() && (*it)->getY() == snake->_head->getY()) {
printf("GAME OVER! (the head in the ass)\n");
this->_shouldExit = TRUE;
return ;
}
}
// with an obstacle
std::list<GameEntity *> obst = map->getObstacles();
for (std::list<GameEntity *>::iterator it = obst.begin(); it != obst.end(); it++) {
if ((*it)->getX() == snake->_head->getX() && (*it)->getY() == snake->_head->getY()) {
printf("GAME OVER! (hit an obstacle)\n");
this->_shouldExit = TRUE;
return ;
}
}
// with an apple
std::list<GameEntity *> apple = map->getApple();
for (std::list<GameEntity *>::iterator it = apple.begin(); it != apple.end(); it++) {
if ((*it)->getX() == snake->_head->getX() && (*it)->getY() == snake->_head->getY()) {
snake->setState("grow");
snake->upSpeed(1);
(*it)->setPosition(-1, -1);
this->_score++;
map->setScore(this->_score);
}
}
// with bonus
GameEntity * bonus = map->getBonus();
if (bonus->getX() == snake->_head->getX() && bonus->getY() == snake->_head->getY()) {
snake->setState("grow");
bonus->setPosition(-1, -1);
this->_score += 10;
map->setScore(this->_score);
}
}
示例15: moveSnake
void UpdateController::moveSnake(Snake &snake) {
switch (input) {
case KEY_LEFT:
snake.movex(-1);
break;
case KEY_RIGHT:
snake.movex(1);
break;
case KEY_UP:
snake.movey(-1);
break;
case KEY_DOWN:
snake.movey(1);
break;
default:
snake.movex(1);
}
for ( auto iter = coins.begin(), endPtr = coins.end() ; iter != endPtr ; ++iter ) {
if (snake.headLiesOn(iter->getxy())) {
snake.grow(iter->getvalue());
coinMutex.lock();
*iter = Coin(getFreeCoord());
coinMutex.unlock();
break;
}
}
auto head = snake.getHead();
if (snake.bitItself() || !map.contains(head) || snakeWallCollision()) {
stop();
}
}