Refactor a bit

This commit is contained in:
bdnugget committed 2024-06-07 12:42:44 +02:00
1 parent 2944a6624a
commit be5374ff11
4 files changed
+55 -50

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
# Auto game for my little man
```sh
gcc -o auto auto.c -lraylib -lGL -lm -lpthread -ldl -lrt -Wl,-rpath,/usr/lib/x86_64-linux-gnu -I/usr/include/raylib -D_DEFAULT_SOURCE
```
BIN
View File
Binary file not shown.
+51 -50
View File
@@ -32,24 +32,25 @@ typedef struct Garage {
Color color;
} Garage;
typedef struct {
int score;
Car car;
int framesCounter;
bool gameOver;
bool pause;
bool allowMove;
Garage* garages;
} GameState;
//----------------------------------------------------------------------------------
// Global Variables Declaration
//----------------------------------------------------------------------------------
static const int screenWidth = 1600;
static const int screenHeight = 1000;
static int framesCounter = 0;
static bool gameOver = false;
static bool pause = false;
static Car car;
static bool allowMove = false;
static Vector2 offset;
static Texture autoTexture;
static Texture garageTexture;
static Garage garages[GARAGE_COUNT];
static Color garageColors[GARAGE_COUNT] = {
RED, GREEN, BLUE, YELLOW, PURPLE
};
@@ -57,7 +58,9 @@ static Color garageColors[GARAGE_COUNT] = {
static Sound autoHappy;
static Sound autoSad;
static int score = 0;
static GameState gameState;
static Vector2 offset = {screenWidth % SQUARE_SIZE, screenHeight % SQUARE_SIZE};
//----------------------------------------------------------------------------------
// Module Functions Declaration (local)
@@ -78,7 +81,6 @@ int main(void) {
autoHappy = LoadSound("resources/auto_happy_vob.ogg");
autoSad = LoadSound("resources/auto_sad_vob.ogg");
autoTexture = LoadTexture("resources/car_200px.png");
garageTexture = LoadTexture("resources/autogarage_200px.png");
InitGame();
@@ -105,66 +107,64 @@ int main(void) {
// Initialize game variables
void InitGame(void) {
SetRandomSeed(time(NULL));
int randomIndex = GetRandomValue(0, 4);
car.color = garageColors[randomIndex];
framesCounter = 0;
gameOver = false;
pause = false;
allowMove = false;
gameState.score = 0;
gameState.framesCounter = 0;
gameState.gameOver = false;
gameState.pause = false;
gameState.allowMove = false;
gameState.car = (Car){ (Vector2){offset.x / 2, offset.y / 2 + 2 * SQUARE_SIZE}, (Vector2){SQUARE_SIZE, SQUARE_SIZE}, garageColors[randomIndex], 0 };
gameState.garages = (Garage*)malloc(GARAGE_COUNT * sizeof(Garage));
offset.x = screenWidth % SQUARE_SIZE;
offset.y = screenHeight % SQUARE_SIZE;
car.position = (Vector2){offset.x / 2, offset.y / 2 + 2 * SQUARE_SIZE};
car.size = (Vector2){SQUARE_SIZE, SQUARE_SIZE};
InitGarages();
}
// Update game (one frame)
void UpdateGame(void) {
if (!gameOver) {
if (!gameState.gameOver) {
if (IsKeyPressed('P'))
pause = !pause;
gameState.pause = !gameState.pause;
if (!pause) {
if (IsKeyPressed(KEY_UP) && allowMove && car.position.y > 0) {
car.direction = -1;
allowMove = false;
if (!gameState.pause) {
if (IsKeyPressed(KEY_UP) && gameState.allowMove && gameState.car.position.y > 0) {
gameState.car.direction = -1;
gameState.allowMove = false;
}
if (IsKeyPressed(KEY_DOWN) && allowMove && car.position.y < (screenHeight - SQUARE_SIZE)) {
car.direction = 1;
allowMove = false;
if (IsKeyPressed(KEY_DOWN) && gameState.allowMove && gameState.car.position.y < (screenHeight - SQUARE_SIZE)) {
gameState.car.direction = 1;
gameState.allowMove = false;
}
if ((framesCounter % (FPS_TARGET / 60)) == 0) {
car.position.x += SQUARE_SIZE/FPS_TARGET;
car.position.y += car.direction * SQUARE_SIZE;
allowMove = true;
car.direction = 0;
if ((gameState.framesCounter % (FPS_TARGET / 60)) == 0) {
gameState.car.position.x += (float)SQUARE_SIZE/FPS_TARGET;
gameState.car.position.y += gameState.car.direction * SQUARE_SIZE;
gameState.allowMove = true;
gameState.car.direction = 0;
}
for (int i = 0; i < GARAGE_COUNT; i++) {
if (CheckCollisionRecs((Rectangle){car.position.x, car.position.y, car.size.x, car.size.y},
(Rectangle){garages[i].position.x, garages[i].position.y, garages[i].size.x, garages[i].size.y})) {
if (ColorToInt(car.color) == ColorToInt(garages[i].color)) {
if (CheckCollisionRecs((Rectangle){gameState.car.position.x, gameState.car.position.y, gameState.car.size.x, gameState.car.size.y},
(Rectangle){gameState.garages[i].position.x, gameState.garages[i].position.y, gameState.garages[i].size.x, gameState.garages[i].size.y})) {
if (ColorToInt(gameState.car.color) == ColorToInt(gameState.garages[i].color)) {
PlaySound(autoHappy);
score++;
gameState.score++;
InitGame();
} else {
PlaySound(autoSad);
gameOver = true;
gameState.gameOver = true;
}
}
}
framesCounter++;
gameState.framesCounter++;
}
} else {
if (IsKeyPressed(KEY_ENTER)) {
score = 0;
gameState.score = 0;
InitGame();
}
}
@@ -176,25 +176,25 @@ void DrawGame(void) {
ClearBackground(DARKGRAY);
if (!gameOver) {
if (!gameState.gameOver) {
for (int i = 1; i < screenHeight / SQUARE_SIZE + 1; i++) {
for (int j = 0; j < screenWidth / (SQUARE_SIZE / 2); j += 2) {
DrawRectangle(j * (SQUARE_SIZE / 2) + offset.x / 2, SQUARE_SIZE * i + offset.y / 2, SQUARE_SIZE / 2, SQUARE_SIZE / 8, RAYWHITE);
DrawRectangle(j * ((float)SQUARE_SIZE / 2) + offset.x / 2, SQUARE_SIZE * i + offset.y / 2, SQUARE_SIZE / 2, SQUARE_SIZE / 8, RAYWHITE);
}
}
// Draw garages
for (int i = 0; i < GARAGE_COUNT; i++) {
DrawRectangleRec((Rectangle){garages[i].position.x, garages[i].position.y, garages[i].size.x, garages[i].size.y}, garages[i].color);
DrawRectangleRec((Rectangle){gameState.garages[i].position.x, gameState.garages[i].position.y, gameState.garages[i].size.x, gameState.garages[i].size.y}, gameState.garages[i].color);
}
DrawTextureV(autoTexture, car.position, car.color);
DrawTextureV(autoTexture, gameState.car.position, gameState.car.color);
// Draw score
DrawText(TextFormat("Score: %d", score), 20, 20, 20, RAYWHITE);
DrawText(TextFormat("Score: %d", gameState.score), 20, 20, 20, RAYWHITE);
if (pause)
if (gameState.pause)
DrawText("GAME PAUSED", screenWidth / 2 - MeasureText("GAME PAUSED", 40) / 2, screenHeight / 2 - 40, 40, GRAY);
} else
DrawText(
@@ -208,9 +208,9 @@ void DrawGame(void) {
void InitGarages(void) {
for (int i = 0; i < GARAGE_COUNT; i++) {
garages[i].size = (Vector2){SQUARE_SIZE, SQUARE_SIZE};
garages[i].position = (Vector2){screenWidth - garages[i].size.x, i * garages[i].size.y};
garages[i].color = garageColors[i];
gameState.garages[i].size = (Vector2){SQUARE_SIZE, SQUARE_SIZE};
gameState.garages[i].position = (Vector2){screenWidth - gameState.garages[i].size.x, i * gameState.garages[i].size.y};
gameState.garages[i].color = garageColors[i];
}
}
@@ -220,6 +220,7 @@ void UnloadGame(void) {
UnloadTexture(garageTexture);
UnloadSound(autoSad);
UnloadSound(autoHappy);
free(gameState.garages);
CloseAudioDevice();
}
Executable → Regular
View File
File mode changed.