Commit 15e6d0a8 authored by Benjamin LEROUX's avatar Benjamin LEROUX

ajout de murs + gestion collision

parent 214cc162
......@@ -22,12 +22,9 @@
//----------------------------------------------------------------------------------
#define SNAKE_LENGTH 256
#define SQUARE_SIZE 31
#define NUMBEROFWALL 8
//----------------------------------------------------------------------------------
// More Defines
//----------------------------------------------------------------------------------
#define BordureX 12.5
#define BordureY 8
//----------------------------------------------------------------------------------
// Types and Structures Definition
......@@ -67,6 +64,8 @@ static int counterTail = 0;
static Food goldenFruit = {0};
static Food deadFruit = {0};
static Rectangle recTab[NUMBEROFWALL];
//------------------------------------------------------------------------------------
// Module Functions Declaration (local)
//------------------------------------------------------------------------------------
......@@ -136,7 +135,7 @@ void InitGame(void)
snake[i].size = (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
snake[i].speed = (Vector2){ SQUARE_SIZE, 0 };
if (i == 0) snake[i].color = DARKBLUE;
if (i == 0) snake[i].color = DARKGREEN;
else snake[i].color = BLUE;
}
......@@ -146,7 +145,7 @@ void InitGame(void)
}
fruit.size = (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
fruit.color = SKYBLUE;
fruit.color = GREEN;
fruit.active = false;
goldenFruit.size= (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
......@@ -156,6 +155,73 @@ void InitGame(void)
deadFruit.size= (Vector2){ SQUARE_SIZE, SQUARE_SIZE };
deadFruit.color=BLACK;
deadFruit.active=false;
recTab[0].x=2*SQUARE_SIZE + offset.x/2; //mur haut gauche
recTab[0].y=2*SQUARE_SIZE + offset.y/2;
recTab[0].width=(SQUARE_SIZE)-0.1;
recTab[0].height=(SQUARE_SIZE*4)-0.1;
recTab[1].x=3*SQUARE_SIZE + offset.x/2;
recTab[1].y=2*SQUARE_SIZE + offset.y/2;
recTab[1].width=(SQUARE_SIZE*3)-0.1;
recTab[1].height=(SQUARE_SIZE)-0.1;
recTab[2].x=screenWidth-(offset.x/2)-(3*SQUARE_SIZE); //mur haut droit
recTab[2].y=2*SQUARE_SIZE + offset.y/2;
recTab[2].width=(SQUARE_SIZE)-0.1;
recTab[2].height=(SQUARE_SIZE*4)-0.1;
recTab[3].x=screenWidth-(offset.x/2)-(6*SQUARE_SIZE);
recTab[3].y=2*SQUARE_SIZE + offset.y/2;
recTab[3].width=(SQUARE_SIZE*3)-0.1;
recTab[3].height=(SQUARE_SIZE)-0.1;
recTab[4].x=2*SQUARE_SIZE + offset.x/2; //mur bas gauche
recTab[4].y=screenHeight-(offset.y/2)-(6*SQUARE_SIZE);
recTab[4].width=(SQUARE_SIZE)-0.1;
recTab[4].height=(SQUARE_SIZE*3)-0.1;
recTab[5].x=2*SQUARE_SIZE + offset.x/2;
recTab[5].y=screenHeight-(offset.y/2)-(3*SQUARE_SIZE);
recTab[5].width=(SQUARE_SIZE*4)-0.1;
recTab[5].height=(SQUARE_SIZE)-0.1;
recTab[6].x=screenWidth-(offset.x/2)-(3*SQUARE_SIZE); //mur bas droit
recTab[6].y=screenHeight-(offset.y/2)-(6*SQUARE_SIZE);
recTab[6].width=(SQUARE_SIZE)-0.1;
recTab[6].height=(SQUARE_SIZE*4)-0.1;
recTab[7].x=screenWidth-(offset.x/2)-(6*SQUARE_SIZE);
recTab[7].y=screenHeight-(offset.y/2)-(3*SQUARE_SIZE);
recTab[7].width=(SQUARE_SIZE*3)-0.1;
recTab[7].height=(SQUARE_SIZE)-0.1;
}
void drawWalls(){
//mur haut gauche
DrawRectangleRec(recTab[0],DARKBROWN);
DrawRectangleRec(recTab[1],DARKBROWN);
//mur haut droit
DrawRectangleRec(recTab[2],DARKBROWN);
DrawRectangleRec(recTab[3],DARKBROWN);
//mur bas gauche
DrawRectangleRec(recTab[4],DARKBROWN);
DrawRectangleRec(recTab[5],DARKBROWN);
//mur bas droit
DrawRectangleRec(recTab[6],DARKBROWN);
DrawRectangleRec(recTab[7],DARKBROWN);
}
bool checkCollisionMur(Vector2 position){
bool res = false;
for(int i=0; i<NUMBEROFWALL;i++){
if(CheckCollisionPointRec(position, recTab[i])) res=true;
}
return res;
}
// Update game (one frame)
......@@ -205,16 +271,9 @@ void UpdateGame(void)
else snake[i].position = snakePosition[i-1];
}
}
// 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))
{
gameOver = true;
}
*/
//collison mur
if(checkCollisionMur(snake[0].position))gameOver=true;
//teleport wall
if((snake[0].position.x)>(screenWidth-offset.x)){
......@@ -246,7 +305,7 @@ void UpdateGame(void)
for (int i = 0; i < counterTail; i++)
{
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))||checkCollisionMur(fruit.position))
{
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;
......@@ -258,7 +317,7 @@ void UpdateGame(void)
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)))
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))||checkCollisionMur(goldenFruit.position))
{
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;
......@@ -268,7 +327,7 @@ void UpdateGame(void)
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)))
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))||checkCollisionMur(deadFruit.position))
{
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;
......@@ -342,15 +401,20 @@ void DrawGame(void)
// Draw snake
for (int i = 0; i < counterTail; i++) DrawRectangleV(snake[i].position, snake[i].size, snake[i].color);
//Draw walls
drawWalls();
// Draw fruit to pick
DrawRectangleV(fruit.position, fruit.size, fruit.color);
if(counterTail%4==0) DrawRectangleV(goldenFruit.position, goldenFruit.size, goldenFruit.color);
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);
//score et timer en temps reel
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("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 70)/2, screenHeight/2 - 120, 70, 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);
}
......@@ -375,3 +439,4 @@ void UpdateDrawFrame(void)
UpdateGame();
DrawGame();
}
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