-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMinecraft.py
206 lines (167 loc) · 6.71 KB
/
Minecraft.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina import *
import ursina,time
app = Ursina()
player_enabled = True
p_key_held = False
original_world = []
world_size = 20 # x and y
world_depth = 5 # z
#camera.orthographic = True
grass_texture = load_texture('assets/grass_block2.png')
stone_texture = load_texture('assets/stone_block2.png')
wood_texture = load_texture('assets/wood_block.png')
brick_texture = load_texture('assets/brick_block.png')
dirt_texture = load_texture('assets/dirt_block.png')
sky_texture = load_texture('assets/skybox.png')
arm_texture = load_texture('assets/arm_texture2.png')
punch_sound = Audio('assets/punch_sound', loop=False, autoplay=False)
block_pick = 1
window.fps_counter.enabled = True
window.exit_button.visible = True
def show_popup(text):
global popup_text
popup_text = Text(text=text, origin=(0, 0), scale=2,color=color.black)
popup_text.x = -popup_text.width / 2
popup_text.y = -popup_text.height / 2
def hide_popup():
global popup_text
destroy(popup_text)
def reset_game():
show_popup("Recreated World blocks !")
for voxel in scene.entities:
if isinstance(voxel, Voxel):
voxel.disable() # Disable current voxels
for x, y, z, texture in original_world:
voxel = Voxel(position=(x, y, z), texture=texture) # Recreate voxels
player.position = (12, 4, 12) # Reset player's position
invoke(hide_popup, delay=4) # Hide popup after 3 seconds
def toggle_player_visibility():
global player_enabled
player_enabled = not player_enabled
player.enabled = player_enabled
def update(self):
global block_pick
global p_key_held
if player.y < -10: # Check if player fell off the edge
reset_game()
if held_keys['r']: # Press 'r' key to reset the game
reset_game()
if held_keys['p'] and not p_key_held:
toggle_player_visibility()
p_key_held = True
elif not held_keys['p'] and p_key_held:
p_key_held = False
if held_keys['left mouse'] or held_keys['right mouse']:
hand.active()
else:
hand.passive()
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
class Voxel(Button):
def __init__(self, position=(0, 0, 0), texture=grass_texture):
super().__init__(
parent=scene,
position=position,
model='assets/block',
origin_y=0.5,
texture=texture,
color=color.color(0, 0, 1),
scale=0.5
)
self.default_color = self.color
def on_mouse_enter(self):
self.color = color.color(19, 0.03, 0.7)
def on_mouse_exit(self):
self.color = self.default_color
def input(self, key):
if key == 'escape':
application.quit()
if self.hovered:
if key == 'right mouse down':
punch_sound.play()
if block_pick == 1: voxel = Voxel(position=self.position + mouse.normal, texture=grass_texture)
if block_pick == 2: voxel = Voxel(position=self.position + mouse.normal, texture=stone_texture)
if block_pick == 3: voxel = Voxel(position=self.position + mouse.normal, texture=brick_texture)
if block_pick == 4: voxel = Voxel(position=self.position + mouse.normal, texture=dirt_texture)
if block_pick == 5: voxel = Voxel(position=self.position + mouse.normal, texture=wood_texture)
if key == 'left mouse down':
punch_sound.play()
destroy(self)
class NonInteractiveButton(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.highlight_color = self.color
self.collision = False
class TableUI(Entity):
def __init__(self):
super().__init__(parent=camera.ui)
cell_size = 0.08 # Size of each cell
spacing = 0.02 # Spacing between cells
self.cells = []
for i in range(9):
if i <= 4:
cell = NonInteractiveButton(
parent=self,
model='quad',
color=color.rgba(1, 1, 1, 0.9),
texture=["assets/grass3d.png","assets/Stone3d.png","assets/Brick3d.png","assets/Dirt3d.png","assets/plank3d.png"][i],
border=0.02,
scale=(cell_size, cell_size), # Cells are square now
origin=(-0.5, 0),
position=(-0.43 + i * (cell_size + spacing), -0.42)) , # Adjust positions
text_entity = Text(parent=cell, text=str(i + 1), position=(-0.43 + i * (cell_size + spacing), -0.382))
else:
cell = NonInteractiveButton(
parent=self,
model='quad',
border=0.02,
scale=(cell_size, cell_size), # Cells are square now
origin=(-0.5, 0),
position=(-0.43 + i * (cell_size + spacing), -0.42)) ,# Adjust positions
text_entity = Text(parent=cell, text=str(i + 1), position=(-0.43 + i * (cell_size + spacing), -0.382))
self.cells.append(cell)
class Sky(Entity):
def __init__(self):
super().__init__(
parent=scene,
model='sphere',
texture=sky_texture,
scale=150,
double_sided=True
)
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.4, -0.6)
)
def active(self):
self.position = Vec2(0.3, -0.5)
def passive(self):
self.position = Vec2(0.4, -0.6)
for z in range(world_size):
for x in range(world_size):
for y in range(world_depth):
if y == 4:
voxel = Voxel(position=(x, y, z), texture=grass_texture)
original_world.append((x, y, z, grass_texture))
elif y == 0:
voxel = Voxel(position=(x, y, z), texture=stone_texture)
original_world.append((x, y, z, stone_texture))
else:
voxel = Voxel(position=(x, y, z), texture=dirt_texture)
original_world.append((x, y, z, dirt_texture))
player = FirstPersonController(position=(12,15,12))
table = TableUI()
sky = Sky()
hand = Hand()
window.fullscreen = True
app.run()