-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (104 loc) · 3.87 KB
/
Copy pathmain.py
File metadata and controls
120 lines (104 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
app = Ursina()
# Load block textures
grass_texture = load_texture('assets/grass_block.png')
stone_texture = load_texture('assets/stone_block.png')
wood_texture = load_texture('assets/wood_block.png')
dirt_texture = load_texture('assets/dirt_block.png')
bedrock_texture = load_texture('assets/bedrock_block.png')
leaves_texture = load_texture('assets/leaves_block.png')
sky_texture = load_texture('assets/sky.png')
arm_texture = load_texture('assets/arm_texture.png')
block_build = Audio('assets/block_build.ogg', loop=False, autoplay=False)
block_break = Audio('assets/block_break.ogg', loop=False, autoplay=False)
block_pick = 1
window.fps_counter.enabled = True
def update():
global block_pick
# Handle keyboard events
if held_keys['left mouse'] or held_keys['right mouse']:
hand.active()
else:
hand.passive()
if held_keys['escape']: app.quit()
if held_keys['1']: block_pick = 1
if held_keys['2']: block_pick = 2
if held_keys['3']: block_pick = 3
if held_keys['4']: block_pick = 4
if held_keys['5']: block_pick = 5
if held_keys['6']: block_pick = 6
# Class representing a single block
class Voxel(Button):
def __init__(self, position=(0, 0, 0), textures=grass_texture):
super().__init__(
parent=scene,
position=position,
model='assets/block',
origin_y=0.5,
texture=textures,
color=color.color(0, 0, random.uniform(0.9, 1)),
highlight_color=color.light_gray,
scale=0.5
)
def input(self, key):
if self.hovered:
# Handle mouse events
if key == 'left mouse down':
block_build.play()
# Create a block based on the selected type
if block_pick == 1: Voxel(position=self.position + mouse.normal, textures=grass_texture)
if block_pick == 2: Voxel(position=self.position + mouse.normal, textures=dirt_texture)
if block_pick == 3: Voxel(position=self.position + mouse.normal, textures=stone_texture)
if block_pick == 4: Voxel(position=self.position + mouse.normal, textures=wood_texture)
if block_pick == 5: Voxel(position=self.position + mouse.normal, textures=bedrock_texture)
if block_pick == 6: Voxel(position=self.position + mouse.normal, textures=leaves_texture)
elif key == 'right mouse down':
block_break.play()
destroy(self)
# Class representing the sky
class Sky(Entity):
def __init__(self):
super().__init__(
parent=scene,
model='sphere',
texture=sky_texture,
scale=250,
double_sided=True,
)
# Class representing the player's hand
class Hand(Entity):
def __init__(self):
super().__init__(
parent=camera.ui,
model='assets/arm',
texture=arm_texture,
scale=0.2,
rotation=Vec3(150, -10, 0),
position=Vec2(0.5, -0.5)
)
def active(self):
self.position = Vec2(0.3, -0.5)
def passive(self):
self.position = Vec2(0.4, -0.6)
# Function for generating terrain from blocks
def terrain(num_x=10, num_y=0, num_z=10):
for x in range(num_x):
for z in range(num_z):
Voxel(position=(x, 0, z), textures=grass_texture)
for y in range(num_y):
Voxel(position=(x, -y - 1, z), textures=stone_texture)
if y == 0:
Voxel(position=(x, y - num_y - 1, z), textures=bedrock_texture)
# Initialize the player and generate terrain
player = FirstPersonController(
position=(5, 10, 5),
speed=4,
height=1,
gravity=0.5,
jump_height=1.5
)
terrain(20, 3, 20)
sky = Sky()
hand = Hand()
app.run()