1
1
from ursina import *
2
2
from ursina .prefabs .first_person_controller import FirstPersonController
3
- # from ursina.prefabs.editor_camera import EditorCamera
4
3
from perlin_noise import PerlinNoise
5
4
from ursina .shaders import lit_with_shadows_shader
6
5
import random
7
- import sys
8
- import math
9
6
import json
7
+ import psutil
10
8
from ursina .prefabs .panel import Panel
11
9
12
-
13
-
10
+ #Initializing Game Object
14
11
app = Ursina ()
15
12
13
+ #Creating the Player Object
16
14
player = FirstPersonController ()
15
+ player .height = 2
16
+ player .cursor = Entity (parent = camera .ui , model = 'quad' ,color = color .light_gray , scale = .008 , rotation_z = 45 )
17
+ player .gravity = 0.5
18
+ # player.model = "assets/player.obj"
19
+ # player.texture = "textures/"
20
+ player .scale = 1
21
+ player .y = 50
17
22
23
+ # Window Settings
24
+ window .title = "Minecraft Python Edition"
18
25
window .exit_button .visible = False
19
26
window .fullscreen = True
20
27
window .vsync = False
21
28
window .borderless = False
22
- window .color = color .rgb (0 ,181 ,226 )
29
+ window .color = color .rgb (0 , 181 , 226 )
23
30
window .show_ursina_splash = True
24
- window .title = "Minecraft Python Edition"
25
- player .height = 2
26
- player .cursor = Entity (parent = camera .ui , model = 'crosshair' ,
27
- color = color .light_gray , scale = .008 , rotation_z = 45 )
28
- player .gravity = 0.5
29
- # player.model = "assets/player.obj"
30
- # player.texture = "textures/"
31
+
32
+ # Init Variables
31
33
terrainblocks = []
32
- player . scale = 1
33
- player . y = 50
34
+ button_font = "font/minecraft.ttf"
35
+ pressed = False
34
36
block_id = 1
35
- o = Panel (scale = 5 )
36
- o .visible = False
37
- inventorytrue = "i"
38
37
bgmusic = [Audio ("assets/music/calm1.ogg" , loop = True , autoplay = False ),
39
38
Audio ("assets/music/calm2.ogg" , loop = True , autoplay = False ),
40
39
Audio ("assets/music/calm3.ogg" , loop = True , autoplay = False ),
44
43
]
45
44
bgumusic = random .choice (bgmusic )
46
45
bgumusic .play ()
47
- # bgmusic.autoplay = True
48
- voxels = []
49
46
noise = PerlinNoise (octaves = 3 ,seed = random .randint (1 ,1000000 ))
50
-
51
- terrain = Entity (model = None ,collider = None )
52
-
53
47
blocks = [
54
48
['leaves' , "assets/leaves_block_tex.png" , load_texture ("assets/leaves_block_tex.png" )],
55
49
["grass" , "assets/grass_block_tex.png" , load_texture ("assets/grass_block_tex.png" )],
63
57
["ice" , "assets/ice_block_tex.png" , load_texture ("assets/ice_block_tex.png" )]
64
58
]
65
59
60
+ # Variables for F3 debug screen
61
+ debugscreen = Entity (model = None , parent = camera .ui )
62
+ debugscreen .visible = False
63
+ coordinates = Text (position = Vec3 (- .87 , 0.44 , 0 ),
64
+ font = button_font , parent = debugscreen )
65
+ cpu_panel = Text (position = Vec3 (- .87 , 0.40 , 0 ),
66
+ parent = debugscreen , font = button_font )
67
+
68
+ #Importing Json
66
69
with open ("configuration.json" , "r" ) as configuration :
67
70
data = json .load (configuration )
68
71
trees = data ["trees" ]
77
80
if fps_counter_enabled == False :
78
81
window .fps_counter = False
79
82
83
+ #----------------------------------------------Function Space---------------------------------------------------
84
+
85
+ #Defining Tree Structure
80
86
def trunk (parent ):
81
87
for __z in range (1 ):
82
88
for __x in range (1 ):
@@ -103,22 +109,14 @@ def plantTree(_x, _y, _z):
103
109
trunk (tree )
104
110
tree .y = 3
105
111
106
-
107
- def checkTree (_x , _y , _z ):
108
- freq = 3
109
- amp = 80
110
- treeChance = ((noise ([_x / freq , _z / freq ])) * amp )
111
- if treeChance > 38 :
112
- plantTree (_x , _y , _z )
113
-
114
-
115
112
def genTrees ():
116
113
for tree in range (treesCount ):
117
114
chosenblock = random .choice (terrainblocks )
118
115
# terrainblocks.remove(chosenblock)
119
116
randomcoordinates = (chosenblock .x ,chosenblock .z ,chosenblock .y )
120
117
plantTree (_x = randomcoordinates [0 ], _z = randomcoordinates [1 ], _y = randomcoordinates [2 ]+ 1 )
121
118
119
+ # Finding about the blocks
122
120
def findsoundbasedontexture (blockid , mode , block , blocklist = blocks ):
123
121
if mode == "default" :
124
122
if blocklist [blockid ][0 ] in blocklist [blockid ][1 ]:
@@ -138,39 +136,52 @@ def whichblockami(block):
138
136
return eachBlock [0 ]
139
137
140
138
except :
141
- print (f"No texture on index" )
142
-
139
+ print (f"No texture on index" )
140
+
141
+ #------------------------------------End of Function Space-------------------------------------------
142
+
143
+ # Task Update
143
144
def update ():
144
- global inventorytrue
145
+ global inventorytrue , pressed
145
146
if held_keys ['left mouse down' ] or held_keys ['right mouse down' ]:
146
147
# punch_sound.play()
147
148
hand .active ()
148
149
else :
149
150
hand .passive ()
150
151
151
- # if held_keys["e"]:
152
- # application.pause()
153
- # o.visible = True
154
- # inventory.visible = True
155
- # inventory.addslots()
156
- # mouse.locked = False
157
- # mouse.visible = True
158
- # # inventorytrue = "o"
159
152
if held_keys ['escape' ]:
160
153
sys .exit ()
161
154
155
+ if held_keys ['f3' ]:
156
+ if pressed == False :
157
+ debugscreen .visible = True
158
+ pressed = True
159
+ elif pressed == True :
160
+ debugscreen .visible = False
161
+ pressed = False
162
+
162
163
selected .adjust_position ()
163
164
164
165
if held_keys ["t" ]:
165
166
plantTree (round (player .x ), round (player .y ), round (player .z ))
166
167
168
+ cpu = psutil .cpu_percent ()
169
+ ram = psutil .virtual_memory ().percent
170
+ coordinates .text = f'Position: { round (player .x )} ,{ round (player .y )} ,{ round (player .z )} '
171
+ pid = os .getpid ()
172
+ python_process = psutil .Process (pid )
173
+ memoryUse = python_process .memory_info ()[0 ]/ 2. ** 30
174
+
175
+ cpu_panel .text = f'CPU: { cpu } % / RAM: { ram } % / Memory use: { round (memoryUse ,2 )} GB'
176
+
167
177
if player .y < - 100 :
168
178
Audio ("assets/sounds/sh/die.ogg" )
169
179
player .y = 15
170
180
player .x = 0
171
181
player .z = 0
172
182
Audio ("assets/sounds/sh/spawn.ogg" )
173
183
184
+ # Defining Voxel
174
185
class Voxel (Button ):
175
186
def __init__ (self , position = (0 , 0 , 0 ), texture = "assets/grass_block_tex.png" , ** kwargs ):
176
187
super ().__init__ (
@@ -191,12 +202,15 @@ def input(self, key):
191
202
if key == 'right mouse down' :
192
203
if sounds == True :
193
204
findsoundbasedontexture (blockid = block_id ,mode = "default" ,block = self .block ).play ()
194
- Voxel (position = self .position + mouse .normal ,
205
+ voxel = Voxel (position = self .position + mouse .normal ,
195
206
texture = blocks [block_id ][2 ])
207
+ terrainblocks .append (voxel )
196
208
if key == 'left mouse down' :
197
209
if sounds == True :
198
210
findsoundbasedontexture (blockid = block_id ,mode = "already" ,block = self .block ).play ()
199
211
destroy (self )
212
+
213
+ # Changing hand texture
200
214
def input (key ):
201
215
global block_id , hand
202
216
if key .isdigit ():
@@ -205,7 +219,7 @@ def input(key):
205
219
block_id = len (blocks ) - 1
206
220
hand .texture = blocks [block_id ][2 ]
207
221
208
-
222
+ # Defining Hand
209
223
class Hand (Entity ):
210
224
def __init__ (self ):
211
225
super ().__init__ (
@@ -226,6 +240,7 @@ def active():
226
240
def passive ():
227
241
hand .position = Vec2 (0.6 , - 0.6 )
228
242
243
+ #----------------------------------------------Inventory Zone-----------------------------------------
229
244
230
245
class Item (Entity ):
231
246
def __init__ (self ,texture ,position = (0 ,- 0.42 )):
@@ -293,61 +308,31 @@ def appendItems(self):
293
308
obsidian = Item (blocks [8 ][2 ], (0.26 , - 0.42 ))
294
309
ice = Item (blocks [9 ][2 ], (0.35 , - 0.42 ))
295
310
296
- # for z in range(-20,20):
297
- # for x in range(-20,20):
298
- # y = noise([x * .02, z * .02])
299
- # y = math.floor(y * 7.5)
300
- # voxel = Voxel(position=(x,y,z))
301
- # voxel.texture = blocks[1][2]
302
- # voxel.parent = terrain
303
- # terrainblocks.append(voxel)
311
+ #-------------------------------------End of Inventory Zone-------------------------------------------
312
+
313
+ # Terrain Generation
304
314
terrainWidth = landsize
305
315
freq = 24
306
316
if parkour == True :
307
317
amp = 100
308
318
else :
309
- amp = 5
319
+ amp = 6
310
320
for i in range (terrainWidth * terrainWidth ):
311
321
voxel = Voxel (texture = blocks [1 ][2 ])
312
322
voxel .x = floor (i / terrainWidth )
313
323
voxel .z = floor (i % terrainWidth )
314
324
voxel .y = floor ((noise ([voxel .x / freq , voxel .z / freq ]))* amp )
315
325
# voxel.parent = terrain
316
326
terrainblocks .append (voxel )
317
-
318
-
319
- # for b in range(1):
320
- # for i in range(terrainWidth*terrainWidth):
321
- # voxel = Voxel(texture=blocks[2][2])
322
- # voxel.x = floor(i/terrainWidth)
323
- # voxel.z = floor(i % terrainWidth)
324
- # voxel.y = floor(((noise([voxel.x/freq, voxel.z/freq]))*amp)-(b+1))
325
-
326
- # for d in range(1):
327
- # for i in range(terrainWidth*terrainWidth):
328
- # voxel = Voxel(texture=blocks[3][2])
329
- # voxel.x = floor(i/terrainWidth)
330
- # voxel.z = floor(i % terrainWidth)
331
- # voxel.y = floor(((noise([voxel.x/freq, voxel.z/freq]))*amp)-(d+2))
332
327
333
328
if trees == True :
334
329
genTrees ()
335
-
336
- # terrain.combine(terrainblocks)
337
- # print("Mesh combined successfully")
338
- # terrain.collider = 'mesh'
339
- # terrain.texture = "white_cube"
340
-
330
+
341
331
if directionalshaders == True :
342
332
DirectionalLight (parent = Voxel , y = 2 , z = 3 , shadows = True )
343
333
344
- if inventory == True :
345
- hotbar = Hotbar ()
346
- hotbar .appendItems ()
347
- selected = Selected ()
348
- # inventory = Inventory()
349
- # inventory.visible = False
350
- inventorytrue = "i"
351
334
hand = Hand ()
352
- # chunk = combine(terrainblocks)
335
+ selected = Selected ()
336
+ hotbar = Hotbar ()
337
+ hotbar .appendItems ()
353
338
app .run ()
0 commit comments