From 720e9ebb1957cdf74b3b1b96caf595a89eac6fc7 Mon Sep 17 00:00:00 2001 From: yannickreiss Date: Sun, 12 Jan 2025 07:54:05 +0100 Subject: [PATCH] Removing warnings --- alire.toml | 4 +- balloon_bounce.gpr | 22 ++++ default.gpr | 22 ++++ src/ballon_bounce.adb | 243 -------------------------------------- src/balloon_bounce.adb | 248 +++++++++++++++++++++++++++++++++++++++ src/random_functions.adb | 72 ++++++------ src/random_functions.ads | 7 +- 7 files changed, 332 insertions(+), 286 deletions(-) create mode 100644 balloon_bounce.gpr create mode 100644 default.gpr delete mode 100644 src/ballon_bounce.adb create mode 100644 src/balloon_bounce.adb diff --git a/alire.toml b/alire.toml index fcc0c94..a7a71f4 100644 --- a/alire.toml +++ b/alire.toml @@ -1,4 +1,4 @@ -name = "ballon_bounce" +name = "balloon_bounce" description = "A little game like pong" version = "0.1.0-dev" @@ -9,7 +9,7 @@ licenses = "MIT" website = "" tags = ["game"] -executables = ["ballon_bounce"] +executables = ["balloon_bounce"] [[depends-on]] asfml = "^2.6.1" diff --git a/balloon_bounce.gpr b/balloon_bounce.gpr new file mode 100644 index 0000000..1573387 --- /dev/null +++ b/balloon_bounce.gpr @@ -0,0 +1,22 @@ +with "config/balloon_bounce_config.gpr"; +project Balloon_Bounce is + + for Source_Dirs use ("src/", "config/"); + for Object_Dir use "obj/" & Balloon_Bounce_Config.Build_Profile; + for Create_Missing_Dirs use "True"; + for Exec_Dir use "bin"; + for Main use ("balloon_bounce.adb"); + + package Compiler is + for Default_Switches ("Ada") use Balloon_Bounce_Config.Ada_Compiler_Switches; + end Compiler; + + package Binder is + for Switches ("Ada") use ("-Es"); -- Symbolic traceback + end Binder; + + package Install is + for Artifacts (".") use ("share"); + end Install; + +end Balloon_Bounce; diff --git a/default.gpr b/default.gpr new file mode 100644 index 0000000..1573387 --- /dev/null +++ b/default.gpr @@ -0,0 +1,22 @@ +with "config/balloon_bounce_config.gpr"; +project Balloon_Bounce is + + for Source_Dirs use ("src/", "config/"); + for Object_Dir use "obj/" & Balloon_Bounce_Config.Build_Profile; + for Create_Missing_Dirs use "True"; + for Exec_Dir use "bin"; + for Main use ("balloon_bounce.adb"); + + package Compiler is + for Default_Switches ("Ada") use Balloon_Bounce_Config.Ada_Compiler_Switches; + end Compiler; + + package Binder is + for Switches ("Ada") use ("-Es"); -- Symbolic traceback + end Binder; + + package Install is + for Artifacts (".") use ("share"); + end Install; + +end Balloon_Bounce; diff --git a/src/ballon_bounce.adb b/src/ballon_bounce.adb deleted file mode 100644 index 3bf1717..0000000 --- a/src/ballon_bounce.adb +++ /dev/null @@ -1,243 +0,0 @@ -with Sf; -with Sf.Graphics.RenderWindow; use Sf.Graphics.RenderWindow; -with Sf.Graphics.Color; use Sf.Graphics.Color; -with Sf.Graphics.Texture; use Sf.Graphics.Texture; -with Sf.Graphics.Sprite; use Sf.Graphics.Sprite; - -with Sf.Window.Event; use Sf.Window.Event; -with Sf.Window.Keyboard; use Sf.Window.Keyboard; - -with Sf.System.Vector2; use Sf.System.Vector2; -with Sf.System.Clock; use Sf.System.Clock; -with Sf.System.Time; use Sf.System.Time; - -use Sf.Graphics; -use Sf.Window; -use Sf.System; -use Sf; - -with Ada.Text_IO; use Ada.Text_IO; -with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; -with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random; -with Ada.Numerics.Discrete_Random; - -with Random_Functions; use Random_Functions; - -procedure Ballon_Bounce is - - -- Window structure - Width : constant sfUint32 := 1_000; - Height : constant sfUint32 := 750; - Width_Float : constant Float := 1_000.0; - Height_Float : constant Float := 750.0; - Main_Window : sfRenderWindow_Ptr := - RenderWindow.create ((Width, Height, 32), "Balloon Bouncer!"); - Event_Hook : Event.sfEvent; - - -- Setup the background - Background_Texture : sfTexture_Ptr := - Texture.createFromFile ("img/background.jpg"); - Background : sfSprite_Ptr := Sprite.create; - - -- Set platform - Platform_Texture : sfTexture_Ptr := - Texture.createFromFile ("img/pipe.png"); - Platform : sfSprite_Ptr := Sprite.create; - Platform_Width : constant Float := 250.0; - Platform_Height : constant Float := 50.0; - - -- Set Ball - type Vector2 is record - X : Float; - Y : Float; - end record; - Ball_Texture : sfTexture_Ptr := Texture.createFromFile ("img/ball.png"); - Ball : sfSprite_Ptr := Sprite.create; - Ball_Size : constant Float := 93.0; - - -- Set easy enemies - Enemy_1_Texture : sfTexture_Ptr := Texture.createFromFile ("img/UFO.png"); - Enemy_1 : sfSprite_Ptr := Sprite.create; - Enemy_1_Width : constant Float := 186.0; - Enemy_1_Height : constant Float := 77.0; - Enemy_1_Position : Vector2 := (-100.0, -100.0); - - -- Game Coordinates - Platform_X : Float := (Width_Float / 2.0) - (Platform_Width / 2.0); - Platform_Y : Float := Height_Float - Platform_Height; - Platform_Speed : Float := 8.0; - Ball_X : Float := (Width_Float / 2.0) - (Ball_Size / 2.0); - Ball_Y : Float := (Height_Float / 2.0) - (Ball_Size / 2.0); - Ball_Direction : Vector2 := (0.0, 1.0); - Ball_Speed : Float := 5.0; - Player_Score : Natural := 0; - - -- Random number generator - - function Rotate_Vector - (Crossing_Place : Float; Direction : Vector2) return Vector2 - is - Result : Vector2; - Angle : constant Float := Crossing_Place / 75.0; - Length : Float; - begin - - Result.X := Angle; - Result.Y := Direction.Y; - - Length := Sqrt ((Result.X * Result.X) + (Result.Y * Result.Y)); - - -- Norming the vector to prevent speed changes - while (Length > 1.2) or (Length < 0.8) loop - Result.X := Result.X / Length; - Result.Y := Result.Y / Length; - Length := Sqrt ((Result.X * Result.X) + (Result.Y * Result.Y)); - end loop; - - -- Prevent glitching down - if Result.Y >= -1.0 then - Result.Y := Result.Y - 1.0; - end if; - - return Result; - end Rotate_Vector; - -begin - RenderWindow.setFramerateLimit (Main_Window, 30); - - -- Background and position initialization - Sprite.setTexture (Background, Background_Texture); - Sprite.setPosition (Background, (Float (0), Float (0))); - - -- Interactive element texture binding - Sprite.setTexture (Platform, Platform_Texture); - Sprite.setTexture (Ball, Ball_Texture); - Sprite.setTexture (Enemy_1, Enemy_1_Texture); - - while RenderWindow.isOpen (Main_Window) = sfTrue loop - while RenderWindow.PollEvent (Main_Window, event => Event_Hook) = - sfTrue - loop - case Event_Hook.eventType is - when Event.sfEvtClosed => - RenderWindow.Close (Main_Window); - when Event.sfEvtKeyPressed => - case Event_Hook.key.code is - when Keyboard.sfKeyEscape => - RenderWindow.Close (Main_Window); - when Keyboard.sfKeyA => - if Platform_X >= 0.0 then - Platform_X := Platform_X - Platform_Speed; - end if; - when Keyboard.sfKeyD => - if Platform_X <= (Width_Float - Platform_Width) - then - Platform_X := Platform_X + Platform_Speed; - end if; - when others => - null; - end case; - when others => - null; - end case; - end loop; - - -- BEGIN: Computing area - -- BEGIN: Ball collision detection - Ball_X := Ball_X + (Ball_Direction.X * Ball_Speed); - Ball_Y := Ball_Y + (Ball_Direction.Y * Ball_Speed); - - if (Ball_X <= 0.0) or ((Ball_X + Ball_Size) >= Width_Float) then - Ball_Direction.X := Ball_Direction.X * (-1.0); - end if; - - if Ball_Y <= 0.0 then - Ball_Direction.Y := Ball_Direction.Y * (-1.0); - end if; - - -- Ball below platform! Possibly Lost! - if ((Ball_Y + Ball_Size) >= Platform_Y) then - - -- Check if platform was hit, or game is lost - if ((Ball_X + (Ball_Size / 2.0)) >= Platform_X) and - ((Ball_X + (Ball_Size / 2.0)) <= (Platform_X + Platform_Width)) - then - Ball_Direction.Y := Ball_Direction.Y * (-1.0); - - -- Change direction - Ball_Direction := - Rotate_Vector - ((Ball_X + (Ball_Size / 2.0)) - - (Platform_X + (Platform_Width / 2.0)), - Ball_Direction); - - -- The Ball seems to rebounce randomly, so we move it away quicker - Ball_Y := Ball_Y - (Ball_Speed * 1.5); - - -- Speed up the ball - Ball_Speed := Ball_Speed + 0.1; - - -- Check if no enemy is present, if so, maybe spawn one - if Enemy_1_Position.X = -100.0 then - if Spawn_Enemy then - Enemy_1_Position.X := - Generate_Coordinates (Width_Float - Enemy_1_Width); - Enemy_1_Position.Y := - Generate_Coordinates - ((Height_Float * 0.8) - Enemy_1_Height); - end if; - end if; - else - -- Game is Lost, reset for now - Ball_X := Width_Float / 2.0; - Ball_Y := Height_Float / 2.0; - Ball_Direction := (0.0, 1.0); - end if; - end if; - -- END: Ball collision detection - - -- BEGIN: Enemy collision detection - - -- Check vertical collision - if ((Ball_Y + Ball_Size) >= Enemy_1_Position.Y) and - (Ball_Y <= (Enemy_1_Position.Y + Enemy_1_Height)) - then - -- Check horizontal collision - if ((Ball_X + Ball_Size) >= Enemy_1_Position.X) and - (Ball_X <= (Enemy_1_Position.X + Enemy_1_Width)) - then - -- Detected collision - Player_Score := Player_Score + 5; - Enemy_1_Position.X := -100.0; - Enemy_1_Position.Y := -100.0; - Put_Line (Player_Score'Image); - end if; - end if; - -- END: Enemy collision detection - - -- END: Computing area - - -- BEGIN: Update states - Sprite.setPosition (Platform, (Platform_X, Platform_Y)); - Sprite.setPosition (Ball, (Ball_X, Ball_y)); - Sprite.setPosition (Enemy_1, (Enemy_1_Position.X, Enemy_1_Position.Y)); - - -- END: Update states - - RenderWindow.clear (Main_Window, Color.sfWhite); - - -- BEGIN: Draw area - RenderWindow.drawSprite (Main_Window, Background); - RenderWindow.drawSprite (Main_Window, Platform); - RenderWindow.drawSprite (Main_Window, Enemy_1); - - -- Always on Top - RenderWindow.drawSprite (Main_Window, Ball); - - -- END: Draw area - - RenderWindow.display (Main_Window); - - end loop; - -end Ballon_Bounce; diff --git a/src/balloon_bounce.adb b/src/balloon_bounce.adb new file mode 100644 index 0000000..39b8ca1 --- /dev/null +++ b/src/balloon_bounce.adb @@ -0,0 +1,248 @@ +with Sf; +with Sf.Graphics.RenderWindow; use Sf.Graphics.RenderWindow; +with Sf.Graphics.Color; use Sf.Graphics.Color; +with Sf.Graphics.Texture; use Sf.Graphics.Texture; +with Sf.Graphics.Sprite; use Sf.Graphics.Sprite; +with Sf.Graphics.Text; use Sf.Graphics.Text; + +with Sf.Window.Event; use Sf.Window.Event; +with Sf.Window.Keyboard; use Sf.Window.Keyboard; + +with Sf.System.Vector2; use Sf.System.Vector2; +with Sf.System.Clock; use Sf.System.Clock; +with Sf.System.Time; use Sf.System.Time; + +use Sf.Graphics; +use Sf.Window; +use Sf.System; +use Sf; + +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions; + +with Random_Functions; use Random_Functions; + +procedure Balloon_Bounce is + + -- Window structure + Width : constant sfUint32 := 1_000; + Height : constant sfUint32 := 750; + Width_Float : constant Float := 1_000.0; + Height_Float : constant Float := 750.0; + Main_Window : sfRenderWindow_Ptr := + RenderWindow.create ((Width, Height, 32), "Balloon Bouncer!"); + Event_Hook : Event.sfEvent; + + -- Setup the background + Background_Texture : sfTexture_Ptr := + Texture.createFromFile ("img/background.jpg"); + Background : sfSprite_Ptr := Sprite.create; + + -- Set platform + Platform_Texture : sfTexture_Ptr := Texture.createFromFile ("img/pipe.png"); + Platform : sfSprite_Ptr := Sprite.create; + Platform_Width : constant Float := 250.0; + Platform_Height : constant Float := 50.0; + + -- Set Ball + type Vector2 is record + X : Float; + Y : Float; + end record; + Ball_Texture : sfTexture_Ptr := Texture.createFromFile ("img/ball.png"); + Ball : sfSprite_Ptr := Sprite.create; + Ball_Size : constant Float := 93.0; + + -- Set easy enemies + Enemy_1_Texture : sfTexture_Ptr := Texture.createFromFile ("img/UFO.png"); + Enemy_1 : sfSprite_Ptr := Sprite.create; + Enemy_1_Width : constant Float := 186.0; + Enemy_1_Height : constant Float := 77.0; + Enemy_1_Position : Vector2 := (-100.0, -100.0); + + -- Game Coordinates + Platform_X : Float := (Width_Float / 2.0) - (Platform_Width / 2.0); + Platform_Y : Float := Height_Float - Platform_Height; + Platform_Speed : Float := 8.0; + Ball_X : Float := (Width_Float / 2.0) - (Ball_Size / 2.0); + Ball_Y : Float := (Height_Float / 2.0) - (Ball_Size / 2.0); + Ball_Direction : Vector2 := (0.0, 1.0); + Ball_Speed : Float := 5.0; + + -- Score + Player_Score : Natural := 0; + Player_Score_Text : sfText_Ptr := Text.create; + + -- Random number generator + + function Rotate_Vector + (Crossing_Place : Float; Direction : Vector2) return Vector2 + is + Result : Vector2; + Angle : constant Float := Crossing_Place / 75.0; + Length : Float; + begin + + Result.X := Angle; + Result.Y := Direction.Y; + + Length := Sqrt ((Result.X * Result.X) + (Result.Y * Result.Y)); + + -- Norming the vector to prevent speed changes + while (Length > 1.2) or (Length < 0.8) loop + Result.X := Result.X / Length; + Result.Y := Result.Y / Length; + Length := Sqrt ((Result.X * Result.X) + (Result.Y * Result.Y)); + end loop; + + -- Prevent glitching down + if Result.Y >= -1.0 then + Result.Y := Result.Y - 1.0; + end if; + + return Result; + end Rotate_Vector; + +begin + RenderWindow.setFramerateLimit (Main_Window, 30); + + -- Background and position initialization + Sprite.setTexture (Background, Background_Texture); + Sprite.setPosition (Background, (Float (0), Float (0))); + Text.setPosition (Player_Score_Text, (Float (0), Float (0))); + + -- Interactive element texture binding + Sprite.setTexture (Platform, Platform_Texture); + Sprite.setTexture (Ball, Ball_Texture); + Sprite.setTexture (Enemy_1, Enemy_1_Texture); + + while RenderWindow.isOpen (Main_Window) = sfTrue loop + while RenderWindow.PollEvent (Main_Window, event => Event_Hook) = sfTrue + loop + case Event_Hook.eventType is + when Event.sfEvtClosed => + RenderWindow.Close (Main_Window); + when Event.sfEvtKeyPressed => + case Event_Hook.key.code is + when Keyboard.sfKeyEscape => + RenderWindow.Close (Main_Window); + when Keyboard.sfKeyA => + if Platform_X >= 0.0 then + Platform_X := Platform_X - Platform_Speed; + end if; + when Keyboard.sfKeyD => + if Platform_X <= (Width_Float - Platform_Width) then + Platform_X := Platform_X + Platform_Speed; + end if; + when others => + null; + end case; + when others => + null; + end case; + end loop; + + -- BEGIN: Computing area + -- BEGIN: Ball collision detection + Ball_X := Ball_X + (Ball_Direction.X * Ball_Speed); + Ball_Y := Ball_Y + (Ball_Direction.Y * Ball_Speed); + + if (Ball_X <= 0.0) or ((Ball_X + Ball_Size) >= Width_Float) then + Ball_Direction.X := Ball_Direction.X * (-1.0); + end if; + + if Ball_Y <= 0.0 then + Ball_Direction.Y := Ball_Direction.Y * (-1.0); + end if; + + -- Ball below platform! Possibly Lost! + if ((Ball_Y + Ball_Size) >= Platform_Y) then + + -- Check if platform was hit, or game is lost + if ((Ball_X + (Ball_Size / 2.0)) >= Platform_X) and + ((Ball_X + (Ball_Size / 2.0)) <= (Platform_X + Platform_Width)) + then + Ball_Direction.Y := Ball_Direction.Y * (-1.0); + + -- Change direction + Ball_Direction := + Rotate_Vector + ((Ball_X + (Ball_Size / 2.0)) - + (Platform_X + (Platform_Width / 2.0)), + Ball_Direction); + + -- The Ball seems to rebounce randomly, so we move it away quicker + Ball_Y := Ball_Y - (Ball_Speed * 1.5); + + -- Speed up the ball + Ball_Speed := Ball_Speed + 0.1; + Player_Score := Player_Score + 1; + + -- Check if no enemy is present, if so, maybe spawn one + if Enemy_1_Position.X = -100.0 then + if Spawn_Enemy then + Enemy_1_Position.X := + Generate_Coordinates (Width_Float - Enemy_1_Width); + Enemy_1_Position.Y := + Generate_Coordinates + ((Height_Float * 0.8) - Enemy_1_Height); + end if; + end if; + else + -- Game is Lost, reset for now + Ball_Speed := 5.0; + Player_Score := 0; + Ball_X := Width_Float / 2.0; + Ball_Y := Height_Float / 2.0; + Ball_Direction := (0.0, 1.0); + end if; + end if; + -- END: Ball collision detection + + -- BEGIN: Enemy collision detection + + -- Check vertical collision + if ((Ball_Y + Ball_Size) >= Enemy_1_Position.Y) and + (Ball_Y <= (Enemy_1_Position.Y + Enemy_1_Height)) + then + -- Check horizontal collision + if ((Ball_X + Ball_Size) >= Enemy_1_Position.X) and + (Ball_X <= (Enemy_1_Position.X + Enemy_1_Width)) + then + -- Detected collision + Player_Score := Player_Score + 5; + Enemy_1_Position.X := -100.0; + Enemy_1_Position.Y := -100.0; + Put_Line (Player_Score'Image); + end if; + end if; + -- END: Enemy collision detection + + -- END: Computing area + + -- BEGIN: Update states + Sprite.setPosition (Platform, (Platform_X, Platform_Y)); + Sprite.setPosition (Ball, (Ball_X, Ball_y)); + Sprite.setPosition (Enemy_1, (Enemy_1_Position.X, Enemy_1_Position.Y)); + Text.setString (Player_score_Text, "SCORE: " & Player_Score'Image); + + -- END: Update states + + RenderWindow.clear (Main_Window, Color.sfWhite); + + -- BEGIN: Draw area + RenderWindow.drawSprite (Main_Window, Background); + RenderWindow.drawSprite (Main_Window, Platform); + RenderWindow.drawSprite (Main_Window, Enemy_1); + + -- Always on Top + RenderWindow.drawSprite (Main_Window, Ball); + RenderWindow.drawText (Main_Window, Player_score_Text); + + -- END: Draw area + + RenderWindow.display (Main_Window); + + end loop; + +end Balloon_Bounce; diff --git a/src/random_functions.adb b/src/random_functions.adb index 812a1f5..8cb44ec 100644 --- a/src/random_functions.adb +++ b/src/random_functions.adb @@ -1,38 +1,38 @@ -package body Random_Functions is - function Spawn_Enemy return Boolean is - subtype Die is Natural range 1 .. 6; - subtype Dice is Natural range 2 * Die'First .. 2 * Die'Last; - package Random_Die is new Ada.Numerics.Discrete_Random (Die); - use Random_Die; - G : Generator; - D : Dice; - Random_Number : Natural; - begin - Reset (G); - Random_Number := Random (G); - if Random_Number = 2 then - return True; - else - return False; - end if; - end Spawn_Enemy; +with Ada.Numerics.Discrete_Random; - function Generate_Coordinates (Limit : Float) return Float is - subtype Coordinate is Integer range 1 .. Integer (Limit); - subtype Coordinates is - Integer range 2 * Coordinate'First .. 2 * Coordinate'Last; - package Random_Coordinate is new Ada.Numerics.Discrete_Random - (Coordinate); - use Random_Coordinate; - G : Generator; - D : Coordinates; - Random_Number : Float; - begin - Reset (G); - Random_Number := Float (Random (G)); - if Random_Number < 0.0 then - Random_Number := Random_Number * (-1.0); - end if; - return Random_Number; - end Generate_Coordinates; +package body Random_Functions is + function Spawn_Enemy return Boolean is + subtype Die is Natural range 1 .. 3; + subtype Dice is Natural range 2 * Die'First .. 2 * Die'Last; + package Random_Die is new Ada.Numerics.Discrete_Random (Die); + use Random_Die; + G : Generator; + Random_Number : Natural; + begin + Reset (G); + Random_Number := Random (G); + if Random_Number = 2 then + return True; + else + return False; + end if; + end Spawn_Enemy; + + function Generate_Coordinates (Limit : Float) return Float is + subtype Coordinate is Integer range 1 .. Integer (Limit); + subtype Coordinates is + Integer range 2 * Coordinate'First .. 2 * Coordinate'Last; + package Random_Coordinate is new Ada.Numerics.Discrete_Random + (Coordinate); + use Random_Coordinate; + G : Generator; + Random_Number : Float; + begin + Reset (G); + Random_Number := Float (Random (G)); + if Random_Number < 0.0 then + Random_Number := Random_Number * (-1.0); + end if; + return Random_Number; + end Generate_Coordinates; end Random_Functions; diff --git a/src/random_functions.ads b/src/random_functions.ads index a315564..29d616c 100644 --- a/src/random_functions.ads +++ b/src/random_functions.ads @@ -1,7 +1,4 @@ -with Ada.Numerics.Discrete_Random; -with Ada.Numerics.Float_Random; - package Random_Functions is - function Spawn_Enemy return Boolean; - function Generate_Coordinates (Limit : Float) return Float; + function Spawn_Enemy return Boolean; + function Generate_Coordinates (Limit : Float) return Float; end Random_Functions;