⚠️ Beta State

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.

Drawing Sprites

Learn how to display a 2D image (a sprite) on the screen.

Introduction

This is the first step in our 2D track! We'll learn the two essential components for 2D rendering: a Camera2d to view the world, and a Sprite to be seen.

from pybevy.prelude import *

The Setup System

We will create a setup system that runs once at startup.

  • commands.spawn(Camera2d()): This creates our 2D camera. Without a camera, nothing would be rendered to the screen.
  • asset_server.load_image(...): We use the asset_server resource to load our image file from the assets folder.
  • commands.spawn(Sprite(...)): This creates an entity with a Sprite component. The sprite component tells the renderer to draw the specified image.
def setup(commands: Commands, asset_server: AssetServer):
    # Spawn the 2D camera
    commands.spawn(Camera2d())
 
    # Spawn a sprite
    commands.spawn(
        Sprite(image=asset_server.load_image("branding/icon.png"))
    )

Running the App

We add DefaultPlugins and our setup system. When you run this, you will see the PyBevy logo in the center of the window.

@entrypoint
def main(app: App) -> App:
    return app.add_plugins(DefaultPlugins).add_systems(Startup, setup)
 
if __name__ == "__main__":
    main().run()