ds2k5
Advanced Hands-on Rust: moving_dragon - little mod - diagonal movment
Hi,
I did a little mod of the Code: moving_dragon
so that the “dragon” can move UP/DOWN + RIGHT/LEFT (diagonal) at the same time
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, movement)
.run();
}
#[derive(Component)]
struct Dragon;
fn setup(//(1)
mut commands: Commands, //(2)
asset_server: Res<AssetServer>//(3)
) {
commands.spawn(Camera2d::default());//(4)
let dragon_image = asset_server.load("dragon_left.png");//(5)
commands
.spawn(Sprite::from_image(dragon_image))//(6)
.insert(Dragon);//(7)
}
fn movement(
keyboard: Res<ButtonInput<KeyCode>>,//(8)
mut dragon_query: Query<&mut Transform, With<Dragon>>,//(9)
) {
let delta = if keyboard.all_pressed([KeyCode::ArrowLeft, KeyCode::ArrowUp]) {//(10)
Vec2::new(-1.0, 1.0)
} else if keyboard.all_pressed([KeyCode::ArrowLeft, KeyCode::ArrowDown]) {
Vec2::new(-1.0, -1.0)
} else if keyboard.all_pressed([KeyCode::ArrowRight, KeyCode::ArrowUp]) {
Vec2::new(1.0, 1.0)
} else if keyboard.all_pressed([KeyCode::ArrowRight, KeyCode::ArrowDown]) {
Vec2::new(1.0, -1.0)
} else if keyboard.pressed(KeyCode::ArrowLeft) {
Vec2::new(-1.0, 0.0)
} else if keyboard.pressed(KeyCode::ArrowRight) {
Vec2::new(1.0, 0.0)
} else if keyboard.pressed(KeyCode::ArrowDown) {
Vec2::new(0.0, -1.0)
} else if keyboard.pressed(KeyCode::ArrowUp) {
Vec2::new(0.0, 1.0)
} else if keyboard.all_pressed([KeyCode::ArrowUp, KeyCode::ArrowLeft]) {
Vec2::new(1.0, 1.0)
} else {
Vec2::ZERO
};
dragon_query.iter_mut().for_each(|mut transform| {//(11)
transform.translation += delta.extend(0.0);//(12)
});
}
But how to change the Code, that the Image is changed when pressing ArrowRight key ?
That the dragon looks to the right when moving right.
Popular Pragmatic Bookshelf topics
Title: Hands-On Rust (Chapter 11: prefab)
Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
Hi Jamis,
I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019.
This test doesn’t pass for me:
...
New
Hi @venkats,
It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
New
Title: Web Development with Clojure, Third Edition, vB17.0 (p9)
The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
On page 78 the following code appears:
<%= link_to ‘Destroy’, product,
class: ‘hover:underline’,
method: :delete,
data: { confirm...
New
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels.
Before ...
New
@mfazio23
I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
I am using Android Studio Chipmunk | 2021.2.1 Patch 2
Build #AI-212.5712.43.2112.8815526, built on July 10, 2022
Runtime version: 11.0....
New
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
Other popular topics
Brace yourself for a fun challenge: build a photorealistic 3D renderer from scratch! In just a couple of weeks, build a ray tracer that r...
New
Which, if any, games do you play? On what platform?
I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
New
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first:
asdf plugin-upd...
New
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
New
Hair Salon Games for Girls Fun
Girls Hair Saloon game is mainly developed for kids. This game allows users to select virtual avatars to ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /deepseek
- /php
- /zig
- /scala
- /sublime-text
- /lisp
- /textmate
- /react-native
- /debian
- /nixos
- /agda
- /kubuntu
- /arch-linux
- /django
- /deno
- /revery
- /nodejs
- /ubuntu
- /manjaro
- /spring
- /diversity
- /lua
- /julia
- /markdown
- /slackware








