Skip to content

B. Making a Game Window

Estimated time: 5 min | Previous: A. Importing Libraries | Next: C. Declaring Variables


๐ŸŽฏ Learning Goals

By the end of this lesson, you'll be able to:

  • โ—‹ app = Ursina()can make a game window with
  • โ—‹ understand why we store the game window in a variable
  • โ—‹ app.run()can run the game with

Core Concepts

Making a Game Window

Game windowis the window where the 3D game is displayed.

๐Ÿ“ Analogy: Setting up a stage

A game window is a theater stageis the same as!

  1. First you set up the stage (Ursina())
  2. Then place the actors (Entity)
  3. And start the show (app.run())

Without a stage, there's no show!

Program flowchart

flowchart TD
    A["from ursina import *"] --> B["app = Ursina()"]
    B --> C["Write game content<br/>(Entity, etc.)"]
    C --> D["app.run()"]
    D --> E["Game window appears!"]

    style A fill:#e3f2fd,stroke:#1976d2
    style B fill:#fff3e0,stroke:#f57c00
    style C fill:#e8f5e9,stroke:#388e3c
    style D fill:#f3e5f5,stroke:#7b1fa2
    style E fill:#ffebee,stroke:#c62828

Writing the code

app = Ursina()

Description:

  • Ursina(): the function that creates the game window
  • app =: the game window app Stores it in a variable
  • This becomes the basic framework of the game

Running the game

app.run()

Description:

  • app.run(): runs the game
  • Without this line, the window won't appear!

โš ๏ธ app.run() always goes last!

app.run()goes at the very endof your code!

Any code after this line won't run until the game ends.

Code structure

Order Code Role
1 from ursina import * Import the library
2 app = Ursina() Making a Game Window
3 (game content) Add Entities and more
4 app.run() Run the game

Example Code

from ursina import *

app = Ursina()  # Make the game window

# Add game content here

app.run()  # Run the game!

Result

A game window with a black background appears!

  • Window title: "ursina"
  • Default background color: dark gray
  • Press ESC to quit

๐ŸŽฏ Quiz

Quiz: the role of app.run()
app.run()what does it do?
Show Answer

Answer: A) Launches the game window and shows it on screen

app.run() starts the game loop and shows the window on screen. Once this function is called, it keeps running until the game quits!


๐Ÿ”ง Debug Clinic

Problem: I run the code but no window appears!

Cause: app.run() is missing

Fix: At the end of your code, app.run() Add

# โŒ Wrong code
from ursina import *
app = Ursina()
Entity(model='cube')
# app.run() is missing!
# โœ… Correct code
from ursina import *
app = Ursina()
Entity(model='cube')
app.run()  # This line is required!

Problem: The window appears and then closes right away!

Cause: There's an error in the code, or app.run() is in the wrong place

Fix: Check the error message and review the order of your code

# โŒ Wrong code - Entity created after app.run()
from ursina import *
app = Ursina()
app.run()
Entity(model='cube')  # Doesn't run!
# โœ… Correct code - app.run() after creating the Entity
from ursina import *
app = Ursina()
Entity(model='cube')
app.run()  # Very last!

Try Coding It Yourself

Complete the code that creates and runs the game window!

Making a Game Window

Fill in the blanks so the game window appears.

Example Answer
from ursina import *

app = Ursina()

app.run()

Result: An empty game window with a black background appears!


๐Ÿงฉ Key Takeaways

What you learned in this lesson:

  • Ursina(): The function that creates the game window
  • app variable: The variable that stores the game window
  • app.run(): The function that runs the game (always at the very end!)
  • Order: import โ†’ Ursina() โ†’ game content โ†’ run()

Review Checklist

  • [ ] app = Ursina()Can you explain what it does?
  • [ ] app.run()Do you know where it should go?
  • [ ] When the game window doesn't appear, what should you check?

Next step: C. Declaring Variables โ†’