Skip to content

Your First 3D App - Part B: Understanding the Code

Now let's analyze the code by its main parts!


Importing Libraries (Import Statement)

In the code above (1), if you look at line from ursina import * imports all of the features from the Ursina library.

Key concept: - from ursina: imports features from the Ursina library - import *: * means "everything." It imports all the features at once - Once you run this line Ursina(), Entity(), color and more become available to use right away

It's just like opening a toolbox and pulling out all the tools!


Creating the Game Window (Creating Game Window)

Code (2)in step app = Ursina() creates the game window.

app = Ursina()
  • Ursina() is the function that creates the game window
  • app so you can store it in a variable and use it later
  • This becomes the basic frame of our game

Declaring Variables (Variable Declaration)

Code (3), (4), (5), we create variables.

What is a variable?

A variable is like a box that holds information. You use variables when you want to store a player's score in a game, or remember the size of a cube.

my_name = "Student"      # String variable
cube_size = 2            # Number variable
cube_color = color.red   # Color variable

Each variable: - my_name: stores the string "Student" - cube_size: stores the number 2 (the size of the cube) - cube_color: stores the color red (the color of the cube)


Console Output (Console Output)

Code (6), (7)in step print() function.

What is a function?

A function is like a bundle of the features we want. print() function is made by bundling together the feature that prints text to the screen.

print("Hello, Ursina!")
print(f"My name is {my_name}")
  • The first print() prints "Hello, Ursina!" to the console
  • The second print() uses an f-string. {my_name} part gets replaced with "Student"

Creating a 3D Cube (Creating 3D Cube)

Code (8)in step Entity() to create a 3D cube.

my_cube = Entity(
    model='cube',
    color=cube_color,
    scale=cube_size
)

Key concept: - Entity() is the function that creates a 3D object - model='cube' sets the cube shape - color=cube_color uses the variable we made earlier cube_color (red) - scale=cube_size uses the variable we made earlier cube_size (2) is used to make the size twice as big


Running the Game (Running the Game)

Code (9), line app.run() runs the game.

app.run()
  • It runs the game window and displays it on the screen
  • Without this line, no matter how much code you write, the window won't appear!
  • The game loop starts and keeps running until the user closes the window