Escriu per cercar…

Ursina

Ursina is a Python wrapper around the Panda3D game engine

Aquesta pàgina encara no s'ha traduït — es mostra en l'idioma original:English

Introduction

Ursina’s most critical parts are written in C++ or shader code, something that allows you to use Python on top of it without incurring any penalties in performance, hence letting you free to enjoy the productivity advantages of writing code in Python.

Panda3D is an Open Source game engine created by Disney and still used for production development.

ps
uv add ursina

Creating your first program

Probably the most intimidating step into writing games is to initialize the window. There are so many concepts that go with this initialization like selecting a screen size, setting the color modes, creating the back buffers, setting up the video card, and writing down the most basic graphics switching system and all that even before we can even see anything.

Fortunately, Ursina makes it so easy that you get all that in three lines. Python is a scripting language, which in essence means you write a text file and the Python interpreter will run it straight, no need to compile or anything.

Paste the following code into main.py:

python
main.py
from ursina import *                    # Import the ursina engine

app = Ursina()                          # Initialise your Ursina app
app.run()                               # Run the app

Run the program with uv run main.py: you will see a window with a red cross on top.

Improving your window

So the window looks good, but it might improve with some work, edit your main.py program to match the code below.

ps
from ursina import *                    # Import the ursina engine

app = Ursina()                          # Initialise your Ursina app

window.title = 'My Game'                # The window title
window.borderless = False               # Show a border
window.fullscreen = False               # Do not go Fullscreen
window.exit_button.visible = False      # Do not show the in-game red X that loses the window
window.fps_counter.enabled = True       # Show the FPS (Frames per second) counter

app.run()                               # Run the app

The “window” object is part of the application, and you can access it directly. Some elements of the window can be accessed after the game is running, but some cannot. Play with those values setting them to True or False and check the effect. Go to the documentation at https://www.ursinaengine.org/cheat_sheet.html#window to see even more options for the window.

Responding to events

So now you have a window running, try to see how to control things from the game.

Game engines work in “passes”. On each pass it will check input conditions, check sound buffers, compute the AI, update the internal game state and logic, then renders the current scene to the background, then does the “flipping” which updates the contents of the screen.

The Ursina engine simplifies all this process, so no need to do the rendering by yourself, but you are allowed to use the “update” function to test and update your internal logic need, then let the engine do the rendering.

To use the update() function, add it to your window program like this:

python
def update():
    print("Update!")                    # Print Update every time this loop is executed
    if held_keys['t']:                  # If t is pressed
        print(held_keys['t'])           # Print the value

Estàs llegint una vista prèvia.

Inicia sessió per llegir l'article complet. Qualsevol compte obre 4 articles gratuïts al mes; l'alumnat i el professorat llegeixen les pàgines del seu curs sense límit.

Inicia sessió