summaryrefslogtreecommitdiffhomepage
path: root/games/snake.c
diff options
context:
space:
mode:
authorRay <[email protected]>2019-05-21 17:47:06 +0200
committerRay <[email protected]>2019-05-21 17:47:06 +0200
commit8b4f58b9e9d3deef9e1f1dfebece16a9aca1e62f (patch)
tree049e864ba695cc1a76fb29bf1c2785ce762e00a9 /games/snake.c
parent0027868d1f4ba76043e1cdfbb5c172696ad0985b (diff)
downloadraylib-8b4f58b9e9d3deef9e1f1dfebece16a9aca1e62f.tar.gz
raylib-8b4f58b9e9d3deef9e1f1dfebece16a9aca1e62f.zip
Review sample games
Diffstat (limited to 'games/snake.c')
-rw-r--r--games/snake.c36
1 files changed, 17 insertions, 19 deletions
diff --git a/games/snake.c b/games/snake.c
index 23d1bd1d..47522754 100644
--- a/games/snake.c
+++ b/games/snake.c
@@ -43,19 +43,19 @@ typedef struct Food {
//------------------------------------------------------------------------------------
// Global Variables Declaration
//------------------------------------------------------------------------------------
-static int screenWidth = 800;
-static int screenHeight = 450;
+static const int screenWidth = 800;
+static const int screenHeight = 450;
-static int framesCounter;
-static bool gameOver;
-static bool pause;
+static int framesCounter = 0;
+static bool gameOver = false;
+static bool pause = false;
-static Food fruit;
-static Snake snake[SNAKE_LENGTH];
-static Vector2 snakePosition[SNAKE_LENGTH];
-static bool allowMove;
-static Vector2 offset;
-static int counterTail;
+static Food fruit = { 0 };
+static Snake snake[SNAKE_LENGTH] = { 0 };
+static Vector2 snakePosition[SNAKE_LENGTH] = { 0 };
+static bool allowMove = false;
+static Vector2 offset = { 0 };
+static int counterTail = 0;
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
@@ -80,7 +80,6 @@ int main(void)
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
-
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@@ -93,7 +92,6 @@ int main(void)
//----------------------------------------------------------------------------------
}
#endif
-
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadGame(); // Unload loaded data (textures, sounds, models...)
@@ -150,7 +148,7 @@ void UpdateGame(void)
if (!pause)
{
- // control
+ // Player control
if (IsKeyPressed(KEY_RIGHT) && (snake[0].speed.x == 0) && allowMove)
{
snake[0].speed = (Vector2){ SQUARE_SIZE, 0 };
@@ -172,7 +170,7 @@ void UpdateGame(void)
allowMove = false;
}
- // movement
+ // Snake movement
for (int i = 0; i < counterTail; i++) snakePosition[i] = snake[i].position;
if ((framesCounter%5) == 0)
@@ -189,7 +187,7 @@ void UpdateGame(void)
}
}
- // wall behaviour
+ // Wall behaviour
if (((snake[0].position.x) > (screenWidth - offset.x)) ||
((snake[0].position.y) > (screenHeight - offset.y)) ||
(snake[0].position.x < 0) || (snake[0].position.y < 0))
@@ -197,13 +195,13 @@ void UpdateGame(void)
gameOver = true;
}
- // collision with yourself
+ // Collision with yourself
for (int i = 1; i < counterTail; i++)
{
if ((snake[0].position.x == snake[i].position.x) && (snake[0].position.y == snake[i].position.y)) gameOver = true;
}
- // fruit.position calculation
+ // Fruit position calculation
if (!fruit.active)
{
fruit.active = true;
@@ -219,7 +217,7 @@ void UpdateGame(void)
}
}
- // collision
+ // Collision
if ((snake[0].position.x < (fruit.position.x + fruit.size.x) && (snake[0].position.x + snake[0].size.x) > fruit.position.x) &&
(snake[0].position.y < (fruit.position.y + fruit.size.y) && (snake[0].position.y + snake[0].size.y) > fruit.position.y))
{