Teach you to program a "My World" with Python 1. Get to know Ursina and learn to draw three-dimensional graphics

Python has a nice 3D engine - Ursina

Ursina official website: www.ursinaengine.org


Open cmd, enter pip install ursina in the console to install ursina


write your first program

First import ursina

from ursina import *

Then create the app

app=Ursina()

run app

app.run()

Final code:

from ursina import *

app=Ursina()

app.run()

If a gray window appears, it means the operation was successful!


Draw solid cuboid

Drawing entities requires a function:

Entity()

Because we want to draw a cuboid, set the parameter model="cube"

code show as below:

from ursina import *

app=Ursina()

cube=Entity(model="cube")

app.run()

running result:


Enlarge and reduce the same proportion of entities

We want to enlarge the entire entity by 2 times year-on-year, and pass in the scale parameter in Entity, with a value of 2

code show as below:

from ursina import *

app=Ursina()

cube=Entity(model="cube",scale=2)

app.run()

Effect:


Enlarge the entity arbitrarily

If we want to enlarge the cube by 2 times along the x direction, we need to pass in the scale_x parameter with a value of 2

code:

from ursina import *

app=Ursina()

cube=Entity(model="cube",scale_x=2)

app.run()

Effect:

You can also zoom in along y (height), the code is as follows:

from ursina import *

app=Ursina()

cube=Entity(model="cube",scale_y=2)

app.run()

Effect:

 The scale parameter can also pass in a tuple in the format of (float,float,float), which means that the xyz sides are enlarged by different multiples. The code example is as follows:

from ursina import *

app=Ursina()

cube=Entity(model="cube",scale=(2,3,4))

app.run()

draw sphere

from ursina import *

app=Ursina()

sphere=Entity(model="sphere")

app.run()

Like creating a cube, just change the value of model to sphere

Effect:


color your entities 

Ursina has its own color module. Some commonly used colors can be used through color.color name, or the value of rgb or rgba can be passed in with the color.rgb() function, for example:

from ursina import *

app=Ursina()

sphere=Entity(model="sphere",color=color.red)

app.run()
from ursina import *

app=Ursina()

sphere=Entity(model="sphere",color=color.rgb(255,0,0))

app.run()

After the two pieces of code are run, the effect is the same, and the effect is as follows:


Well, this is the basic knowledge of Ursina. In the next section, we will continue to learn how to set entity materials, how to create button-type entities, how to create blocks in "Minecraft" by inheriting classes, and first-person processing!

You can pay attention to my free column - teach you how to make a "Minecraft", here will continue to update the tutorial, remember to pay attention!

If you like it, please like and follow!

Guess you like

Origin blog.csdn.net/leleprogrammer/article/details/124780527