diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 134 | ||||
-rw-r--r-- | src/pong.h | 3 | ||||
-rw-r--r-- | src/title.c | 9 | ||||
-rw-r--r-- | src/versus.c | 121 |
4 files changed, 138 insertions, 129 deletions
diff --git a/src/main.c b/src/main.c index de4dcb0..11c69b7 100644 --- a/src/main.c +++ b/src/main.c @@ -98,144 +98,30 @@ int main() { SetWindowState(FLAG_WINDOW_RESIZABLE); SetWindowMinSize(1280, 720); - //Initialize Variables - Camera2D MainCamera; - MainCamera.target = (Vector2){0, 0}; - MainCamera.offset = (Vector2){0, 0}; - MainCamera.rotation = 0; //Populate Audio Queue for(unsigned int i = 0; i < 20; i++) { AudioQueue[i] = -1; } mtx_init(&AudioQueueBeingModified, mtx_plain); - // Init Player Variables - struct Players Player; - Player.Y = 0; - Player.Score = 0; - char PlayerScore[50]; // Used later to display score on screen. - - // Init Enemy Variables - struct Players Enemy; - Enemy.Y = 0; - Enemy.Score = 0; - Enemy.Direction = 0; - char EnemyScore[50]; - - struct Balls Ball; - Ball.X = 1280/2.0f; - Ball.Y = 720/2.0f; - Ball.Direction = LEFT; - Ball.Speed = 3.0f; - Ball.Angle = 0.0f; - - - // Set Sprites - Texture2D PaddleSprite = LoadTexture("resources/paddle.png"); - Texture2D BallSprite = LoadTexture("resources/ball.png"); - - // Set Collision Boxes - Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height}; - Enemy.HitBox = (Rectangle){1200, Enemy.Y, 5, PaddleSprite.height}; - Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height}; - Enemy.BallDetector = (Rectangle){0, Enemy.Y+120, 1280, PaddleSprite.height/5.0f}; - // Initialize Internal Clock thrd_t InternalClock; thrd_create(&InternalClock, internal_clock, NULL); thrd_t AudioThread; thrd_create(&AudioThread, audio, NULL); - // Launch Title Screen - jmp_buf RestartGame; - setjmp(RestartGame); - title_screen(); - play_audio(99); - Enemy.Score = 0; - Player.Score = 0; - - while(!WindowShouldClose() && GameGoing == true) { - MainCamera.zoom = GetScreenHeight()/720.0f; - MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; - MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; - - - // Who won? - if (Enemy.Score >= 5) { - StopSoundMulti(); - play_audio(MUSIC_DEFEAT); - while(!IsKeyDown(KEY_SPACE)) { - BeginDrawing(); - EnableCursor(); - ClearBackground(BLACK); - DrawText("You Lose.", 0, 0, 128, RED); - DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); - EndDrawing(); - } - longjmp(RestartGame, 0); - } else if (Player.Score >= 5) { - StopSoundMulti(); - play_audio(MUSIC_VICTORY); - while(!IsKeyDown(KEY_SPACE)) { - BeginDrawing(); - EnableCursor(); - ClearBackground(BLACK); - DrawText("You Win!", 0, 0, 128, BLUE); - DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); - EndDrawing(); - } - longjmp(RestartGame, 0); - } - - //Controls - if(IsKeyDown(KEY_UP)) { - Player.Y -= 10; - } else if (IsKeyDown(KEY_DOWN)) { - Player.Y += 10; - } else if(IsKeyPressed(KEY_ESCAPE)) { - EnableCursor(); - } else if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsCursorHidden() == true) { - Player.Y = GetMouseY()-PaddleSprite.height/2.0f; - DisableCursor(); - } - - if(GetMouseY() < 0) { - SetMousePosition(0, 0); - } else if(GetMouseY() > 720) { - SetMousePosition(0, 720); - } - - //Check if players are off-screen - if (Player.Y < 0) { - Player.Y = 0; - } else if (Player.Y > 480) { - Player.Y = 480; + // Launch Game + while (GameGoing == true) { + switch(title_screen()) { + case 0: //Versus + play_audio(STOP_ALL_SOUNDS); + versus_main(); + break; + default: + break; } - - enemy(&Enemy, Ball); - - // Collision - ball(&Player.HitBox, &Enemy.HitBox, &Ball, &Player.Score, &Enemy.Score); - - //Updates hitbox with player's position. - Player.HitBox.y = Player.Y; - - //Turn Scores into strings. - snprintf(PlayerScore, 50, "Player: %d", Player.Score); - snprintf(EnemyScore, 50, "Enemy: %d", Enemy.Score); - - BeginDrawing(); - ClearBackground(BLACK); - BeginMode2D(MainCamera); - DrawTexture(PaddleSprite, 0, Player.Y, WHITE); - DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE); - DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE); - //DrawRectangleRec(Enemy.BallDetector, RED); - DrawText(PlayerScore, 0, 0, 32, GREEN); - DrawText(EnemyScore, 1130, 688, 32, RED); - EndMode2D(); - EndDrawing(); } + GameGoing = false; thrd_join(AudioThread, NULL); CloseWindow(); diff --git a/src/pong.h b/src/pong.h index 70e9215..26ec9c2 100644 --- a/src/pong.h +++ b/src/pong.h @@ -38,7 +38,8 @@ extern atomic_int Ticks; void enemy(struct Players *Enemy, struct Balls ball); void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerScore, int *EnemyScore); bool play_audio(int SoundEffect); -void title_screen(); +int title_screen(); +void versus_main(); //Sounds extern const int SOUND_BOUNCE; diff --git a/src/title.c b/src/title.c index 1f2a51d..56e382c 100644 --- a/src/title.c +++ b/src/title.c @@ -4,7 +4,8 @@ void help_text() { } -void title_screen() { +int title_screen() { + // Init Camera Camera2D MainCamera; MainCamera.offset = (Vector2){0,0}; MainCamera.target = (Vector2){0,0}; @@ -38,7 +39,7 @@ void title_screen() { if (CheckCollisionRecs(Mouse, Versus)) { Selected = &Versus; if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - return; + return 0; } } else if (CheckCollisionRecs(Mouse, Marathon)) { Selected = &Marathon; @@ -50,7 +51,7 @@ void title_screen() { Selected = &Exit; if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { GameGoing = false; - return; + return -1; } } BeginDrawing(); @@ -67,5 +68,5 @@ void title_screen() { EndMode2D(); EndDrawing(); } - return; + return -1; } diff --git a/src/versus.c b/src/versus.c new file mode 100644 index 0000000..a5c1060 --- /dev/null +++ b/src/versus.c @@ -0,0 +1,121 @@ +#include "pong.h" + +void versus_main() { + // Init Player Variables + struct Players Player; + Player.Y = 0; + Player.Score = 0; + + // Init Enemy Variables + struct Players Enemy; + Enemy.Y = 0; + Enemy.Score = 0; + Enemy.Direction = 0; + + // Init Ball Variables + struct Balls Ball; + Ball.X = 1280/2.0f; + Ball.Y = 720/2.0f; + Ball.Direction = LEFT; + Ball.Speed = 3.0f; + Ball.Angle = 0.0f; + + // Set Sprites + Texture2D PaddleSprite = LoadTexture("resources/paddle.png"); + Texture2D BallSprite = LoadTexture("resources/ball.png"); + + // Set Collision Boxes + Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height}; + Enemy.HitBox = (Rectangle){1200, Enemy.Y, 5, PaddleSprite.height}; + Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height}; + Enemy.BallDetector = (Rectangle){0, Enemy.Y+120, 1280, PaddleSprite.height/5.0f}; + char EnemyScore[50]; + char PlayerScore[50]; // Used later to display score on screen. + // Init Camera + Camera2D MainCamera; + MainCamera.target = (Vector2){0, 0}; + MainCamera.offset = (Vector2){0, 0}; + MainCamera.rotation = 0; + while(!WindowShouldClose() && GameGoing == true) { + MainCamera.zoom = GetScreenHeight()/720.0f; + MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; + MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; + + // Who won? + if (Enemy.Score >= 5) { + StopSoundMulti(); + play_audio(MUSIC_DEFEAT); + while(!IsKeyDown(KEY_SPACE)) { + BeginDrawing(); + EnableCursor(); + ClearBackground(BLACK); + DrawText("You Lose.", 0, 0, 128, RED); + DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); + EndDrawing(); + } + return; + } else if (Player.Score >= 5) { + StopSoundMulti(); + play_audio(MUSIC_VICTORY); + while(!IsKeyDown(KEY_SPACE)) { + BeginDrawing(); + EnableCursor(); + ClearBackground(BLACK); + DrawText("You Win!", 0, 0, 128, BLUE); + DrawText("Press space to return to title screen.", 0, 128, 24, WHITE); + EndDrawing(); + } + return; + } + + //Controls + if(IsKeyDown(KEY_UP)) { + Player.Y -= 10; + } else if (IsKeyDown(KEY_DOWN)) { + Player.Y += 10; + } else if(IsKeyPressed(KEY_ESCAPE)) { + EnableCursor(); + } else if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsCursorHidden() == true) { + Player.Y = GetMouseY()-PaddleSprite.height/2.0f; + DisableCursor(); + } + + if(GetMouseY() < 0) { + SetMousePosition(0, 0); + } else if(GetMouseY() > 720) { + SetMousePosition(0, 720); + } + + //Check if players are off-screen + if (Player.Y < 0) { + Player.Y = 0; + } else if (Player.Y > 480) { + Player.Y = 480; + } + + enemy(&Enemy, Ball); + + // Collision + ball(&Player.HitBox, &Enemy.HitBox, &Ball, &Player.Score, &Enemy.Score); + + //Updates hitbox with player's position. + Player.HitBox.y = Player.Y; + + //Turn Scores into strings. + snprintf(PlayerScore, 50, "Player: %d", Player.Score); + snprintf(EnemyScore, 50, "Enemy: %d", Enemy.Score); + + BeginDrawing(); + ClearBackground(BLACK); + BeginMode2D(MainCamera); + DrawTexture(PaddleSprite, 0, Player.Y, WHITE); + DrawTexture(PaddleSprite, 1200, Enemy.Y, WHITE); + DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE); + //DrawRectangleRec(Enemy.BallDetector, RED); + DrawText(PlayerScore, 0, 0, 32, GREEN); + DrawText(EnemyScore, 1130, 688, 32, RED); + EndMode2D(); + EndDrawing(); + } + return; +} |