bevy/examples/2d/pixel_perfect.rs
Aevyrie 1d8d78ef0e
Update color and naming for consistency (#10367)
The `ClearColor` PR was merged before I was quite finished. This fixes a
few errors, and addresses Cart's feedback about the pixel perfect
example by updating the sprite colors to match the existing bevy bird
branding colors.


![image](https://github.com/bevyengine/bevy/assets/2632925/33722c45-ed66-4d3a-af11-f4197611a13c)
2023-11-04 02:09:23 +00:00

49 lines
1.5 KiB
Rust

//! Renders a 2D scene containing pixelated bevy logo in a pixel perfect style
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(
// This sets image filtering to nearest
// This is done to prevent textures with low resolution (e.g. pixel art) from being blurred
// by linear filtering.
ImagePlugin::default_nearest(),
))
.add_systems(Startup, setup)
.add_systems(Update, sprite_movement)
.run();
}
#[derive(Component)]
enum Direction {
Left,
Right,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn((
SpriteBundle {
texture: asset_server.load("pixel/bevy_pixel_dark.png"),
transform: Transform::from_xyz(100., 0., 0.),
..default()
},
Direction::Right,
));
}
fn sprite_movement(time: Res<Time>, mut sprite_position: Query<(&mut Direction, &mut Transform)>) {
for (mut logo, mut transform) in &mut sprite_position {
match *logo {
Direction::Right => transform.translation.x += 30. * time.delta_seconds(),
Direction::Left => transform.translation.x -= 30. * time.delta_seconds(),
}
if transform.translation.x > 200. {
*logo = Direction::Left;
} else if transform.translation.x < -200. {
*logo = Direction::Right;
}
}
}