mthelm85
mthelm85

Reputation: 151

How to add a background image to 2d game in Bevy

How can I add a background image to my 2d game? The code is here:

https://github.com/mthelm85/game

In assets/textures I have a file called pitch.png that I would like to serve as the background for my window. I can't find any info on how to do this.

Thanks!

Upvotes: 4

Views: 5287

Answers (1)

frankenapps
frankenapps

Reputation: 8251

You do not need to set the z-translation. Bevy will render the last added item on top. So if you want to use the image as a background simply spawn the corresponding sprite first like that:

fn setup(
    commands: &mut Commands,
    asset_server: Res<AssetServer>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    let background_image: Handle<Texture> = asset_server.load("textures/pitch.png");
    let texture_handle = asset_server.load("sprites/sprite_max.png");
    commands
        .spawn(Camera2dBundle::default())
        .spawn(SpriteBundle {
            material: materials.add(background_image.into()),
            ..Default::default()
        })
        .spawn(SpriteBundle {
            material: materials.add(texture_handle.into()),
            ..Default::default()
        })
        .with(Max);
}

If you want the image to cover the whole screen initially you will have to scale it accordingly, for example like so:

use bevy::prelude::*;

fn main() {
    App::build()
        .add_resource(WindowDescriptor {
            title: "Game".to_string(),
            width: 900.0,
            height: 900.0,
            vsync: true,
            resizable: false,
            ..Default::default()
        })
        .add_system(move_max.system())
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup.system())
        .run();
}

fn setup(
    commands: &mut Commands,
    asset_server: Res<AssetServer>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    let background_image: Handle<Texture> = asset_server.load("textures/pitch.png");
    let texture_handle = asset_server.load("sprites/sprite_max.png");
    commands
        .spawn(Camera2dBundle::default())
        .spawn(SpriteBundle {
            material: materials.add(background_image.into()),
            transform: Transform::from_scale(Vec3::new(1.5, 1.5, 0.0)),
            ..Default::default()
        })
        .spawn(SpriteBundle {
            material: materials.add(texture_handle.into()),
            ..Default::default()
        })
        .with(Max);
}

struct Max;

fn move_max(
    keyboard_input: Res<Input<KeyCode>>,
    mut max_positions: Query<&mut Transform, With<Max>>,
) {
    for mut transform in max_positions.iter_mut() {
        if keyboard_input.pressed(KeyCode::Left) {
            transform.translation.x -= 1.0;
        }
        if keyboard_input.pressed(KeyCode::Right) {
            transform.translation.x += 1.0;
        }
        if keyboard_input.pressed(KeyCode::Down) {
            transform.translation.y -= 1.0;
        }
        if keyboard_input.pressed(KeyCode::Up) {
            transform.translation.y += 1.0;
        }
    }
}

Upvotes: 4

Related Questions