1. Home
  2. /
  3. Bevy
  4. /
  5. Moving Sprite Example

Moving Sprite Example - Bevy Typing CST Test

Loading…

Moving Sprite Example — Bevy Code

Moves a sprite with arrow keys.

use bevy::prelude::*;

fn main() {
	App::build()
		.add_plugins(DefaultPlugins)
		.add_startup_system(setup.system())
		.add_system(move_sprite.system())
		.run();
}

struct Player;

fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut materials: ResMut<Assets<ColorMaterial>>) {
	commands.spawn_bundle(OrthographicCameraBundle::new_2d());
	commands.spawn_bundle(SpriteBundle {
		material: materials.add(asset_server.load("player.png").into()),
		..Default::default()
	}).insert(Player);
}

fn move_sprite(keyboard_input: Res<Input<KeyCode>>, mut query: Query<&mut Transform, With<Player>>) {
	for mut transform in query.iter_mut() {
		if keyboard_input.pressed(KeyCode::Left) { transform.translation.x -= 2.0; }
		if keyboard_input.pressed(KeyCode::Right) { transform.translation.x += 2.0; }
		if keyboard_input.pressed(KeyCode::Up) { transform.translation.y += 2.0; }
		if keyboard_input.pressed(KeyCode::Down) { transform.translation.y -= 2.0; }
	}
}

Bevy Language Guide

Bevy is a modern, open-source Rust game engine designed for high-performance 2D and 3D games, with a focus on ECS architecture, modularity, and cross-platform support for desktop and web.

Primary Use Cases

  • ▸Cross-platform 2D and 3D games
  • ▸Rust-native game projects
  • ▸Educational games and simulations
  • ▸Prototypes and experimental game engines
  • ▸WebAssembly and desktop deployment

Notable Features

  • ▸Rust-based ECS (Entity Component System)
  • ▸Modular plugin architecture
  • ▸wgpu renderer for 2D/3D
  • ▸Bevy UI toolkit
  • ▸Asset management and hot-reloading

Origin & Creator

Bevy was created by Carter Anderson and maintained by an open-source community, emphasizing Rust-native ECS and performance-focused game development.

Industrial Note

Bevy is gaining traction in Rust-native game development, educational games, indie 2D/3D games, simulations, and hobby projects where performance and safety are priorities.

Quick Explain

  • ▸Bevy uses Rust and provides a data-driven Entity Component System (ECS) for managing game logic efficiently.
  • ▸It has a built-in renderer using wgpu (WebGPU), supporting modern graphics APIs.
  • ▸Bevy includes systems for input handling, audio, physics integration, asset management, and UI with its own Bevy UI toolkit.
  • ▸Developers benefit from Rust’s safety, performance, and concurrency while building games that can run on Windows, macOS, Linux, and WebAssembly.
  • ▸Bevy is ideal for high-performance 2D/3D games, prototypes, simulations, and experimental Rust projects.

Core Features

  • ▸System scheduling for ECS
  • ▸Scene management via entities/components
  • ▸Input handling across platforms
  • ▸Camera and transform management
  • ▸Audio and asset pipelines

Learning Path

  • ▸Week 1: Rust basics, ECS concepts
  • ▸Week 2: Entities, components, and systems
  • ▸Week 3: 2D rendering and input
  • ▸Week 4: UI and audio
  • ▸Week 5: 3D, plugins, and WASM deployment

Practical Examples

  • ▸2D platformer
  • ▸Top-down shooter
  • ▸Roguelike with ECS
  • ▸3D prototype with camera controls
  • ▸WebAssembly casual game

Comparisons

  • ▸Bevy vs Unity: Bevy is Rust-native ECS; Unity is editor-first and C#-based
  • ▸Bevy vs Godot: Bevy is ECS-first and code-centric; Godot provides editor + GDScript
  • ▸Bevy vs LibGDX: Bevy uses Rust; LibGDX uses Java/Kotlin
  • ▸Bevy vs Phaser: Bevy for native and WASM, Phaser for HTML5 web
  • ▸Bevy excels in safety, performance, and Rust-native ECS

Strengths

  • ▸High-performance via Rust and ECS
  • ▸Memory safety and concurrency advantages
  • ▸Fast iteration with hot-reload
  • ▸Cross-platform including WASM
  • ▸Lightweight and modular architecture

Limitations

  • ▸Smaller ecosystem than Unity/LibGDX
  • ▸No official physics engine (requires plugins)
  • ▸UI system still evolving
  • ▸Limited built-in tools for 3D compared to AAA engines
  • ▸Requires Rust knowledge

When NOT to Use

  • ▸Non-Rust projects
  • ▸AAA console pipelines
  • ▸Teams needing visual editors
  • ▸Zero-code environments
  • ▸Projects requiring extensive built-in physics/UI tools

Cheat Sheet

  • ▸`App::build()`: initialize engine
  • ▸`spawn()`: create entity
  • ▸`Commands`: manipulate entities
  • ▸`SystemSet`: schedule systems
  • ▸`Query`: access components in systems

FAQ

  • ▸Is Bevy free?
  • ▸Yes, MIT open-source license.
  • ▸Does it support 3D?
  • ▸Yes, fully 3D with wgpu renderer.
  • ▸Can it deploy to web?
  • ▸Yes, via WebAssembly.
  • ▸Is Rust required?
  • ▸Yes, Bevy is Rust-native.
  • ▸Is it beginner-friendly?
  • ▸For Rust developers; requires programming knowledge.

30-Day Skill Plan

  • ▸Build small 2D games
  • ▸Explore ECS patterns
  • ▸Integrate physics and audio plugins
  • ▸Learn wgpu rendering basics
  • ▸Port projects to WebAssembly

Final Summary

  • ▸Bevy is a modern, Rust-native ECS game engine for 2D and 3D.
  • ▸It offers high performance, memory safety, and modular architecture.
  • ▸Ideal for desktop and WebAssembly games, prototypes, and simulations.
  • ▸Fully code-centric with a focus on Rust developers.
  • ▸Best for teams wanting control, safety, and Rust-native tooling.

Project Structure

  • ▸src/main.rs (core logic)
  • ▸assets/ (images, audio, shaders)
  • ▸Cargo.toml (dependencies and settings)
  • ▸plugins/ (optional custom Bevy plugins)
  • ▸systems/ (game logic organization)

Monetization

  • ▸Desktop paid releases
  • ▸WASM games with subscriptions
  • ▸Integration with ad services via Rust bindings
  • ▸In-game purchases via server APIs
  • ▸Licensing game engines built with Bevy

Productivity Tips

  • ▸Use ECS bundles to simplify entity creation
  • ▸Leverage plugins for repetitive functionality
  • ▸Use asset hot-reloading
  • ▸Keep systems small and modular
  • ▸Profile often during development

Basic Concepts

  • ▸Entities are game objects
  • ▸Components are data attached to entities
  • ▸Systems operate on entities/components
  • ▸Resources store global state
  • ▸Plugins organize functionality

Official Docs

  • ▸https://bevyengine.org/
  • ▸https://docs.rs/bevy/latest/bevy/
  • ▸https://github.com/bevyengine/bevy

More Bevy Typing Exercises

Bevy Simple Counter ExampleBevy FPS Counter ExampleBevy Simple Timer ExampleBevy Mouse Position ExampleBevy Simple Animation ExampleBevy Keyboard Input ExampleBevy Spawn Moving Entity ExampleBevy Click Detection ExampleBevy Random Movement Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher