Commit 19c7cb3a authored by Jan Hendrik Stratmann's avatar Jan Hendrik Stratmann
Browse files

Added Game Over Screen

parent 97471039
......@@ -6,6 +6,8 @@ target_sources(${PROJECT_NAME}
main.c
menu.h
menu.c
gameover.h
gameover.c
game.h
game.c
alien.h
......
#include "raylib.h"
#include <stddef.h>
#define GAME_OVER_FONT_SIZE 50
#define SCORE_FONT_SIZE 40
#define CONTINUE_FONT_SIZE 20
extern const int GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT;
Vector2 gameOverBtnPos, scoreBtnPos, highscoreBtnPos, continueBtnPos;
int score, highscore;
void initGameOver() { //Get position of text boxes
//Game over
gameOverBtnPos = (Vector2) { (GAME_SCREEN_WIDTH - (float) MeasureText("GAME OVER!", GAME_OVER_FONT_SIZE)) / 2, (float) GAME_SCREEN_HEIGHT / 5 };
//Score of latest game
scoreBtnPos = (Vector2) { (GAME_SCREEN_WIDTH - (float) MeasureText("Score: 100 s", SCORE_FONT_SIZE)) / 2, (float) GAME_SCREEN_HEIGHT * 2 / 5 };
//Highscore
highscoreBtnPos = (Vector2) { (GAME_SCREEN_WIDTH - (float) MeasureText("Highcore: 100 s", SCORE_FONT_SIZE)) / 2, (float) GAME_SCREEN_HEIGHT * 3 / 5 };
//Information how to continue
continueBtnPos = (Vector2) { (GAME_SCREEN_WIDTH - (float) MeasureText("Press <Enter> to continue...", CONTINUE_FONT_SIZE)) / 2, (float) GAME_SCREEN_HEIGHT * 4 / 5 };
}
void calcGameOver(int s) {
score = s;
highscore = StorageLoadValue(0);
if(highscore < score){ // Update Highscore
StorageSaveValue(0, score);
highscore = score;
}
}
void drawGameOver() {
DrawText("GAME OVER!", (int) gameOverBtnPos.x, (int) gameOverBtnPos.y, GAME_OVER_FONT_SIZE, RED);
DrawText(FormatText("Score: %d s", score), (int) scoreBtnPos.x, (int) scoreBtnPos.y, SCORE_FONT_SIZE, RED);
DrawText(FormatText("Highscore: %d s", highscore), (int) highscoreBtnPos.x, (int) highscoreBtnPos.y, SCORE_FONT_SIZE, RED);
DrawText("Press <Enter> to continue...", (int) continueBtnPos.x, (int) continueBtnPos.y, CONTINUE_FONT_SIZE, WHITE);
}
#pragma once
void initGameOver();
void calcGameOver();
void drawGameOver();
#include "menu.h"
#include "game.h"
#include "gameover.h"
#include <raylib.h>
......@@ -10,6 +11,8 @@
const int GAME_SCREEN_WIDTH = 400, GAME_SCREEN_HEIGHT = 400, FRAME_RATE = 60;
int FRAMES_COUNT = 0;
int startTime = 0, endTime = 0;
int main() {
SetConfigFlags(FLAG_VSYNC_HINT);
InitWindow(GetScreenWidth(), GetScreenHeight(), "Space Intruders");
......@@ -23,10 +26,12 @@ int main() {
RenderTexture2D target = LoadRenderTexture(GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
SetTextureFilter(target.texture, FILTER_BILINEAR);
enum { MENU, GAME } state = MENU;
enum { MENU, GAME, GAME_OVER } state = MENU;
initMenu();
initGame();
initGameOver();
while (!WindowShouldClose()) {
if (IsKeyPressed(KEY_F11)) {
......@@ -36,11 +41,18 @@ int main() {
if (IsKeyPressed(KEY_ENTER) && state == MENU) {
state = GAME;
initGame();
startTime = GetTime();
} // Enter to start the game when in menu
if (IsKeyPressed(KEY_BACKSPACE) && state == GAME) {
state = GAME_OVER;
endTime=GetTime();
calcGameOver(endTime-startTime);
} //Backspace to end game
if (IsKeyPressed(KEY_ENTER) && state == GAME_OVER) {
state = MENU;
} // Backspace to go back to the menu
} // Enter to go back to the menu
float scale = min((float) GetScreenWidth() / GAME_SCREEN_WIDTH, (float) GetScreenHeight() / GAME_SCREEN_HEIGHT);
......@@ -58,6 +70,8 @@ int main() {
drawMenu();
} else if (state == GAME) {
drawGame();
} else if (state == GAME_OVER) {
drawGameOver();
}
EndTextureMode();
......
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