Commit c8801c19 authored by MESLIN Léa's avatar MESLIN Léa

Update arkanoid_lea_meslin.c

parent d507ebcb
...@@ -47,6 +47,9 @@ typedef struct Brick { ...@@ -47,6 +47,9 @@ typedef struct Brick {
static const int screenWidth = 800; static const int screenWidth = 800;
static const int screenHeight = 450; static const int screenHeight = 450;
static int score= 0;
static int highScore = -50;
static bool gameOver = false; static bool gameOver = false;
static bool pause = false; static bool pause = false;
...@@ -123,6 +126,9 @@ void InitGame(void) ...@@ -123,6 +126,9 @@ void InitGame(void)
// Initialize bricks // Initialize bricks
int initialDownPosition = 50; int initialDownPosition = 50;
score=0;
highScore=highScore;
for (int i = 0; i < LINES_OF_BRICKS; i++) for (int i = 0; i < LINES_OF_BRICKS; i++)
{ {
for (int j = 0; j < BRICKS_PER_LINE; j++) for (int j = 0; j < BRICKS_PER_LINE; j++)
...@@ -138,33 +144,40 @@ void InitGame(void) ...@@ -138,33 +144,40 @@ void InitGame(void)
} }
} }
} }
/*
brick[2][3].couleur =1;
brick[4][10].couleur=1;
brick[4][2].couleur=2;
brick[4][3].couleur=2;
brick[4][4].couleur=3;
brick[4][12].couleur=3;*/
} }
void briqueCouleurRouge(int i, int j){ void briqueCouleurRouge(int i, int j){
if ((brick[i][j].couleur== 1)){ if ((brick[i][j].couleur== 1)){
int k= i; int k= i;
int l= j; int l= j;
if (k>0){ if (k==0){
brick[k-1][l].active=false; brick[k+1][l].active=false;
brick[k][l-1].active=false;
brick[k][l+1].active=false; brick[k][l+1].active=false;
}
else if (l==19 && k==0){
brick[k+1][l].active=false;
brick[k][l-1].active=false; brick[k][l-1].active=false;
brick[k-1][l-1].active=false;
brick[k-1][l+1].active=false;
} }
else { else if (l==0 && k==0){
brick[k+1][l].active=false;
brick[k][l+1].active=false; brick[k][l+1].active=false;
}
else if (l==19 && k>0){
brick[k+1][l].active=false;
brick[k-1][l].active=false;
brick[k][l-1].active=false; brick[k][l-1].active=false;
}
else if (l==0 && k>0){
brick[k+1][l].active=false; brick[k+1][l].active=false;
brick[k+1][l+1].active=false; brick[k-1][l].active=false;
brick[k+1][l-1].active=false; brick[k][l+1].active=false;
}
else{
brick[k+1][l].active=false;
brick[k-1][l].active=false;
brick[k][l-1].active=false;
brick[k][l+1].active=false;
} }
} }
} }
...@@ -179,18 +192,23 @@ void briqueCouleurNoire(int i, int j){ ...@@ -179,18 +192,23 @@ void briqueCouleurNoire(int i, int j){
void briqueCouleurVerte(int i, int j){ void briqueCouleurVerte(int i, int j){
if ((brick[i][j].couleur== 3)){ if ((brick[i][j].couleur== 3)){
ball.position.y=screenHeight*1/8; ball.position.y=screenHeight*1/8;
ball.speed.x+= -1/2;
} }
} }
// Update game (one frame) // Update game (one frame)
void UpdateGame(void) void UpdateGame(void)
{ {
if (!gameOver) if (!gameOver)
{ {
if (IsKeyPressed('P')) pause = !pause; if (IsKeyPressed('P')) pause = !pause;
if (!pause) if (!pause)
{ {
// Player movement logic // Player movement logic
if (IsKeyDown(KEY_LEFT)) player.position.x -= 5; if (IsKeyDown(KEY_LEFT)) player.position.x -= 5;
if ((player.position.x - player.size.x/2) <= 0) player.position.x = player.size.x/2; if ((player.position.x - player.size.x/2) <= 0) player.position.x = player.size.x/2;
...@@ -227,6 +245,7 @@ void UpdateGame(void) ...@@ -227,6 +245,7 @@ void UpdateGame(void)
ball.active = false; ball.active = false;
player.life--; player.life--;
score-=10;
} }
// Collision logic: ball vs player // Collision logic: ball vs player
...@@ -257,6 +276,7 @@ void UpdateGame(void) ...@@ -257,6 +276,7 @@ void UpdateGame(void)
briqueCouleurRouge(i,j); briqueCouleurRouge(i,j);
briqueCouleurNoire(i,j); briqueCouleurNoire(i,j);
briqueCouleurVerte(i,j); briqueCouleurVerte(i,j);
score +=1;
} }
// Hit above // Hit above
else if (((ball.position.y + ball.radius) >= (brick[i][j].position.y - brickSize.y/2)) && else if (((ball.position.y + ball.radius) >= (brick[i][j].position.y - brickSize.y/2)) &&
...@@ -268,6 +288,7 @@ void UpdateGame(void) ...@@ -268,6 +288,7 @@ void UpdateGame(void)
briqueCouleurRouge(i,j); briqueCouleurRouge(i,j);
briqueCouleurNoire(i,j); briqueCouleurNoire(i,j);
briqueCouleurVerte(i,j); briqueCouleurVerte(i,j);
score +=1;
} }
// Hit left // Hit left
else if (((ball.position.x + ball.radius) >= (brick[i][j].position.x - brickSize.x/2)) && else if (((ball.position.x + ball.radius) >= (brick[i][j].position.x - brickSize.x/2)) &&
...@@ -279,6 +300,7 @@ void UpdateGame(void) ...@@ -279,6 +300,7 @@ void UpdateGame(void)
briqueCouleurRouge(i,j); briqueCouleurRouge(i,j);
briqueCouleurNoire(i,j); briqueCouleurNoire(i,j);
briqueCouleurVerte(i,j); briqueCouleurVerte(i,j);
score +=1;
} }
// Hit right // Hit right
else if (((ball.position.x - ball.radius) <= (brick[i][j].position.x + brickSize.x/2)) && else if (((ball.position.x - ball.radius) <= (brick[i][j].position.x + brickSize.x/2)) &&
...@@ -290,17 +312,21 @@ void UpdateGame(void) ...@@ -290,17 +312,21 @@ void UpdateGame(void)
briqueCouleurRouge(i,j); briqueCouleurRouge(i,j);
briqueCouleurNoire(i,j); briqueCouleurNoire(i,j);
briqueCouleurVerte(i,j); briqueCouleurVerte(i,j);
score +=1;
} }
} }
} }
} }
// Game over logic // Game over logic
if (player.life <= 0) gameOver = true; if (player.life <= 0){
gameOver = true;
int scoreFinPartie= score;
if (scoreFinPartie > highScore) highScore = scoreFinPartie;
}
else else
{ {
gameOver = true; gameOver = true;
for (int i = 0; i < LINES_OF_BRICKS; i++) for (int i = 0; i < LINES_OF_BRICKS; i++)
{ {
for (int j = 0; j < BRICKS_PER_LINE; j++) for (int j = 0; j < BRICKS_PER_LINE; j++)
...@@ -308,9 +334,14 @@ void UpdateGame(void) ...@@ -308,9 +334,14 @@ void UpdateGame(void)
if (brick[i][j].active) gameOver = false; if (brick[i][j].active) gameOver = false;
} }
} }
if (gameOver){
int scoreFinPartie= score;
if (abs(scoreFinPartie) > highScore) highScore = scoreFinPartie;
} }
} }
} }
}
else else
{ {
if (IsKeyPressed(KEY_ENTER)) if (IsKeyPressed(KEY_ENTER))
...@@ -319,6 +350,7 @@ void UpdateGame(void) ...@@ -319,6 +350,7 @@ void UpdateGame(void)
gameOver = false; gameOver = false;
} }
} }
} }
// Draw game (one frame) // Draw game (one frame)
...@@ -336,6 +368,9 @@ void DrawGame(void) ...@@ -336,6 +368,9 @@ void DrawGame(void)
// Draw player lives // Draw player lives
for (int i = 0; i < player.life; i++) DrawRectangle(20 + 40*i, screenHeight - 30, 35, 10, LIGHTGRAY); for (int i = 0; i < player.life; i++) DrawRectangle(20 + 40*i, screenHeight - 30, 35, 10, LIGHTGRAY);
DrawText(TextFormat("SCORE : %i", score),screenWidth -120 , screenHeight -30 , 15, BLACK);
DrawText(TextFormat("HIGH SCORE : %i", highScore),screenWidth -160 , 10 , 15, BLACK);
// Draw ball // Draw ball
DrawCircleV(ball.position, ball.radius, MAROON); DrawCircleV(ball.position, ball.radius, MAROON);
...@@ -357,8 +392,20 @@ void DrawGame(void) ...@@ -357,8 +392,20 @@ void DrawGame(void)
if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY); if (pause) DrawText("GAME PAUSED", screenWidth/2 - MeasureText("GAME PAUSED", 40)/2, screenHeight/2 - 40, 40, GRAY);
} }
else DrawText("PRESS [ENTER] TO PLAY AGAIN", GetScreenWidth()/2 - MeasureText("PRESS [ENTER] TO PLAY AGAIN", 20)/2, GetScreenHeight()/2 - 50, 20, GRAY); else {
if (player.life ==0){
ClearBackground(BLACK);
DrawText("You'll do better next time :)", GetScreenWidth()/2 -MeasureText("You'll do better next time",20)/2, GetScreenHeight()/2 -90, 20, WHITE);
DrawText("Press [Enter] to play again", GetScreenWidth()/2 -MeasureText("Play [Enter] to play again",20)/2, GetScreenHeight()/2 -45, 20, WHITE);
DrawText(TextFormat("Your score : %i",score), GetScreenWidth()/2 -MeasureText("Your score : 25",25)/2, GetScreenHeight()/2 , 25, WHITE);
}
else {
ClearBackground(BLACK);
DrawText("Congrats you won !!", GetScreenWidth()/2 -MeasureText("Congrats you won !!",20)/2, GetScreenHeight()/2 -90, 20, WHITE);
DrawText("Press [Enter] to play again", GetScreenWidth()/2 -MeasureText("Play [Enter] to play again",20)/2, GetScreenHeight()/2 -45, 20, WHITE);
DrawText(TextFormat("Your score : %i",score), GetScreenWidth()/2 -MeasureText("Your score : 25",25)/2, GetScreenHeight()/2 , 25, WHITE);
}
}
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