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.
Mouse Grab
Capture and hide the mouse cursor for FPS-style camera control.
Introduction
In FPS games, you need to capture the mouse so it doesn't leave the window. This example shows how to toggle cursor visibility and grab mode with mouse click and Escape key.
from pybevy.prelude import *
from pybevy.window import Window, CursorGrabModeCursor Control System
Left-click to grab the cursor (hide and lock it). Press Escape to release.
def cursor_grab(
input: Res[ButtonInput],
windows: Query[Mut[Window]],
) -> None:
for window in windows:
if input.just_pressed(MouseButton.Left):
window.cursor_options.visible = False
window.cursor_options.grab_mode = CursorGrabMode.Locked
print("Cursor grabbed")
if input.just_pressed(KeyCode.Escape):
window.cursor_options.visible = True
window.cursor_options.grab_mode = CursorGrabMode.None_
print("Cursor released")Running the App
@entrypoint
def main(app: App) -> App:
return app.add_plugins(DefaultPlugins).add_systems(Update, cursor_grab)
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.