From 02001b4d4547e8befd44a959ea9936c51d348c21 Mon Sep 17 00:00:00 2001 From: Ray Date: Sun, 28 Feb 2021 08:39:11 +0100 Subject: [PATCH 01/60] Added a detailed tutorial for the breakout game to the Bevy book. --- content/learn/book/contributing/_index.md | 2 +- content/learn/book/faq/_index.md | 2 +- content/learn/book/next-steps/_index.md | 2 +- content/learn/book/troubleshooting/_index.md | 2 +- content/learn/book/your-first-game/_index.md | 688 ++++++++++++++++++ .../learn/book/your-first-game/breakout1.png | Bin 0 -> 8453 bytes .../learn/book/your-first-game/breakout2.png | Bin 0 -> 1855 bytes .../learn/book/your-first-game/breakout3.png | Bin 0 -> 2327 bytes .../learn/book/your-first-game/breakout4.png | Bin 0 -> 2624 bytes .../learn/book/your-first-game/breakout5.png | Bin 0 -> 4767 bytes 10 files changed, 692 insertions(+), 4 deletions(-) create mode 100644 content/learn/book/your-first-game/_index.md create mode 100644 content/learn/book/your-first-game/breakout1.png create mode 100644 content/learn/book/your-first-game/breakout2.png create mode 100644 content/learn/book/your-first-game/breakout3.png create mode 100644 content/learn/book/your-first-game/breakout4.png create mode 100644 content/learn/book/your-first-game/breakout5.png 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..296b035218 --- /dev/null +++ b/content/learn/book/your-first-game/_index.md @@ -0,0 +1,688 @@ ++++ +title = "Your first game" +weight = 3 +sort_by = "weight" +template = "book-section.html" +page_template = "book-section.html" ++++ + +Okay, now that you know the basics of Bevy, let's look at an actual game. Specifically let's look at one of the examples that come with Bevy: Breakout. + +![screenshot1](breakout1.png) + +For those of you who don't know, Breakout is an arcade game developed and published by Atari in 1976. In the 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 loses a turn when the ball touches the bottom of the screen; to prevent this from happening, the player has a horizontally movable paddle to bounce the ball upward, keeping it in play. [Source: Wikipedia](https://en.wikipedia.org/wiki/Breakout_(video_game)) + +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) + +You can take a look at the complete [source code of the game](https://github.com/bevyengine/bevy/blob/main/examples/game/breakout.rs), but I suggest to follow along this tutorial first, since it will slowly build up to the example game one step at a time. + +So what are the elements of the 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