/** * \fn guiPlay(BoardSize size) * \brief This function is the main game loop * \details Function that create a new game, perform the game loop and display the game with SDL * \param size Variable representing the size of the Board (Small, Medium or Large) */ int guiPlay(BoardSize size) { /*******************/ /** VARIABLES **/ /*******************/ /***** SDL Variables *****/ SDL_Event event; //Variable to capture mouse/keyboard events SDL_Surface* screen;//Buffer that will contain the pixels to render on the screen /***** General Variables *****/ Assets assets; //Variable that will contain all the assets used in the game Timer timer; //Variable used to control the actions based on the time /***** Structure Variables *****/ bool continueGameMove1, continueGameMove2; //Variable used to check if the snake x is dead bool snake1InDebuff, snake2InDebuff; Game game; //Variable to access the game Board board; //Variable to access the board Snake snake1; //Variable to access the first snake Snake snake2; //Variable to access the first snake /************************/ /** INITIALIZATION **/ /************************/ /***** SDL Initialization *****/ SDL_Init(SDL_INIT_VIDEO); //Initialization of the SDL Library TTF_Init(); SDL_WM_SetCaption("Larasnake", NULL); //Set the title and icon name of the displayed window screen = guiCreateScreen(size); /***** General Variables *****/ srand(time(NULL)); //Initialization of the random function timer = guiCreateTimer(); assets = guiLoadAssets(size); /***** Structure Variables *****/ continueGameMove1 = true; continueGameMove2 = true; snake1InDebuff = false; snake2InDebuff = false; game = gameCreate(size); board = gameGetBoard(game); guiSetFieldType(game, assets, size); snake1 = gameGetSnake(game, 1); snake2 = gameGetSnake(game, 2); //music of the game Mix_Music *musiqueGame = Mix_LoadMUS("./sound/musiqueGame.mp3"); //music of the game Mix_PlayMusic(musiqueGame, -1); //loop --PLAY HERE Mix_VolumeMusic(MIX_MAX_VOLUME / 6); //half of the maximum sound /************************/ /** GAME LOOP **/ /************************/ gameFeed(game, true); //first ham appeared while (gameGetIsPlaying(game)) { timer->start = SDL_GetTicks(); // Start of the current frame guiEvent(&event, snake1, game); // catch player event and set the direction of snake1 if(!gameGetIsPaused(game)) { if(gameGetPauseTimer(game)==0) //know if we don't leave the pause { if(boardGetType(board)) //if we have to change the background { boardSetType(board, false); //disable change flag guiChangeBackground(screen, assets,size); guiSetFieldType(game, assets, size); printf("MAP CHANGED\n"); //the map is now changed } ////// Move of snake 1 (player) ////// timer->snake1MoveTimer += SDL_GetTicks() - timer->snake1LastMove; if (timer->snake1MoveTimer >= snakeGetSpeed(snake1)) { // test if we wait enough time to move the snake 1 continueGameMove1 = gameMoveSnake(game, snake1); // move th snake1. if snake1 is dead continueGameMove1=false timer->snake1MoveTimer = 0; // set the move timer to 0 when the snake move } timer->snake1LastMove = SDL_GetTicks(); ///////////////////////////////////// ////// Move of snake 2 (AI) ////// timer->snake2MoveTimer += SDL_GetTicks() - timer->snake2LastMove; if (timer->snake2MoveTimer >= snakeGetSpeed(snake2)) { // test if we wait enough time to move the snake 2 snakeSetDirection(snake2, iaSurviveDepth(board, snake2, snake1)); // let ia choose the best direction of snake2 continueGameMove2 = gameMoveSnake(game, snake2); // move the snake2. if snake2 is dead continueGameMove2=false timer->snake2MoveTimer = 0; // set the move timer to 0 when the snake move } timer->snake2LastMove = SDL_GetTicks(); ///////////////////////////////// ///////////////// Debuff snake1 ///////////////// if (!itemListIsEmpty(snakeGetItemList(snake1))) { if (!snake1InDebuff) { snake1InDebuff = true; timer->snake1LastDebuff = SDL_GetTicks(); } else { timer->snake1DebuffTimer += SDL_GetTicks() - timer->snake1LastDebuff; if(timer->snake1DebuffTimer >= ITEM_DEBUFF_INTERVAL) { gameItemDebuff(snakeGetItemList(snake1)->next, snake1); snake1InDebuff = false; timer->snake1DebuffTimer = 0; } timer->snake1LastDebuff = SDL_GetTicks(); } } //////////////////////////////////////////////// ///////////////// Debuff snake2 ///////////////// if (!itemListIsEmpty(snakeGetItemList(snake2))) { if (!snake2InDebuff) { snake2InDebuff = true; timer->snake2LastDebuff = SDL_GetTicks(); } else { timer->snake2DebuffTimer += SDL_GetTicks() - timer->snake2LastDebuff; if(timer->snake2DebuffTimer >= ITEM_DEBUFF_INTERVAL) { gameItemDebuff(snakeGetItemList(snake2)->next, snake2); snake2InDebuff = false; timer->snake2DebuffTimer = 0; } timer->snake2LastDebuff = SDL_GetTicks(); } } //////////////////////////////////////////////// ///////// Item pop ///////// timer->itemPopTimer += SDL_GetTicks() - timer->itemLastPop; if(timer->itemPopTimer >= ITEM_POP_INTERVAL) { gameFeed(game, false); //Function called to put some food on the board timer->itemPopTimer = 0; } timer->itemLastPop = SDL_GetTicks(); ///////// Item pop ///////// //>>>>>>> 43afef4547198632093d7b891941aa8c99643d24 } } /////// Draw /////// guiDrawGame(screen, game, assets, size); // draw the board on screen with surfaces stored in the Assets struct guiReloadScreen(screen); // reload all the screen //boardDisplay(board); /////////////////// if(!continueGameMove1 || !continueGameMove2) // if one snake die the game is over gameEnd(game); ////// Framerate management ////// timer->end = SDL_GetTicks(); // Get the time after the calculations timer->delay = FRAME_MS - (timer->end - timer->start); // Calculate how long to delay should be if(timer->delay > 0) { SDL_Delay(timer->delay); // Delay processing } } int idWinner; if(continueGameMove1) { if(snakeGetId(snake1) == 1) idWinner = 1; else idWinner = 2; } else { if(snakeGetId(snake1) == 1) idWinner = 2; else idWinner = 1; } ////// Free ////// gameFree(game); guiFreeAssets(assets); Mix_FreeMusic(musiqueGame); //free the music //TTF_Quit(); //SDL_Quit(); return idWinner; }
virtual void ProcessEvent(Rocket::Core::Event& event) { GUIEvent guiEvent( event ); m_pEvent->OnEvent( guiEvent ); }