Skip to content

E. Final Code and Running It

Estimated time: 5 min | Previous: D. Making a 3D Cube with Entity


🎯 Learning Goals

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

  • ○ Understand the structure of the finished code
  • ○ Be able to run the code
  • ○ Be able to avoid common mistakes

Core Concepts

Overall Program Structure

📝 Analogy: Constructing a Building

An Ursina program is like constructing a buildingis the same as!

  1. Gather materials - import (buy bricks and cement)
  2. Clear the ground - Ursina() (level the land)
  3. Draw the blueprint - declare variables (what goes where)
  4. Put up the building - Entity (walls, doors, windows)
  5. Open the doors - app.run() (move in!)

Final Code Flowchart

flowchart TD
    A["1. from ursina import *"] --> B["2. app = Ursina()"]
    B --> C["3. Declare variables"]
    C --> D["4. print() output"]
    D --> E["5. Create Entity()"]
    E --> F["6. app.run()"]
    F --> G["Game runs!"]

    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
    style F fill:#e0f2f1,stroke:#00796b
    style G fill:#fce4ec,stroke:#c2185b

Code Structure Summary

Order Code Role
1 from ursina import * Import the library
2 app = Ursina() Making a Game Window
3 Declare variables Store configuration values
4 print() Console output
5 Entity() Create 3D objects
6 app.run() Run the game

⚠️ Be sure to keep the order!

  • importalways goes at the very top
  • app.run()always goes at the very bottom
  • An Entity is Ursina()and app.run() in between!

Final Code

from ursina import *  # 1. Import the library

app = Ursina()  # 2. Create the game window

# 3. Declare variables
my_name = "Student"
cube_size = 2
cube_color = color.red

# 4. Console output
print("Hello, Ursina!")
print(f"My name is {my_name}")

# 5. Create a cube with Entity
my_cube = Entity(
    model='cube',
    color=cube_color,
    scale=cube_size
)

app.run()  # 6. Run the game

Result

  • "Hello, Ursina!" and your name are printed to the console
  • A red cube appears in the center of the screen
  • Press ESC to quit

How to Run

Run from the terminal

# 1. Move to the folder where the file is
cd folder_path

# 2. Run it with Python
python filename.py

Shortcut

Action Windows Mac
Quit the game ESC Or Alt+F4 ESC Or Cmd+Q

🎯 Quiz

Quiz: Code Order
What should come at the very end of the code?
Show Answer

Answer: B) app.run()

app.run() starts the game loop, so it should always come very end. Any code after this line won't run until the game quits!


🔧 Debug Clinic

Mistake 1: Forgetting import

# ❌ Starting without from ursina import *
app = Ursina()  # NameError!

Fix: At the very top of the file, from ursina import * Add

Mistake 2: Forgetting app.run()

# ❌ No app.run() at the end
Entity(model='cube')
# No window appears!

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

Mistake 3: Typo in a variable name

# ❌ Mixing up cube_color and cube_colour
cube_color = color.red
Entity(color=cube_colour)  # NameError!

Fix: Check your variable names carefully


Try Coding It Yourself

Write the complete code yourself from scratch!

Make a Green Sphere

Start from an empty file and write the full code that shows a green sphere on screen.

Example Answer
from ursina import *

app = Ursina()

my_sphere = Entity(
    model='sphere',
    color=color.green
)

app.run()

Result: A green sphere appears in the center of the screen!


Challenge

Challenge 1: ⭐ Easy

Change the cube's color to blue (color.blue).

Hint: cube_color Just change the value of the variable.

Challenge 2: ⭐ Easy

Change the cube's size to 5.

Hint: cube_size Find the variable and change it.

Challenge 3: ⭐⭐ Medium

Add a sphere as well.

Hint: model='sphere'Use it. Set the position to position=(2, 0, 0)so it doesn't overlap with the cube.

Challenge 4: ⭐⭐ Medium

Print the cube's color and size to the console.

Hint: You can use an f-string to print variable values.

Challenge 5: ⭐⭐⭐ Hard

Place three cubes of different colors in a row.

Hint: Set each cube's positiondifferently. For example: (0,0,0), (2,0,0), (4,0,0)

Challenge 6: ⭐⭐⭐ Hard

Make a pyramid shape: a large cube on the bottom, a small cube on top.

Hint: scalefor the size, and positionuse the y value to adjust the height.


🧩 Key Takeaways

What you learned in this lesson:

Program structure (order matters!):

  1. from ursina import * - Library
  2. app = Ursina() - Game window
  3. Variable declarations - settings
  4. Entity() - 3D objects
  5. app.run() - Run (last of all!)

Review Checklist

  • [ ] Did you memorize the basic structure of an Ursina program?
  • [ ] Can you explain why the order of the code matters?
  • [ ] Do you know the 3 most common mistakes?

Congratulations! 🎉

You finished your first 3D app!

Things you can do now:

  • ✅ Import the Ursina library
  • ✅ Create a game window
  • ✅ Store values in variables
  • ✅ Make a 3D cube with Entity
  • ✅ Run the game

Next lesson: Variables and Argumentslet's learn about the various arguments of Entity!