Skip to main content

Overview

The Player 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.
public Player(Texture texture, float x, float y, float width, float height, float speed)
texture
Texture
required
The graphical texture of the player character
x
float
required
Initial horizontal position in pixels
y
float
required
Initial vertical position in pixels
width
float
required
Visual width of the player in pixels
height
float
required
Visual height of the player in pixels
speed
float
required
Movement speed in pixels per frame

Example

Texture playerTexture = new Texture("images/player.png");
Player player = new Player(playerTexture, 400, 100, 64, 64, 5.0f);

Fields

FieldTypeDescription
xfloatCurrent horizontal position
yfloatCurrent vertical position
widthfloatVisual width of the player
heightfloatVisual height of the player
speedfloatMovement speed in pixels per frame
textureTextureGraphical texture for rendering
boundsRectangleCollision rectangle (smaller than visual size for fair gameplay)
engineSoundSoundEngine sound effect
shootSoundSoundShooting sound effect
bulletTextureTextureTexture used for bullets
bulletsArray<Bullet>Collection of all active bullets
shootCooldownfloatTime between shots (0.3 seconds)
shootTimerfloatCurrent cooldown timer

Methods

render

Draws the player and all active bullets to the screen.
public void render(SpriteBatch batch)
batch
SpriteBatch
required
The SpriteBatch where the player texture will be rendered
Example:
player.render(batch);

update

Updates the player’s logic and all active bullets. Manages cooldown timer and removes inactive bullets.
public void update(float deltaTime)
deltaTime
float
required
Time elapsed since the last frame in seconds
Example:
player.update(Gdx.graphics.getDeltaTime());

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.
public void shoot()
Example:
if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
    player.shoot();
}

moveLeft

Moves the player left while keeping them within screen boundaries.
public void moveLeft()
Example:
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
    player.moveLeft();
}

moveRight

Moves the player right while keeping them within screen boundaries.
public void moveRight()
Example:
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
    player.moveRight();
}

moveUp

Moves the player up while keeping them within screen boundaries.
public void moveUp()
Example:
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
    player.moveUp();
}

moveDown

Moves the player down while keeping them within screen boundaries.
public void moveDown()
Example:
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
    player.moveDown();
}

activate

Activates the player’s engine sound. Called when the game begins.
public void activate()
Example:
player.activate();

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.
public void dispose()
Example:
@Override
public void dispose() {
    player.dispose();
}

Getters

getX

Returns the current horizontal position of the player.
public float getX()
Returns: float - The X coordinate of the player

getY

Returns the current vertical position of the player.
public float getY()
Returns: float - The Y coordinate of the player

getWidth

Returns the visual width of the player.
public float getWidth()
Returns: float - Width in pixels

getHeight

Returns the visual height of the player.
public float getHeight()
Returns: float - Height in pixels

getBounds

Returns the collision rectangle of the player. The rectangle is smaller than the visual size for fairer gameplay.
public Rectangle getBounds()
Returns: Rectangle - The collision bounds Example:
if (player.getBounds().overlaps(obstacle.getBounds())) {
    // Handle collision
}

getBullets

Returns all active bullets fired by the player.
public Array<Bullet> getBullets()
Returns: Array<Bullet> - Array containing all active bullets Example:
for (Bullet bullet : player.getBullets()) {
    if (bullet.getBounds().overlaps(enemy.getBounds())) {
        bullet.setActive(false);
        enemy.destroy();
    }
}

Complete Usage Example

public class GameScreen implements Screen {
    private SpriteBatch batch;
    private Player player;
    
    @Override
    public void show() {
        batch = new SpriteBatch();
        Texture playerTexture = new Texture("images/player.png");
        
        // Create player at center bottom of screen
        float x = Gdx.graphics.getWidth() / 2 - 32;
        float y = 50;
        player = new Player(playerTexture, x, y, 64, 64, 5.0f);
        player.activate();
    }
    
    @Override
    public void render(float delta) {
        // Handle input
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            player.moveLeft();
        }
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            player.moveRight();
        }
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
            player.moveUp();
        }
        if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            player.moveDown();
        }
        if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
            player.shoot();
        }
        
        // Update and render
        player.update(delta);
        
        batch.begin();
        player.render(batch);
        batch.end();
    }
    
    @Override
    public void dispose() {
        player.dispose();
        batch.dispose();
    }
}

Build docs developers (and LLMs) love