PyBevy is in an early and experimental stage. The API is incomplete, subject to breaking changes without notice, and you should expect bugs. Many features are still under development.
Sprite Tile
A sprite tiled in a grid with an oscillating size animation.
Introduction
Sprites can tile their image across a region instead of stretching. Combined with a custom animation resource, this creates a dynamically scaling tiled texture.
from pybevy.prelude import *
from pybevy.sprite import SpriteImageModeAnimation State Resource
A resource that tracks the current size and oscillation speed for the animation.
@resource
class AnimationState(Resource):
def __init__(self):
self.min = 128.0
self.max = 512.0
self.current = 128.0
self.speed = 50.0Setup
Create a tiled sprite using SpriteImageMode.tiled(). The image repeats instead of stretching when the sprite size changes.
def setup(commands: Commands, asset_server: AssetServer) -> None:
commands.spawn(Camera2d())
sprite = Sprite(image=asset_server.load_image("branding/icon.png"))
sprite.image_mode = SpriteImageMode.tiled(tile_x=True, tile_y=True, stretch_value=0.5)
commands.spawn(sprite)Animation System
Oscillate the sprite's custom size between min and max values each frame.
def animate(sprites: Query[Mut[Sprite]], state: ResMut[AnimationState], time: Res[Time]) -> None:
if state.current >= state.max or state.current <= state.min:
state.speed = -state.speed
state.current += state.speed * time.delta_secs()
for sprite in sprites:
sprite.custom_size = (state.current, state.current)Running the App
@entrypoint
def main(app: App) -> App:
return (
app
.add_plugins(DefaultPlugins)
.insert_resource(AnimationState())
.add_systems(Startup, setup)
.add_systems(Update, animate)
)
if __name__ == "__main__":
main().run()Running this example
Use PyBevy's hot reload feature to run and develop this example. If you don't have PyBevy installed, check out the Quick Start guide.
The code will reload automatically when you make changes to the file.
From Python to Rust
Notice how the core concepts in the code—Commands, Assets, App, and Systems—are identical to the original Bevy example?
This is the power of pybevy! It lets you learn Bevy's powerful, data-driven architecture in friendly Python.
When your project grows and you're ready for maximum, native performance, you'll already know the concepts to start writing systems in Bevy Engine with Rust.