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!
- First you set up the stage (Ursina())
- Then place the actors (Entity)
- 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
Description:
Ursina(): the function that creates the game windowapp =: the game windowappStores it in a variable- This becomes the basic framework of the game
Running the game
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
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
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
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
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 โ