Commit 214cc162 authored by Benjamin LEROUX's avatar Benjamin LEROUX

gestion fruit different complet

parent e896fafd
...@@ -36,6 +36,7 @@ typedef struct Snake { ...@@ -36,6 +36,7 @@ typedef struct Snake {
Vector2 position; Vector2 position;
Vector2 size; Vector2 size;
Vector2 speed; Vector2 speed;
int score;
Color color; Color color;
} Snake; } Snake;
...@@ -63,6 +64,9 @@ static bool allowMove = false; ...@@ -63,6 +64,9 @@ static bool allowMove = false;
static Vector2 offset = { 0 }; static Vector2 offset = { 0 };
static int counterTail = 0; static int counterTail = 0;
static Food goldenFruit = {0};
static Food deadFruit = {0};
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Module Functions Declaration (local) // Module Functions Declaration (local)
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
...@@ -116,6 +120,7 @@ int main(void) ...@@ -116,6 +120,7 @@ int main(void)
void InitGame(void) void InitGame(void)
{ {
framesCounter = 0; framesCounter = 0;
snake[0].score=0;
gameOver = false; gameOver = false;
pause = false; pause = false;
...@@ -143,6 +148,14 @@ void InitGame(void) ...@@ -143,6 +148,14 @@ void InitGame(void)
fruit.size = (Vector2){ SQUARE_SIZE, SQUARE_SIZE }; fruit.size = (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
fruit.color = SKYBLUE; fruit.color = SKYBLUE;
fruit.active = false; fruit.active = false;
goldenFruit.size= (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
goldenFruit.color=YELLOW;
goldenFruit.active=false;
deadFruit.size= (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
deadFruit.color=BLACK;
deadFruit.active=false;
} }
// Update game (one frame) // Update game (one frame)
...@@ -222,9 +235,12 @@ void UpdateGame(void) ...@@ -222,9 +235,12 @@ void UpdateGame(void)
if ((snake[0].position.x == snake[i].position.x) && (snake[0].position.y == snake[i].position.y)) gameOver = true; 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) if (!fruit.active)
{ {
goldenFruit.position=(Vector2) {-100,-100}; //disparition du goldenFruit
deadFruit.position=(Vector2) {-100,-100}; //disparition du deadFruit
fruit.active = true; fruit.active = true;
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 };
...@@ -236,15 +252,58 @@ void UpdateGame(void) ...@@ -236,15 +252,58 @@ void UpdateGame(void)
i = 0; i = 0;
} }
} }
//autres fruits
if(counterTail%4==0){
goldenFruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.x/2, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.y/2 };
for (int i = 0; i < counterTail; i++)
{
while (((goldenFruit.position.x == snake[i].position.x) && (goldenFruit.position.y == snake[i].position.y))||((goldenFruit.position.x == fruit.position.x) && (goldenFruit.position.y == fruit.position.y)))
{
goldenFruit.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;
}
}
}else if(counterTail%7==0){
deadFruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.x/2, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.y/2 };
for (int i = 0; i < counterTail; i++)
{
while (((deadFruit.position.x == snake[i].position.x) && (deadFruit.position.y == snake[i].position.y))||((deadFruit.position.x == fruit.position.x) && (goldenFruit.position.y == fruit.position.y)))
{
deadFruit.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;
}
}
}
} }
// Collision // Collision fruit
if ((snake[0].position.x < (fruit.position.x + fruit.size.x) && (snake[0].position.x + snake[0].size.x) > fruit.position.x) && 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)) (snake[0].position.y < (fruit.position.y + fruit.size.y) && (snake[0].position.y + snake[0].size.y) > fruit.position.y))
{ {
snake[counterTail].position = snakePosition[counterTail - 1]; snake[counterTail].position = snakePosition[counterTail - 1];
snake[counterTail].color = fruit.color;
counterTail += 1; counterTail += 1;
fruit.active = false; fruit.active = false;
snake[0].score+=100;
}
// Collision goldenFruit
if ((snake[0].position.x < (goldenFruit.position.x + goldenFruit.size.x) && (snake[0].position.x + snake[0].size.x) > goldenFruit.position.x) &&
(snake[0].position.y < (goldenFruit.position.y + goldenFruit.size.y) && (snake[0].position.y + snake[0].size.y) > goldenFruit.position.y))
{
snake[counterTail].position = snakePosition[counterTail - 1];
snake[counterTail].color = goldenFruit.color;
counterTail += 1;
goldenFruit.position=(Vector2) {-100,-100}; //disparition du goldenFruit
snake[0].score+=250;
}
// Collision deadFruit
if ((snake[0].position.x < (deadFruit.position.x + deadFruit.size.x) && (snake[0].position.x + snake[0].size.x) > deadFruit.position.x) &&
(snake[0].position.y < (deadFruit.position.y + deadFruit.size.y) && (snake[0].position.y + snake[0].size.y) > deadFruit.position.y))
{
gameOver=true;
} }
framesCounter++; framesCounter++;
...@@ -285,11 +344,22 @@ void DrawGame(void) ...@@ -285,11 +344,22 @@ void DrawGame(void)
// Draw fruit to pick // Draw fruit to pick
DrawRectangleV(fruit.position, fruit.size, fruit.color); DrawRectangleV(fruit.position, fruit.size, fruit.color);
if(counterTail%4==0) DrawRectangleV(goldenFruit.position, goldenFruit.size, goldenFruit.color);
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY); if(counterTail%7==0) DrawRectangleV(deadFruit.position, deadFruit.size, deadFruit.color);
// DrawText(TextFormat("TIME: %.02f", (float)framesCounter/60), 10, 10, 20, BLACK);
//DrawText(TextFormat("SCORE: %04i",snake[0].score), 10, 30, 20, BLACK);
if (pause) {
DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 80)/2, screenHeight/2 - 160, 80, BLACK);
DrawText(TextFormat("TIME: %.02f", (float)framesCounter/60), screenWidth/2 - MeasureText("TIME: 00.00", 60)/2, screenHeight - 140, 60, BLUE);
DrawText(TextFormat("SCORE: %04i",snake[0].score ), screenWidth/2 - MeasureText("SCORE: 0000", 60)/2, screenHeight - 200, 60, BLUE);
}
}
else {
DrawText("PRESS [ENTER] TO PLAY AGAIN", screenWidth/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 40)/2, screenHeight - 200, 40, BLACK);
DrawText(TextFormat("TIME: %.02f", (float)framesCounter/60), screenWidth/2 - MeasureText("TIME: 00.00", 60)/2, screenHeight/2 - 120, 60, BLUE);
DrawText(TextFormat("SCORE: %04i",snake[0].score ), screenWidth/2 - MeasureText("SCORE: 0000", 60)/2, screenHeight/2 - 180, 60, BLUE);
} }
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY);
EndDrawing(); EndDrawing();
} }
......
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