Basic 3-D game using Python

In this post we will discuss how to create a 3-D Minecraft like game using python, we will be using ursina package of python for this project , lets start by installing the package

Package Installation

 install ursina

Code Explanation

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

we have firstly imported all the components from ursina package, then we specifically need first_person_controller class from ursina.prefab to give us first person view of the player

set=Ursina()

Sky(texture="sky_default")
player=FirstPersonController()

blocks=[]

now we will create class instance for ursina and the variable name will be set, after this we will set texture to our sky, image used for this is sky_default ,then we will assign our player view to be first person view

for i in range (20):
    for j in range(20):
        block=Button(
            color=color.white,
            model='cube',
            position=(j,0,i),
            texture='brick',
            parent=scene,
            origin_y=0.5)
        blocks.append(block)

Lets create platform for player where it can move, jump and do all kind of things , we will start off by creating two for loops with variables i,j these variables will be used as x and z coordinates of our plane also y is fixed so we don’t need to define it ,our platform is in the form of buttons ,each button is a cube of color white, position is increased after each loop to create a new button as block, texture of image is brick , then with all of this info block is added to a list blocks , now after both the loops are completed blocks variable will have all the data from creating platform , after creating background and platform lets create our plater and define movement to it

player = Entity(
    model = 'cube' ,
    color = color.black,
    scale_y = 2
    )

player.x += held_keys['d'] * .1
player.x -= held_keys['a'] * .1

Player is an entity so we will declare that first, then our player is of black color, model is cube and scale or size of cube is 2 unit, after creating our player we will define movement if we hold ‘d’ key player will move right , left on ‘a’, ‘w’ for up, ‘s’ for down and space for jump, now lets go the final thing and give our player some weapon , for now this weapon wont do anything but we will add functionality to it in further posts

sword = Entity(
parent=camera.ui,
model='cube',
texture = 'bow_arrow.png',
position = Vec2(0.406, -0.42))

weapon is an entity it is attached to camera so that it can move with user , model is cube and image used is bow_arrow.png, we will also define relative position of weapon and this is it now lets run our setup and let me display full code for you

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController


set=Ursina()

Sky(texture="sky_default")
player=FirstPersonController()

blocks=[]


player = Entity(
    model = 'cube' ,
    color = color.black,
    scale_y = 2
    )

player.x += held_keys['d'] * .1
player.x -= held_keys['a'] * .1

sword = Entity(
    parent=camera.ui,
    model='cube',
    texture = 'bow_arrow.png',
    position = Vec2(0.406, -0.42))

for i in range (20):
    for j in range(20):
        block=Button(
            color=color.white,
            model='cube',
            position=(j,0,i),
            texture='brick',
            parent=scene,
            origin_y=0.5)
        blocks.append(block)



set.run()    

soo there you goo a functioning 3 d game using python

About the author

Harshit

Hey, I'am Harshit Roy, a programmer with an obsession of learning new things, this blog is dedicated to helping people to learn programming fun way by creating projects

View all posts

1 Comment