From 8d19dc629bb1638990f7f9268880d7a139678a8e Mon Sep 17 00:00:00 2001 From: Charadon Date: Thu, 2 Jun 2022 13:35:28 -0400 Subject: Added music and almost done marathon mode. --- src/ball.c | 42 +++++++++++++++++---------- src/main.c | 14 ++++++++- src/marathon.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/pong.h | 3 ++ src/sounds.h | 4 ++- src/title.c | 6 +++- src/versus.c | 9 +++++- 7 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 src/marathon.c (limited to 'src') diff --git a/src/ball.c b/src/ball.c index 95cc0c5..4fc1eb4 100644 --- a/src/ball.c +++ b/src/ball.c @@ -3,6 +3,11 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerScore, int *EnemyScore) { + bool NoEnemy = false; + if (Enemy == NULL) { + NoEnemy = true; + } + // Moves ball Ball->Y += Ball->Angle; if (Ball->Direction == LEFT) { @@ -28,18 +33,20 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc } play_audio(0); } - if (CheckCollisionRecs(*Enemy, Ball->HitBox) && Ball->Direction == RIGHT) { - Ball->Direction = LEFT; - Ball->Speed *= 1.5f; - if (Ball->Speed > 40) { - Ball->Speed = 40; - } - if (Ball->Speed != 40) { - Ball->Angle = GetRandomValue(-10, 10); - } else { - Ball->Angle = GetRandomValue(-30, 30); - } - play_audio(SOUND_BOUNCE); + if (NoEnemy == false) { + if (CheckCollisionRecs(*Enemy, Ball->HitBox) && Ball->Direction == RIGHT) { + Ball->Direction = LEFT; + Ball->Speed *= 1.5f; + if (Ball->Speed > 40) { + Ball->Speed = 40; + } + if (Ball->Speed != 40) { + Ball->Angle = GetRandomValue(-10, 10); + } else { + Ball->Angle = GetRandomValue(-30, 30); + } + play_audio(SOUND_BOUNCE); + } } // Bounce ball if touches top or bottom of screen. @@ -50,13 +57,18 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc // Calculates score and resets ball. bool Scored = false; - if (Ball->X < 0) { + if (Ball->X < 0 && NoEnemy == false) { *EnemyScore += 1; Scored = true; - } else if (Ball ->X > 1280) { + } else if (Ball ->X > 1280 && NoEnemy == false) { *PlayerScore += 1; Scored = true; - } + } else if (Ball->X > (1280-32) && NoEnemy == true) { + *PlayerScore += 1; + Scored = false; + Ball->Direction = LEFT; + play_audio(SOUND_PLAYER_SCORE); + } if (Scored == true) { Ball->X = 1280/2.0f; Ball->Y = 720/2.0f; diff --git a/src/main.c b/src/main.c index 11c69b7..5f733f5 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,8 @@ int audio() { Sound TitleScreen = LoadSound("resources/title.wav"); Sound Victory = LoadSound("resources/victory.wav"); Sound Defeat = LoadSound("resources/defeat.wav"); + Sound PlayerScore = LoadSound("resources/score_player.wav"); + Sound EnemyScore = LoadSound("resources/score_enemy.wav"); const struct timespec Delay = { 0, 20000000 }; @@ -52,6 +54,12 @@ int audio() { case 3: //Title Screen PlaySoundMulti(TitleScreen); break; + case 4: //Player Score + PlaySoundMulti(PlayerScore); + break; + case 5: //Enemy Score + PlaySoundMulti(EnemyScore); + break; case 99: //Stop All Sounds StopSoundMulti(); break; @@ -117,12 +125,16 @@ int main() { play_audio(STOP_ALL_SOUNDS); versus_main(); break; + case 1: //Marathon + play_audio(STOP_ALL_SOUNDS); + marathon_main(); + break; default: break; } } - GameGoing = false; + GameGoing = false; // Make sure the game is going to end. thrd_join(AudioThread, NULL); CloseWindow(); return(0); diff --git a/src/marathon.c b/src/marathon.c new file mode 100644 index 0000000..b12b64a --- /dev/null +++ b/src/marathon.c @@ -0,0 +1,89 @@ +#include "pong.h" +#include "raylib.h" + +void marathon_main() { + + // Init Music + Music Background = LoadMusicStream("resources/marathon.wav"); + Background.looping = true; + PlayMusicStream(Background); + + // Init balls lmao + struct Balls Ball; + Ball.X = 1280/2.0f; + Ball.Y = 720/2.0f; + Ball.Direction = LEFT; + Ball.Speed = 3.0f; + Ball.Angle = 0.0f; + + // Init Player + struct Players Player; + Player.Y = 0; + Player.Direction = 0; + Player.Score = 0; + + // Init sprites + Texture2D PaddleSprite = LoadTexture("resources/paddle.png"); + Texture2D BallSprite = LoadTexture("resources/ball.png"); + + char PlayerScore[50]; // Used later to display score on screen. + + // Set Collision Boxes + Player.HitBox = (Rectangle){80, Player.Y, 5, PaddleSprite.height}; + Ball.HitBox = (Rectangle){Ball.X, Ball.Y, BallSprite.width, BallSprite.height}; + + // Init Camera + Camera2D MainCamera; + MainCamera.target = (Vector2){0, 0}; + MainCamera.offset = (Vector2){0, 0}; + MainCamera.rotation = 0; + + while(!WindowShouldClose() && GameGoing == true) { + UpdateMusicStream(Background); + snprintf(PlayerScore, 50, "Player: %d", Player.Score); + MainCamera.zoom = GetScreenHeight()/720.0f; + MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; + MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; + + //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; + } + + // Collision + ball(&Player.HitBox, NULL, &Ball, &Player.Score, NULL); + //Updates hitbox with player's position. + Player.HitBox.y = Player.Y; + + BeginDrawing(); + ClearBackground(BLACK); + BeginMode2D(MainCamera); + DrawTexture(PaddleSprite, 0, Player.Y, WHITE); + DrawTexture(BallSprite, Ball.X, Ball.Y, WHITE); + DrawText(PlayerScore, 0, 0, 32, BLUE); + EndMode2D(); + EndDrawing(); + } + EnableCursor(); + return; +} diff --git a/src/pong.h b/src/pong.h index 26ec9c2..8fb51c5 100644 --- a/src/pong.h +++ b/src/pong.h @@ -40,6 +40,7 @@ void ball(Rectangle *Player, Rectangle *Enemy, struct Balls *Ball, int *PlayerSc bool play_audio(int SoundEffect); int title_screen(); void versus_main(); +void marathon_main(); //Sounds extern const int SOUND_BOUNCE; @@ -47,5 +48,7 @@ extern const int MUSIC_DEFEAT; extern const int MUSIC_VICTORY; extern const int MUSIC_TITLE; extern const int STOP_ALL_SOUNDS; +extern const int SOUND_PLAYER_SCORE; +extern const int SOUND_ENEMY_SCORE; #endif diff --git a/src/sounds.h b/src/sounds.h index 0418c9c..5a87cb1 100644 --- a/src/sounds.h +++ b/src/sounds.h @@ -1,10 +1,12 @@ #ifndef SOUNDS_H #define SOUNDS_H - +#include "pong.h" const int SOUND_BOUNCE = 0; const int MUSIC_DEFEAT = 1; const int MUSIC_VICTORY = 2; const int MUSIC_TITLE = 3; +const int SOUND_PLAYER_SCORE = 4; +const int SOUND_ENEMY_SCORE = 5; const int STOP_ALL_SOUNDS = 99; #endif diff --git a/src/title.c b/src/title.c index 56e382c..c73fb10 100644 --- a/src/title.c +++ b/src/title.c @@ -1,4 +1,5 @@ #include "pong.h" +#include "raylib.h" void help_text() { @@ -39,10 +40,13 @@ int title_screen() { if (CheckCollisionRecs(Mouse, Versus)) { Selected = &Versus; if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - return 0; + return(0); } } else if (CheckCollisionRecs(Mouse, Marathon)) { Selected = &Marathon; + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { + return(1); + } } else if (CheckCollisionRecs(Mouse, Settings)) { Selected = &Settings; } else if (CheckCollisionRecs(Mouse, Help)) { diff --git a/src/versus.c b/src/versus.c index a5c1060..ed933e5 100644 --- a/src/versus.c +++ b/src/versus.c @@ -1,4 +1,5 @@ #include "pong.h" +#include "raylib.h" void versus_main() { // Init Player Variables @@ -19,6 +20,11 @@ void versus_main() { Ball.Direction = LEFT; Ball.Speed = 3.0f; Ball.Angle = 0.0f; + + // Init music + Music Background = LoadMusicStream("resources/versus.wav"); + Background.looping = true; + PlayMusicStream(Background); // Set Sprites Texture2D PaddleSprite = LoadTexture("resources/paddle.png"); @@ -31,12 +37,13 @@ void versus_main() { 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 + // Init Camera Camera2D MainCamera; MainCamera.target = (Vector2){0, 0}; MainCamera.offset = (Vector2){0, 0}; MainCamera.rotation = 0; while(!WindowShouldClose() && GameGoing == true) { + UpdateMusicStream(Background); MainCamera.zoom = GetScreenHeight()/720.0f; MainCamera.offset = (Vector2){GetScreenWidth()/2.0f, GetScreenHeight()/2.0f}; MainCamera.target = (Vector2){1280/2.0f, 720/2.0f}; -- cgit 1.4.1-2-gfad0