Skip to content

Instantly share code, notes, and snippets.

@rhulha
Created April 30, 2021 07:20
Show Gist options
  • Save rhulha/0d41043539b979b1da450ee0c745490d to your computer and use it in GitHub Desktop.
Save rhulha/0d41043539b979b1da450ee0c745490d to your computer and use it in GitHub Desktop.
Breakout Step 1
use bevy::{
prelude::*,
render::pass::ClearColor,
};
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::rgb(0.9, 0.9, 0.9)))
.add_startup_system(setup.system())
.add_system(paddle_movement_system.system())
.run();
}
struct Paddle {
speed: f32,
}
enum Collider {
Solid,
Scorable,
Paddle,
}
fn setup(
commands: &mut Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
) {
commands
// cameras
.spawn(OrthographicCameraBundle::new_2d())
.spawn(UiCameraBundle::default())
// paddle
.spawn(SpriteBundle {
material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()),
transform: Transform::from_xyz(0.0, -215.0, 0.0),
sprite: Sprite::new(Vec2::new(120.0, 30.0)),
..Default::default()
})
.with(Paddle { speed: 500.0 })
.with(Collider::Paddle);
}
fn paddle_movement_system(
time: Res<Time>,
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&Paddle, &mut Transform)>,
) {
for (paddle, mut transform) in query.iter_mut() {
let mut direction = 0.0;
if keyboard_input.pressed(KeyCode::Left) {
direction -= 1.0;
}
if keyboard_input.pressed(KeyCode::Right) {
direction += 1.0;
}
let translation = &mut transform.translation;
// move the paddle horizontally
translation.x += time.delta_seconds() * direction * paddle.speed;
// bound the paddle
translation.x = translation.x.min(380.0).max(-380.0);
}
}
@rhulha
Copy link
Author

rhulha commented Nov 2, 2021

I am sorry to hear that, unfortunately my tutorial was not accepted into the Bevy book, so I have stopped working on it... As such I probably won't dedicate any more time on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment