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
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84:
bundle exec sequel...
New
Title: Web Development with Clojure, Third Edition, pg 116
Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display)
It looks like
.with_simple_console_no_bg(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
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
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
Dear Sophie.
I tried to do the “Authorization” exercise and have two questions:
When trying to plug in an email-service, I found the ...
New
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
AWDWR 7, page 152, page 153:
Hello everyone,
I’m a little bit lost on the hotwire part. I didn’t fully understand it.
On page 152 @rub...
New
@mfazio23
I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New
Other popular topics
Reading something? Working on something? Planning something? Changing jobs even!?
If you’re up for sharing, please let us know what you’...
New
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
Was just curious to see if any were around, found this one:
I got 51/100:
Not sure if it was meant to buy I am sure at times the b...
New
Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New
Jan | Rethink the Computer.
Jan turns your computer into an AI machine by running LLMs locally on your computer. It’s a privacy-focus, l...
New
Background
Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /zig
- /centos
- /deepseek
- /php
- /scala
- /react-native
- /lisp
- /sublime-text
- /textmate
- /nixos
- /debian
- /agda
- /django
- /kubuntu
- /deno
- /arch-linux
- /nodejs
- /ubuntu
- /revery
- /spring
- /manjaro
- /diversity
- /lua
- /julia
- /markdown
- /slackware








