2022-04-14 19:20:18 +00:00
|
|
|
#include "../include/raylib.h"
|
|
|
|
|
2022-03-30 00:29:19 +00:00
|
|
|
#if defined(PLATFORM_WEB)
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2022-03-27 20:40:15 +00:00
|
|
|
// screen variables
|
|
|
|
static const int screenWidth = 800;
|
|
|
|
static const int screenHeight = 450;
|
|
|
|
|
|
|
|
// Gamescreens
|
|
|
|
typedef enum GameScreen { TITLE = 0, GAMEPLAY, GAMEOVER, CREDITS } GameScreen;
|
|
|
|
|
|
|
|
// structs
|
|
|
|
typedef struct Ball {
|
|
|
|
Vector2 position;
|
|
|
|
Vector2 speed;
|
|
|
|
float radius;
|
2022-04-05 21:56:57 +00:00
|
|
|
float growth;
|
2022-03-27 20:40:15 +00:00
|
|
|
Color color;
|
|
|
|
bool active;
|
|
|
|
} Ball;
|
|
|
|
|
|
|
|
typedef struct Player {
|
2022-04-10 16:17:41 +00:00
|
|
|
Texture2D sprite;
|
|
|
|
int currentframe;
|
|
|
|
Vector2 sprite_pos;
|
|
|
|
Rectangle frameRec;
|
2022-03-27 20:40:15 +00:00
|
|
|
Rectangle hitbox;
|
|
|
|
int hp;
|
|
|
|
} Player;
|
|
|
|
|
|
|
|
// Game variables
|
|
|
|
static GameScreen currentScreen = { 0 };
|
|
|
|
static Sound fxbounce = { 0 };
|
|
|
|
static Player player = { 0 };
|
|
|
|
static Ball ball = { 0 };
|
|
|
|
static bool pause;
|
|
|
|
static bool mute;
|
2022-05-14 19:53:25 +00:00
|
|
|
static bool ShowHitbox;
|
2022-03-27 20:40:15 +00:00
|
|
|
static int pauseTimer;
|
2022-05-04 00:17:40 +00:00
|
|
|
static int score, bestscore;
|
2022-03-27 20:40:15 +00:00
|
|
|
static int selected = 0;
|
|
|
|
|
|
|
|
// Game functions
|
2022-04-30 16:06:03 +00:00
|
|
|
static void gameSetup(void);
|
|
|
|
static void updateGame(void);
|
|
|
|
static void drawGame(void);
|
|
|
|
static void gameReset(void);
|
|
|
|
static void gameLoop(void);
|
|
|
|
static void unloadGame(void);
|
2022-03-27 20:40:15 +00:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
InitWindow(screenWidth, screenHeight, "Avoid");
|
|
|
|
|
|
|
|
InitAudioDevice();
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
gameSetup();
|
2022-03-27 20:40:15 +00:00
|
|
|
|
2022-03-30 00:29:19 +00:00
|
|
|
#if defined(PLATFORM_WEB)
|
2022-04-30 16:06:03 +00:00
|
|
|
emscripten_set_main_loop(gameLoop, 0, 1);
|
2022-03-30 00:29:19 +00:00
|
|
|
#else
|
|
|
|
SetTargetFPS(60);
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
while (!WindowShouldClose()) gameLoop();
|
2022-03-30 00:29:19 +00:00
|
|
|
#endif
|
2022-03-27 20:40:15 +00:00
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
unloadGame();
|
2022-03-27 20:40:15 +00:00
|
|
|
|
|
|
|
CloseWindow();
|
|
|
|
|
2022-03-30 00:29:19 +00:00
|
|
|
return 0;
|
2022-03-27 20:40:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
void gameSetup(void)
|
2022-03-27 20:40:15 +00:00
|
|
|
{
|
2022-04-30 16:06:03 +00:00
|
|
|
// asset loading & setting of variable values
|
2022-03-27 20:40:15 +00:00
|
|
|
currentScreen = TITLE;
|
|
|
|
|
|
|
|
fxbounce = LoadSound("assets/sfx/boing.wav");
|
|
|
|
|
|
|
|
SetMasterVolume(0.2);
|
|
|
|
|
2022-04-10 16:17:41 +00:00
|
|
|
player.sprite = LoadTexture("assets/gfx/player.png");
|
2022-04-13 19:33:00 +00:00
|
|
|
player.currentframe = 0;
|
2022-03-27 20:40:15 +00:00
|
|
|
player.hp = 30;
|
2022-04-10 16:17:41 +00:00
|
|
|
player.frameRec = (Rectangle) {
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
2022-04-13 19:33:00 +00:00
|
|
|
(float) player.sprite.width/2,
|
2022-04-10 16:17:41 +00:00
|
|
|
(float) player.sprite.height
|
|
|
|
};
|
2022-03-27 20:40:15 +00:00
|
|
|
player.hitbox = (Rectangle) {
|
|
|
|
GetScreenWidth()/2.0f - 30,
|
|
|
|
GetScreenHeight()/2.0f - 30,
|
2022-04-10 16:17:41 +00:00
|
|
|
70,
|
|
|
|
70
|
2022-03-27 20:40:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ball.position = (Vector2){ 50, 50 };
|
2022-05-14 19:53:25 +00:00
|
|
|
ball.speed = (Vector2){ 400.0f, 300.0f };
|
2022-03-27 20:40:15 +00:00
|
|
|
ball.radius = 20;
|
2022-04-05 21:56:57 +00:00
|
|
|
ball.growth = 2;
|
2022-03-27 20:40:15 +00:00
|
|
|
ball.color = MAROON;
|
|
|
|
ball.active = true;
|
|
|
|
|
|
|
|
pause = 0;
|
|
|
|
mute = 0;
|
2022-05-14 19:53:25 +00:00
|
|
|
ShowHitbox = 0;
|
2022-03-27 20:40:15 +00:00
|
|
|
|
|
|
|
pauseTimer = 0;
|
2022-05-04 00:17:40 +00:00
|
|
|
score = 0;
|
2022-03-27 20:40:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
void updateGame(void)
|
2022-03-27 20:40:15 +00:00
|
|
|
{
|
2022-04-30 16:06:03 +00:00
|
|
|
// code that runs as long as the program is running
|
2022-04-13 19:33:00 +00:00
|
|
|
if ((IsKeyDown(KEY_LEFT_ALT)) && (IsKeyPressed(KEY_F))) ToggleFullscreen();
|
2022-03-27 20:40:15 +00:00
|
|
|
|
|
|
|
switch(currentScreen) {
|
|
|
|
case TITLE:
|
|
|
|
if (IsKeyPressed(KEY_UP)) selected++;
|
|
|
|
if (IsKeyPressed(KEY_DOWN)) selected--;
|
|
|
|
if (selected > 0) selected--;
|
2022-03-27 20:58:24 +00:00
|
|
|
if (selected < -2) selected++;
|
2022-03-27 20:40:15 +00:00
|
|
|
|
|
|
|
if ((selected == 0) && (IsKeyPressed(KEY_ENTER))) currentScreen = GAMEPLAY;
|
|
|
|
if ((selected == -1) && (IsKeyPressed(KEY_ENTER))) currentScreen = CREDITS;
|
2022-03-27 20:58:24 +00:00
|
|
|
if ((selected == -2) && (IsKeyPressed(KEY_ENTER))) OpenURL("https://gitdab.com/Canneddonuts/Avoid.git");
|
2022-03-27 20:40:15 +00:00
|
|
|
break;
|
|
|
|
case GAMEPLAY:
|
|
|
|
|
2022-04-13 19:33:00 +00:00
|
|
|
if (IsKeyPressed(KEY_M)) mute = !mute;
|
2022-03-27 20:40:15 +00:00
|
|
|
|
2022-03-30 00:29:19 +00:00
|
|
|
if (IsKeyPressed(KEY_ENTER)) pause = !pause;
|
2022-03-27 20:40:15 +00:00
|
|
|
|
|
|
|
if (!pause) {
|
|
|
|
// Controls
|
|
|
|
if (IsKeyDown(KEY_LEFT)) player.hitbox.x -= GetFrameTime() * 300.0f;
|
|
|
|
if (IsKeyDown(KEY_RIGHT)) player.hitbox.x += GetFrameTime() * 300.0f;
|
|
|
|
if (IsKeyDown(KEY_UP)) player.hitbox.y -= GetFrameTime() * 300.0f;
|
|
|
|
if (IsKeyDown(KEY_DOWN)) player.hitbox.y += GetFrameTime() * 300.0f;
|
|
|
|
|
2022-04-10 16:17:41 +00:00
|
|
|
player.sprite_pos = (Vector2){ player.hitbox.x, player.hitbox.y };
|
2022-04-13 19:33:00 +00:00
|
|
|
player.frameRec.x = (float)player.currentframe*(float)player.sprite.width/2;
|
2022-04-10 16:17:41 +00:00
|
|
|
|
2022-03-27 20:40:15 +00:00
|
|
|
// Player to da wallz collies
|
|
|
|
if ((player.hitbox.x + player.hitbox.width) >= GetScreenWidth()) player.hitbox.x = GetScreenWidth() - player.hitbox.width;
|
|
|
|
else if (player.hitbox.x <= 0) player.hitbox.x = 0;
|
|
|
|
|
|
|
|
if ((player.hitbox.y + player.hitbox.height) >= GetScreenHeight()) player.hitbox.y = GetScreenHeight() - player.hitbox.height;
|
|
|
|
else if (player.hitbox.y <= 0) player.hitbox.y = 0;
|
|
|
|
|
|
|
|
if (IsKeyPressed(KEY_D)) ball.active = !ball.active;
|
2022-05-14 19:53:25 +00:00
|
|
|
|
|
|
|
if (IsKeyPressed(KEY_H)) ShowHitbox = !ShowHitbox;
|
|
|
|
|
2022-05-04 00:17:40 +00:00
|
|
|
if (IsKeyPressed(KEY_R)) {
|
|
|
|
gameReset();
|
|
|
|
currentScreen = TITLE;
|
|
|
|
}
|
2022-05-14 19:53:25 +00:00
|
|
|
|
|
|
|
if (player.hp <= 0) {
|
|
|
|
gameReset();
|
|
|
|
currentScreen = GAMEOVER;
|
|
|
|
}
|
|
|
|
|
2022-03-27 20:40:15 +00:00
|
|
|
if (ball.active) {
|
2022-05-04 00:17:40 +00:00
|
|
|
score++;
|
2022-03-27 20:40:15 +00:00
|
|
|
// moveiement oof the balls
|
|
|
|
ball.position.x += GetFrameTime() * ball.speed.x;
|
|
|
|
ball.position.y += GetFrameTime() * ball.speed.y;
|
|
|
|
|
2022-05-04 00:17:40 +00:00
|
|
|
if (score >= bestscore) bestscore = score;
|
2022-03-27 20:40:15 +00:00
|
|
|
// Ballz to da wallz collies
|
2022-03-30 00:29:19 +00:00
|
|
|
if ((ball.position.x >= (GetScreenWidth() - ball.radius)) || (ball.position.x <= ball.radius)) {
|
|
|
|
ball.speed.x *= -1.0f;
|
|
|
|
if (!mute) PlaySoundMulti(fxbounce);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ball.position.y >= (GetScreenHeight() - ball.radius)) || (ball.position.y <= ball.radius)) {
|
|
|
|
ball.speed.y *= -1.0f;
|
|
|
|
if (!mute) PlaySoundMulti(fxbounce);
|
|
|
|
}
|
2022-03-27 20:40:15 +00:00
|
|
|
|
2022-04-13 19:33:00 +00:00
|
|
|
if (CheckCollisionCircleRec(ball.position, ball.radius, player.hitbox)) {
|
2022-05-14 19:53:25 +00:00
|
|
|
player.hp -= GetFrameTime() * 3.0f;
|
2022-04-13 19:33:00 +00:00
|
|
|
player.currentframe = 1;
|
|
|
|
} else player.currentframe = 0;
|
2022-03-27 20:40:15 +00:00
|
|
|
|
2022-04-10 16:17:41 +00:00
|
|
|
if (ball.radius <= 100) ball.radius += GetFrameTime() * ball.growth;
|
2022-03-27 20:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else pauseTimer++;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case GAMEOVER:
|
2022-05-04 00:17:40 +00:00
|
|
|
if (score > bestscore) bestscore = score;
|
2022-03-27 20:40:15 +00:00
|
|
|
if (IsKeyPressed(KEY_ENTER)) {
|
2022-04-30 16:06:03 +00:00
|
|
|
gameReset();
|
2022-03-27 20:40:15 +00:00
|
|
|
currentScreen = GAMEPLAY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CREDITS:
|
|
|
|
if (IsKeyPressed(KEY_ENTER)) currentScreen = TITLE;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
void drawGame(void)
|
2022-03-27 20:40:15 +00:00
|
|
|
{
|
2022-04-30 16:06:03 +00:00
|
|
|
// code to render the game to the game window
|
2022-03-27 20:40:15 +00:00
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
switch(currentScreen) {
|
|
|
|
case TITLE:
|
|
|
|
DrawRectangle(0, 0, screenWidth, screenHeight, ORANGE);
|
|
|
|
DrawText("Controls", 10, 10, 30, PURPLE);
|
2022-05-04 00:17:40 +00:00
|
|
|
DrawText(TextFormat("BEST: %i", bestscore), 600, 0, 30, WHITE);
|
2022-03-27 20:40:15 +00:00
|
|
|
DrawText("Press the arrow keys to move", 10, 40, 10, RED);
|
2022-03-30 00:29:19 +00:00
|
|
|
DrawText("Press 'ENTER' to pause", 10, 60, 10, RED);
|
2022-03-27 20:40:15 +00:00
|
|
|
DrawText("Press 'M' to mute", 10, 80, 10, RED);
|
2022-04-13 19:33:00 +00:00
|
|
|
DrawText("Press 'Left-ALT' + 'F' for full screen", 10, 100, 10, RED);
|
2022-03-27 20:40:15 +00:00
|
|
|
DrawText("Press 'R' to restart", 10, 120, 10, RED);
|
|
|
|
DrawText("Press 'ENTER' to select an option", 10, 140, 10, RED);
|
|
|
|
DrawText("Avoid", 330, 20, 50, BLUE);
|
|
|
|
if (selected == 0) DrawText("PLAY", 360, 220, 20, WHITE);
|
|
|
|
else DrawText("PLAY", 360, 220, 20, BLUE);
|
|
|
|
|
|
|
|
if (selected == -1) DrawText("CREDITS", 340, 240, 20, WHITE);
|
|
|
|
else DrawText("CREDITS", 340, 240, 20, BLUE);
|
2022-03-27 20:58:24 +00:00
|
|
|
|
|
|
|
if (selected == -2) DrawText("SOURCE CODE", 315, 260, 20, WHITE);
|
|
|
|
else DrawText("SOURCE CODE", 315, 260, 20, BLUE);
|
2022-03-27 20:40:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GAMEPLAY:
|
|
|
|
DrawRectangle(0, 0, screenWidth, screenHeight, BLACK);
|
|
|
|
DrawFPS(10, 430);
|
|
|
|
DrawText(TextFormat("HP: %i", player.hp), 10, 10, 20, RED);
|
2022-05-04 00:17:40 +00:00
|
|
|
DrawText(TextFormat("SCORE: %i", score), 10, 30, 20, BLUE);
|
2022-03-27 20:40:15 +00:00
|
|
|
DrawText(TextFormat("BALL SIZE: %f", ball.radius), 10, 50, 20, PINK);
|
|
|
|
if (ball.active) DrawCircleV(ball.position, (float)ball.radius, ball.color);
|
2022-05-14 19:53:25 +00:00
|
|
|
if (ShowHitbox) DrawRectangleRec(player.hitbox, BLUE);
|
2022-04-10 16:17:41 +00:00
|
|
|
DrawTextureRec(player.sprite, player.frameRec, player.sprite_pos, WHITE);
|
2022-03-27 20:40:15 +00:00
|
|
|
if (pause && ((pauseTimer/30)%2)) DrawText("PAUSED", 330, 190, 30, PURPLE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GAMEOVER:
|
|
|
|
DrawRectangle(0, 0, screenWidth, screenHeight, BLUE);
|
|
|
|
DrawText("GAMEOVER", 250, 20, 50, RED);
|
|
|
|
DrawText("PRESS ENTER TO RESET", 270, 220, 20, WHITE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CREDITS:
|
|
|
|
DrawRectangle(0, 0, screenWidth, screenHeight, GREEN);
|
2022-03-30 00:29:19 +00:00
|
|
|
DrawText("Avoid", 330, 20, 50, PINK);
|
2022-05-04 00:17:40 +00:00
|
|
|
DrawText("Programming by Return0ne", 10, 210, 20, BLUE);
|
2022-03-27 20:40:15 +00:00
|
|
|
DrawText("Morale support by Tobi/Tobrella and Jelly_man", 10, 240, 20, BLUE);
|
|
|
|
DrawText("Powered by raylib 4.0", 10, 270, 20, BLUE);
|
2022-03-30 00:29:19 +00:00
|
|
|
DrawText("A Canneddonuts project 2022", 10, 310, 40, RED);
|
2022-03-27 20:40:15 +00:00
|
|
|
DrawText("Press 'ENTER' ", 10, 350, 20, WHITE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
EndDrawing();
|
|
|
|
}
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
void gameReset(void)
|
|
|
|
{
|
|
|
|
// code to reset all variables without reloading assets
|
|
|
|
currentScreen = TITLE;
|
|
|
|
|
|
|
|
player.currentframe = 0;
|
|
|
|
player.hp = 30;
|
|
|
|
player.hitbox = (Rectangle) {
|
|
|
|
GetScreenWidth()/2.0f - 30,
|
|
|
|
GetScreenHeight()/2.0f - 30,
|
|
|
|
70,
|
|
|
|
70
|
|
|
|
};
|
|
|
|
|
|
|
|
ball.position = (Vector2){ 50, 50 };
|
|
|
|
ball.radius = 20;
|
|
|
|
ball.active = true;
|
|
|
|
|
2022-05-14 19:53:25 +00:00
|
|
|
ShowHitbox = 0;
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
pauseTimer = 0;
|
2022-05-04 00:17:40 +00:00
|
|
|
score = 0;
|
2022-04-30 16:06:03 +00:00
|
|
|
}
|
2022-03-27 20:40:15 +00:00
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
void gameLoop(void)
|
2022-03-27 20:40:15 +00:00
|
|
|
{
|
2022-04-30 16:06:03 +00:00
|
|
|
updateGame();
|
|
|
|
drawGame();
|
2022-03-27 20:40:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 16:06:03 +00:00
|
|
|
void unloadGame(void)
|
2022-03-27 20:40:15 +00:00
|
|
|
{
|
|
|
|
UnloadSound(fxbounce);
|
2022-04-10 16:17:41 +00:00
|
|
|
UnloadTexture(player.sprite);
|
2022-03-27 20:40:15 +00:00
|
|
|
}
|