diff --git a/content/learn/book/contributing/_index.md b/content/learn/book/contributing/_index.md index 8546049d34..73af1718a9 100644 --- a/content/learn/book/contributing/_index.md +++ b/content/learn/book/contributing/_index.md @@ -1,6 +1,6 @@ +++ title = "Contributing" -weight = 4 +weight = 5 sort_by = "weight" template = "book-section.html" page_template = "book-section.html" diff --git a/content/learn/book/faq/_index.md b/content/learn/book/faq/_index.md index f89045ffd0..8f638daf3f 100644 --- a/content/learn/book/faq/_index.md +++ b/content/learn/book/faq/_index.md @@ -1,6 +1,6 @@ +++ title = "Faq" -weight = 6 +weight = 7 sort_by = "weight" template = "book-section.html" page_template = "book-section.html" diff --git a/content/learn/book/next-steps/_index.md b/content/learn/book/next-steps/_index.md index 3c0284409f..ff3098b7b1 100644 --- a/content/learn/book/next-steps/_index.md +++ b/content/learn/book/next-steps/_index.md @@ -1,6 +1,6 @@ +++ title = "Next Steps" -weight = 3 +weight = 4 sort_by = "weight" template = "book-section.html" page_template = "book-section.html" diff --git a/content/learn/book/troubleshooting/_index.md b/content/learn/book/troubleshooting/_index.md index 889c234ee8..544eac6683 100644 --- a/content/learn/book/troubleshooting/_index.md +++ b/content/learn/book/troubleshooting/_index.md @@ -1,6 +1,6 @@ +++ title = "Troubleshooting" -weight = 5 +weight = 6 sort_by = "weight" template = "book-section.html" page_template = "book-section.html" diff --git a/content/learn/book/your-first-game/_index.md b/content/learn/book/your-first-game/_index.md new file mode 100644 index 0000000000..624276e6b4 --- /dev/null +++ b/content/learn/book/your-first-game/_index.md @@ -0,0 +1,685 @@ ++++ +title = "Your first game" +weight = 3 +sort_by = "weight" +template = "book-section.html" +page_template = "book-section.html" ++++ + +Now that you know the basics of Bevy, let's build a simple game. Specifically, let's look at one of the examples that come with Bevy: Breakout. + +![screenshot1](breakout1.png) + +In [Breakout](https://en.wikipedia.org/wiki/Breakout_(video_game)), a layer of bricks lines the top third of the screen and the goal is to destroy them all. A ball moves straight around the screen, bouncing off the top and two sides of the screen. When a brick is hit, the ball bounces back and the brick is destroyed. The player has a horizontally movable paddle to bounce the ball upward, keeping it in play. + +Here is a version of the game compiled to WebGL that you can play right away: +[https://mrk.sed.pl/bevy-showcase/breakout.html](https://mrk.sed.pl/bevy-showcase/breakout.html) + +In this tutorial, we're going to build up our example game one step at a time until it reaches the finished complete [source code of the game](https://github.com/bevyengine/bevy/blob/latest/examples/game/breakout.rs). + +So what are the elements of our game? + + * A paddle + * A ball + * Bricks + * Walls + * A score counter + +Let's start with the paddle: + +```rs +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>, + asset_server: Res, +) { + 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