
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

Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
New

Title: Web Development with Clojure, Third Edition - migrations/create not working: p159
When I execute the command:
user=> (create-...
New

I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps.
With:
ruby-3.0.0
s...
New

Hello! Thanks for the great book.
I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New

#book-python-testing-with-pytest-second-edition
Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
New

Hi all,
currently I wonder how the Tailwind colours work (or don’t work).
For example, in app/views/layouts/application.html.erb I have...
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

root_layout: {PentoWeb.LayoutView, :root},
This results in the following following error:
no “root” html template defined for PentoWeb...
New

I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New
Other popular topics

What chair do you have while working… and why?
Is there a ‘best’ type of chair or working position for developers?
New

New

Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face:
Perhaps if there’s enough peop...
New
New

This looks like a stunning keycap set :orange_heart:
A LEGENDARY KEYBOARD LIVES ON
When you bought an Apple Macintosh computer in the e...
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

Saw this on TikTok of all places! :lol:
Anyone heard of them before?
Lite:
New

Author Spotlight:
Peter Ullrich
@PJUllrich
Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Author Spotlight:
Bruce Tate
@redrapids
Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

A Brief Review of the Minisforum V3 AMD Tablet.
Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /typescript
- /svelte
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /react
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /deepseek
- /zig
- /scala
- /textmate
- /sublime-text
- /lisp
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /julia
- /slackware
- /c
- /markdown