Overview
ThePlayer class represents the controllable player character in the Space Birds game. The player can move in all directions within screen boundaries, shoot bullets, and collide with obstacles. It maintains a shooting system with cooldown mechanics and manages all active bullets.
Constructor
Creates a new player instance with the specified properties.The graphical texture of the player character
Initial horizontal position in pixels
Initial vertical position in pixels
Visual width of the player in pixels
Visual height of the player in pixels
Movement speed in pixels per frame
Example
Fields
| Field | Type | Description |
|---|---|---|
x | float | Current horizontal position |
y | float | Current vertical position |
width | float | Visual width of the player |
height | float | Visual height of the player |
speed | float | Movement speed in pixels per frame |
texture | Texture | Graphical texture for rendering |
bounds | Rectangle | Collision rectangle (smaller than visual size for fair gameplay) |
engineSound | Sound | Engine sound effect |
shootSound | Sound | Shooting sound effect |
bulletTexture | Texture | Texture used for bullets |
bullets | Array<Bullet> | Collection of all active bullets |
shootCooldown | float | Time between shots (0.3 seconds) |
shootTimer | float | Current cooldown timer |
Methods
render
Draws the player and all active bullets to the screen.The SpriteBatch where the player texture will be rendered
update
Updates the player’s logic and all active bullets. Manages cooldown timer and removes inactive bullets.Time elapsed since the last frame in seconds
shoot
Attempts to fire a new bullet if not in cooldown. Creates a bullet at the player’s position, plays shooting sound, and resets the cooldown timer.moveLeft
Moves the player left while keeping them within screen boundaries.moveRight
Moves the player right while keeping them within screen boundaries.moveUp
Moves the player up while keeping them within screen boundaries.moveDown
Moves the player down while keeping them within screen boundaries.activate
Activates the player’s engine sound. Called when the game begins.dispose
Releases all resources associated with the player, including textures, sounds, and all active bullets. Must be called when the player is no longer needed.Getters
getX
Returns the current horizontal position of the player.float - The X coordinate of the player
getY
Returns the current vertical position of the player.float - The Y coordinate of the player
getWidth
Returns the visual width of the player.float - Width in pixels
getHeight
Returns the visual height of the player.float - Height in pixels
getBounds
Returns the collision rectangle of the player. The rectangle is smaller than the visual size for fairer gameplay.Rectangle - The collision bounds
Example:
getBullets
Returns all active bullets fired by the player.Array<Bullet> - Array containing all active bullets
Example: