Commit b3a5282b authored by Fan FEI's avatar Fan FEI

Version "2.0"

  add soundtrack
parent 67c5b302
...@@ -13,14 +13,19 @@ ...@@ -13,14 +13,19 @@
* Expanded on this basis by Fan FEI * Expanded on this basis by Fan FEI
* *
* *
*
*
*
*
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
...@@ -31,6 +36,8 @@ ...@@ -31,6 +36,8 @@
#define SNAKE_LENGTH 256 #define SNAKE_LENGTH 256
#define SQUARE_SIZE 31 #define SQUARE_SIZE 31
Music soundtrack;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Types and Structures Definition // Types and Structures Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
...@@ -83,6 +90,11 @@ int main(void) ...@@ -83,6 +90,11 @@ int main(void)
//--------------------------------------------------------- //---------------------------------------------------------
InitWindow(screenWidth, screenHeight, "sample game: snake By FanFEI"); // Initialize the interface and title InitWindow(screenWidth, screenHeight, "sample game: snake By FanFEI"); // Initialize the interface and title
InitAudioDevice();
char *music = "Music_snake_FanFEI.mp3";
soundtrack = LoadMusicStream(music);
PlayMusicStream(soundtrack);
InitGame(); //Initialize the game InitGame(); //Initialize the game
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
...@@ -97,6 +109,7 @@ int main(void) ...@@ -97,6 +109,7 @@ int main(void)
// Update and Draw // Update and Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateDrawFrame(); UpdateDrawFrame();
UpdateMusicStream(soundtrack);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
#endif #endif
...@@ -117,6 +130,7 @@ int main(void) ...@@ -117,6 +130,7 @@ int main(void)
// Initialize game variables // Initialize game variables
void InitGame(void) void InitGame(void)
{ {
framesCounter = 0; framesCounter = 0;
gameOver = false; // Game state gameOver = false; // Game state
pause = false; pause = false;
...@@ -183,7 +197,7 @@ void UpdateGame(void) ...@@ -183,7 +197,7 @@ void UpdateGame(void)
// Snake movement // Snake movement
for (int i = 0; i < counterTail; i++) snakePosition[i] = snake[i].position; for (int i = 0; i < counterTail; i++) snakePosition[i] = snake[i].position;
if ((framesCounter%10) == 0) if ((framesCounter%5) == 0)
{ {
for (int i = 0; i < counterTail; i++) for (int i = 0; i < counterTail; i++)
{ {
...@@ -222,7 +236,6 @@ void UpdateGame(void) ...@@ -222,7 +236,6 @@ void UpdateGame(void)
while ((fruit.position.x == snake[i].position.x) && (fruit.position.y == snake[i].position.y)) while ((fruit.position.x == snake[i].position.x) && (fruit.position.y == snake[i].position.y))
{ {
fruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.x/2, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.y/2 }; fruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.x/2, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.y/2 };
i = 0;
} }
} }
} }
...@@ -241,10 +254,12 @@ void UpdateGame(void) ...@@ -241,10 +254,12 @@ void UpdateGame(void)
} }
else else
{ {
StopMusicStream(soundtrack);
if (IsKeyPressed(KEY_ENTER)) //Press enter to start the game if (IsKeyPressed(KEY_ENTER)) //Press enter to start the game
{ {
InitGame(); InitGame();
gameOver = false; gameOver = false;
PlayMusicStream(soundtrack);
} }
} }
} }
...@@ -285,6 +300,7 @@ void DrawGame(void) ...@@ -285,6 +300,7 @@ void DrawGame(void)
// Unload game variables // Unload game variables
void UnloadGame(void) void UnloadGame(void)
{ {
UnloadMusicStream(soundtrack);
// TODO: Unload all dynamic loaded data (textures, sounds, models...) // TODO: Unload all dynamic loaded data (textures, sounds, models...)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment