
BUBBLE TROUBLE β’ Flutter Game from Scratch
π¨π½βπ»π±π° Build, Launch & Monetize Course: https://buildlaunchmonetize.app/ MY APPS π₯π± Habit Tracker: https://ritualz.app π³π± Expense Tracker: https://dollatracker.app π₯·π½ FOLLOW ME Patreon: https://www.patreon.com/mitchkoko/ Instagram: https://instagram.com/createdbykoko/ Twitter: https://twitter.com/createdbykoko/ TikTok: https://www.tiktok.com/@createdbykoko/
BUBBLE TROUBLE β’ Flutter Game from Scratch
Creating a Bubble Trouble Game with Flutter
Introduction to the Game Development
- The video introduces a game development project using Flutter, specifically cloning the game "Bubble Trouble" which is typically played in web browsers.
- The presenter suggests that while the game can be adapted for mobile, it will primarily focus on creating it for Flutter Web.
Setting Up the Project
- A new Flutter project is initiated, and unnecessary code below the main function is removed to start fresh. A stateless widget is created as a foundation for the app.
- The debug banner in the app is turned off for a cleaner interface, and a home page widget is established as a stateful widget due to its dynamic nature.
Building the User Interface
- The UI begins with a column containing two expanded widgets; one pink and one gray, where their heights are adjusted using flex properties to create an uneven split (3:1 ratio).
- A new file named
button.dart
is created to encapsulate button code, promoting cleaner organization and reusability within the homepage layout. Each button has defined dimensions and displays text initially.
Enhancing Button Functionality
- Buttons are centered within their row, and icons replace text labels for better visual appeal; rounded corners are added using
ClipRRect
. This enhances aesthetics significantly.
- Object-oriented programming principles are applied by creating a constructor for buttons that allows different icons (back arrow, up arrow, forward arrow) without rewriting code multiple times. This promotes efficiency in coding practices.
Adding Interactivity
- To make buttons functional, they are wrapped in
GestureDetector
, enabling tap detection which triggers specified functions when interacted withβthis adds interactivity to previously static elements of the UI.
Game Development Basics
Setting Up the Game Environment
- The initial setup involves creating various widgets in a designated area, using a stack widget to manage multiple elements effectively.
- A blue container is created with specified dimensions (50x50), which can be positioned anywhere on the screen using alignment coordinates.
- The coordinate system for positioning uses values from -1 to 1 for both horizontal and vertical axes, allowing precise placement of game elements.
Player Positioning and Mechanics
- The player character is positioned at the bottom center of the screen, with rounded corners applied to enhance aesthetics.
- Initially, a square represents the player; later it will be replaced by an image. The focus remains on implementing movement mechanics first.
- A variable
playerX
is introduced to control the player's horizontal position dynamically instead of hardcoding values.
Implementing Movement Controls
- Methods are defined for moving left (
moveLeft
) and right (moveRight
), as well as firing missiles. These methods will respond to button taps.
- Movement adjustments are made by modifying
playerX
, where negative values move left and positive values move right, ensuring smooth gameplay dynamics.
Fine-Tuning Player Movement
- Adjustments are made to ensure that movement increments are appropriate; larger jumps could disrupt gameplay balance.
- Emphasis is placed on maintaining readability in code structure while managing player movements through method calls.
Code Organization and Object-Oriented Programming
- A new file named
player.dart
is created for better organization, encapsulating player-related logic within a stateless widget calledMyPlayer
.
- Constructors are utilized to pass variables like
playerX
into the new widget, promoting modularity in code design.
Enhancing User Interaction
- To improve user experience on desktops/laptops, keyboard controls are implemented via a
RawKeyboardListener
, allowing players to use arrow keys for movement instead of mouse clicks.
- Key events trigger corresponding methods (
moveLeft
,moveRight
) when specific keys are pressed, enhancing gameplay fluidity without relying solely on buttons.
Missile Mechanics and Game Development
Implementing Movement Constraints
- The missile can be fired, but movement to the left is restricted to prevent going off-screen. An if statement ensures that the position does not exceed negative one when moving left.
- A timer with a duration of 100 is added for firing the missile, along with creating missile variables including dimensions and color.
Adjusting Missile Behavior
- The missile's y-coordinate needs adjustment to move upwards; this requires subtracting from its current position.
- To align with gameplay mechanics similar to "Bubble Trouble," the missile should extend upwards rather than just changing its y-coordinate.
Positioning and Firing Logic
- The missile's x-coordinate must match the player's x-position upon firing, ensuring it starts from the correct location.
- A condition is set to stop the missile once it reaches the top of the screen by canceling its timer and resetting its state.
Encapsulation and Code Organization
- The code related to missiles is encapsulated into a new file called
missile.dart
, creating a stateless widget namedMyMissile
for better organization.
- A boolean variable
midShot
determines whether a shot can be fired based on whether it's currently in progress, preventing movement tracking during firing.
Ball Creation and Interaction
- New variables for ball coordinates are created, leading to another file named
ball.dart
where ball behavior will be coded.
- A play button is introduced at the bottom of the interface, which initiates game actions when pressed.
Game Dynamics: Directional Movement
Establishing Directionality
- An enum called
Direction
is created to manage ball movement options within the game environment.
- The initial direction of movement for the ball is set to left. Conditions are established to reverse direction upon hitting walls.
Collision Detection Mechanism
Understanding Height to Coordinate Conversion
Converting Height into Coordinates
- The process involves converting a height into a coordinate, specifically an ordinate on the y-axis. The range is from negative one at the top to positive one at the bottom, with zero in the middle.
Calculating Proportions
- To find the ball's position relative to total height, use a media query. The top part of the screen is set to three-quarters of its total height for calculations.
Deriving Position Formula
- The formula for position requires calculating the proportion of height over total height and adjusting it by subtracting from positive one. This adjustment accounts for the negative-to-positive range.
Testing Formula Validity
- By substituting values (e.g., ball in the middle), we can verify that when height proportion equals half, it results in a position of zero, confirming our formula's accuracy.
Implementing Code Changes
- The derived equation will be implemented in code with comments explaining its function: taking height as input and outputting a corresponding position.
Game Mechanics: Missile and Ball Interaction
Checking Collision Conditions
- When checking if a missile hits a ball, it's crucial to ensure that any part of the missile can trigger this interactionβnot just its tip.
Handling Floating Point Precision
- Instead of directly comparing x-coordinates due to potential rounding issues with doubles, calculate their absolute difference and check if it's below an acceptable margin (e.g., 0.3).
Resetting Game Elements
- After detecting collisions or interactions, reset relevant game elements like missiles while ensuring smooth gameplay mechanics are maintained.
Enhancing Ball Movement Dynamics
Introducing Bounce Mechanics
- Transitioning from simple left-right movement to implementing bounce dynamics requires new variables such as time and height based on quadratic equations used in classic games.
Adjusting Jump Physics
- To prevent unrealistic behavior where the ball drops past ground level, implement checks that reset jumps when reaching ground level using defined conditions related to height.
Refining Player Interaction Logic
Updating Velocity Variables
- Introduce separate velocity variables for more control over jump dynamics; adjust parameters for smoother gameplay experience while maintaining realistic physics.
Checking Player Status
Bubble Trouble Game Development
Implementing Game Mechanics
- The game logic includes a function that returns true or false, cancels the timer, and prints "dead" when certain conditions are met.
- Instead of merely printing to the console, a dialogue screen is introduced to enhance user experience. The message displayed is "you dead bro," which adds a humorous touch.
- The tutorial emphasizes decorating the dialogue screen for better aesthetics, indicating that visual appeal is important in game design.
- Future enhancements suggested include adding a restart button on the dialogue screen and replacing static elements (like a purple box) with images to improve engagement.