
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

Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online:
p. 186 But...
New

Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New

Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New

On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New

A Common-Sense Guide to Data Structures and Algorithms, Second Edition by Jay Wengrow @jaywengrow
Hi,
I have the paperback version of t...
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

When I run the coverage example to report on missing lines, I get:
pytest --cov=cards --report=term-missing ch7
ERROR: usage: pytest [op...
New

When running tox for the first time, I got the following error:
ERROR: InterpreterNotFound: python3.10
I realised that I was running ...
New

Is the book’s epub format available to read on Google Play Books?
New

Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
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

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New

Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New

Just done a fresh install of macOS Big Sur and on installing Erlang I am getting:
asdf install erlang 23.1.2
Configure failed.
checking ...
New

Small essay with thoughts on macOS vs. Linux:
I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New

The V Programming Language
Simple language for building maintainable programs
V is already mentioned couple of times in the forum, but I...
New

Seems like a lot of people caught it - just wondered whether any of you did?
As far as I know I didn’t, but it wouldn’t surprise me if I...
New

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
New

API 4
Path:
/user/following/
Method:
GET
Description:
Returns the list of all names of people whom the user follows
Response
[
{ ...
New

Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
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
- /svelte
- /typescript
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /ocaml
- /react
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /centos
- /html
- /php
- /deepseek
- /zig
- /scala
- /textmate
- /lisp
- /sublime-text
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /revery
- /django
- /ubuntu
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /julia
- /slackware
- /c
- /neovim