-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
436 lines (378 loc) · 13.8 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from perlin_noise import PerlinNoise
from ursina.shaders import lit_with_shadows_shader
import random
import json
import psutil
from ursina.prefabs.panel import Panel
#Initializing Game Object
app = Ursina()
#Creating the Player Object
player = FirstPersonController()
player.height = 2
player.cursor = Entity(parent=camera.ui, model='quad',color=color.light_gray, scale=.008, rotation_z=45)
player.gravity = 0.5
player.jump_height = 1
player.fall_after = .35
camera.y -= 0.35
# player.model = "assets/player.obj"
# player.texture = "textures/"
# Window Settings
window.title = "Minecraft Python Edition"
window.exit_button.visible = False
window.fullscreen = True
window.vsync = False
window.borderless = False
window.color = color.rgb(0, 181, 226)
window.show_ursina_splash = True
# Init Variables
terrainblocks = []
button_font = "font/minecraft.ttf"
pressed = False
block_id = 1
bgmusic = [Audio("assets/music/calm1.ogg", loop=True, autoplay=False),
Audio("assets/music/calm2.ogg", loop=True, autoplay=False),
Audio("assets/music/calm3.ogg", loop=True, autoplay=False),
Audio("assets/music/hal1.ogg", loop=True, autoplay=False),
Audio("assets/music/hal3.ogg", loop=True, autoplay=False),
Audio("assets/music/hal4.ogg", loop=True, autoplay=False),
]
bgumusic = random.choice(bgmusic)
bgumusic.play()
noise = PerlinNoise(octaves=3,seed=random.randint(1,1000000))
blocks = [
['leaves', "assets/leaves_block_tex.png", load_texture("assets/leaves_block_tex.png")],
["grass", "assets/grass_block_tex.png", load_texture("assets/grass_block_tex.png")],
["dirt", "assets/dirt_block_tex.png", load_texture("assets/dirt_block_tex.png")],
["stone", "assets/stone_block_tex.png", load_texture("assets/stone_block_tex.png")],
["cobblestone", "assets/cobblestone_block_tex.png", load_texture("assets/cobblestone_block_tex.png")],
["sand", "assets/sand_block_tex.png", load_texture("assets/sand_block_tex.png")],
["oak", "assets/oak_block_tex.png", load_texture("assets/oak_block_tex.png")],
["planks", "assets/planks_block_tex.png", load_texture("assets/planks_block_tex.png")],
["obsidian", "assets/obsidian_block_tex.png", load_texture("assets/obsidian_block_tex.png")],
["ice", "assets/ice_block_tex.png", load_texture("assets/ice_block_tex.png")]
]
# Variables for F3 debug screen
debugscreen = Entity(model=None, parent=camera.ui)
debugscreen.visible = False
coordinates = Text(position=Vec3(-.87, 0.44, 0),
font=button_font, parent=debugscreen)
cpu_panel = Text(position=Vec3(-.87, 0.40, 0),
parent=debugscreen, font=button_font)
#Importing Json
with open("configuration.json", "r") as configuration:
data = json.load(configuration)
trees = data["trees"]
inventory = data["showInventory"]
sounds = data["sounds"]
directionalshaders = data["directionalShaders"]
treesCount = data["treesCount"]
fps_counter_enabled = data["fps_counter_enabled"]
parkour = data["parkour_mode"]
landsize = data["land_size"]
world_type = data["world_type"]
defaultblock = data["default_block"]
if fps_counter_enabled == False:
window.fps_counter = False
#----------------------------------------------Function Space---------------------------------------------------
#Defining Tree Structure
def trunk(parent):
for __z in range(1):
for __x in range(1):
for __y in range(3):
voxel = Voxel(position=(__x, __y - 3, __z), texture=blocks[6][2])
voxel.parent = parent
for ___z in range(5):
for ___x in range(5):
for ___y in range(2):
voxel = Voxel(position=(___x - 2, ___y, ___z - 2),
texture='assets/leaves_block_tex.png')
voxel.parent = parent
for ____z in range(3):
for ____x in range(3):
for ____y in range(2):
voxel = Voxel(position=(____x - 1, ____y+2, ____z - 1),
texture='assets/leaves_block_tex.png')
voxel.parent = parent
def plantTree(_x, _y, _z):
tree = Entity(model=None, position=Vec3(_x, _y, _z))
trunk(tree)
tree.y = 3
def genTrees():
for tree in range(treesCount):
chosenblock = random.choice(terrainblocks)
# terrainblocks.remove(chosenblock)
randomcoordinates = (chosenblock.x,chosenblock.z,chosenblock.y)
plantTree(_x=randomcoordinates[0], _z=randomcoordinates[1], _y=randomcoordinates[2]+1)
# Finding about the blocks
def findsoundbasedontexture(blockid, mode, block, blocklist=blocks):
if mode == "default":
if blocklist[blockid][0] in blocklist[blockid][1]:
sound = Audio(f"assets/sounds/{blocklist[blockid][0]}/{blocklist[blockid][0]}{random.randint(1, 4)}", loop=False, autoplay=False)
return sound
elif mode == "already":
for blockk in blocklist:
if blockk[0] == block:
soundd = Audio(f"assets/sounds/{block}/{block}{random.randint(1,4)}", loop=False, autoplay=False)
return soundd
def whichblockami(block):
for eachBlock in blocks:
try:
if f"assets/{block.texture.name}" in eachBlock[1]:
return eachBlock[0]
except:
print(f"No texture on index")
# Sneaking
def sneak():
camera.y = -0.6
player.speed = 1
# Sprinting
def sprint():
camera.fov = 100
player.speed = 10
player.jumping = True
# Zooming
def zoom():
camera.fov = 70
# Default Function
def default():
player.speed = 5
camera.fov = 90
camera.y = -0.35
#------------------------------------End of Function Space-------------------------------------------
# Task Update
def update():
global inventorytrue, pressed, items
if held_keys['left mouse down'] or held_keys['right mouse down']:
# punch_sound.play()
hand.active()
else:
hand.passive()
if held_keys["shift"]:
sneak()
elif held_keys["control"] and held_keys["w"]:
sprint()
elif held_keys["c"]:
zoom()
else:
default()
if held_keys['escape']:
sys.exit()
if held_keys['f3']:
if pressed == False:
debugscreen.visible = True
pressed = True
elif pressed == True:
debugscreen.visible = False
pressed = False
selected.adjust_position()
if held_keys["t"]:
plantTree(round(player.x), round(player.y), round(player.z))
cpu = psutil.cpu_percent()
ram = psutil.virtual_memory().percent
coordinates.text = f'Position: {round(player.x)},{round(player.y)},{round(player.z)}'
pid = os.getpid()
python_process = psutil.Process(pid)
memoryUse = python_process.memory_info()[0]/2.**30
cpu_panel.text = f'CPU: {cpu}% / RAM: {ram}% / Memory use: {round(memoryUse,2)} GB'
if player.y < -100:
Audio("assets/sounds/sh/die.ogg")
player.y = 15
player.x = 0
player.z = 0
Audio("assets/sounds/sh/spawn.ogg")
# if player.y < -100:
# Audio("assets/sounds/sh/die.ogg")
# application.pause()
# o.visible = True
# dietext.visible = True
# items = False
# hotbar.destroyItems()
# hotbar.visible = False
# hand.visible = False
# selected.visible = False
# respawn_button = RespawnButton()
# respawn_button.text_entity.font = button_font
# mouse.locked = False
# Defining Voxel
class Voxel(Button):
def __init__(self, position=(0, 0, 0), texture=f"assets/{defaultblock}_block_tex.png", **kwargs):
super().__init__(
parent=scene,
position=position,
model="assets/block",
origin_y=0.5,
texture=texture,
color=color.color(0, 0, random.uniform(0.9, 1)),
scale=1,
shader=lit_with_shadows_shader,
collider = "box"
)
self.block = whichblockami(self)
def input(self, key):
if self.hovered:
if key == 'right mouse down':
if sounds == True:
findsoundbasedontexture(blockid=block_id,mode="default",block=self.block).play()
voxel = Voxel(position=self.position+mouse.normal,
texture=blocks[block_id][2])
terrainblocks.append(voxel)
if key == 'left mouse down':
if sounds == True:
findsoundbasedontexture(blockid=block_id,mode="already",block=self.block).play()
destroy(self)
# Changing hand texture
def input(key):
global block_id, hand
if key.isdigit():
block_id = int(key)
if block_id >= len(blocks):
block_id = len(blocks) - 1
hand.texture = blocks[block_id][2]
# Death Screen
o = Panel(scale=4, color=color.rgba(255, 0, 0, 200))
o.visible = False
items = True
dietext = Text(font=button_font, text="You Died!",
position=Vec2(-0.1, 0.2), color=color.white, scale=2)
dietext.visible = False
class RespawnButton(Button):
def __init__(self):
super().__init__(
text="Respawn",
parent=camera.ui,
model="quad",
texture="textures/widgets/button.png",
color=color.color(0, 0, random.uniform(0.9, 1)),
scale=(0.5, 0.1)
)
def input(self, key):
if self.hovered:
self.texture = "textures/widgets/button_selected.png"
if key == "left mouse down":
dietext.visible = False
application.resume()
player.y = 15
player.x = 0
player.z = 0
Audio("assets/sounds/sh/spawn.ogg")
destroy(self)
# Defining Hand
class Hand(Entity):
def __init__(self):
super().__init__(
parent=camera.ui,
model='assets/block',
texture=blocks[block_id][2],
scale=0.3,
shader=lit_with_shadows_shader,
rotation=Vec3(-10, -10, 10),
position=Vec2(-0.6, -0.6)
)
@staticmethod
def active():
hand.position = Vec2(0.4, -0.5)
@staticmethod
def passive():
hand.position = Vec2(0.6, -0.6)
#----------------------------------------------Inventory Zone-----------------------------------------
class Item(Entity):
def __init__(self,texture,position=(0,-0.42)):
super().__init__(
parent=camera.ui,
model="assets/block",
texture=texture,
scale=0.04,
position=Vec2(position),
rotation=Vec3(-10,-35,-10),
shader=lit_with_shadows_shader,
)
class Selected(Entity):
def __init__(self, position=(0, -0.4)):
super().__init__(
parent=camera.ui,
model="quad",
texture="textures/widgets/selected.png",
scale=0.1,
position=Vec2(position),
# rotation=Vec3(-10, -35, -10),
# shader=basic_lighting_shader,
)
def adjust_position(self):
if block_id == 1:
self.position = Vec2(-0.35,-0.4)
if block_id == 2:
self.position = Vec2(-0.26, -0.4)
if block_id == 3:
self.position = Vec2(-0.17, -0.4)
if block_id == 4:
self.position = Vec2(-0.08, -0.4)
if block_id == 5:
self.position = Vec2(0, -0.4)
if block_id == 6:
self.position = Vec2(0.08, -0.4)
if block_id == 7:
self.position = Vec2(0.17, -0.4)
if block_id == 8:
self.position = Vec2(0.26, -0.4)
if block_id == 9:
self.position = Vec2(0.35, -0.4)
class Hotbar(Entity):
def __init__(self):
super().__init__(
parent=camera.ui,
model='quad',
scale=(0.8, 0.10, 0),
position=Vec2(0, -0.4),
texture='textures/widgets/hotbar.png'
)
def appendItems(self):
self.grass = Item(blocks[1][2],(-0.35,-0.42))
self.dirt = Item(blocks[2][2],(-0.26,-0.42))
self.stone = Item(blocks[3][2],(-0.17, -0.42))
self.cobblestone = Item(blocks[4][2],(-0.08,-0.42))
self.sand = Item(blocks[5][2],(0,-0.42))
self.oak = Item(blocks[6][2],(0.08,-0.42))
self.planks = Item(blocks[7][2],(0.17,-0.42))
self.obsidian = Item(blocks[8][2], (0.26, -0.42))
self.ice = Item(blocks[9][2], (0.35, -0.42))
def destroyItems(self):
if items == False:
self.grass.visible = False
self.dirt.visible = False
self.stone.visible = False
self.cobblestone.visible = False
self.sand.visible = False
self.oak.visible = False
self.planks.visible = False
self.obsidian.visible = False
self.ice.visible = False
#-------------------------------------End of Inventory Zone-------------------------------------------
# Terrain Generation
terrainWidth = landsize
freq = 24
if parkour == True:
amp = 100
else:
amp = 5
if world_type == "super_flat":
amp = 0
else:
amp = 5
for i in range(terrainWidth*terrainWidth):
voxel = Voxel(texture=blocks[1][2])
voxel.x = floor(i/terrainWidth)
voxel.z = floor(i % terrainWidth)
voxel.y = floor((noise([voxel.x/freq, voxel.z/freq]))*amp)
# voxel.parent = terrain
terrainblocks.append(voxel)
if trees == True:
genTrees()
if directionalshaders == True:
DirectionalLight(parent=Voxel, y=2, z=3, shadows=True)
hand = Hand()
hotbar = Hotbar()
selected = Selected()
hotbar.appendItems()
app.run()