-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain_v2.py
More file actions
96 lines (78 loc) · 2.56 KB
/
main_v2.py
File metadata and controls
96 lines (78 loc) · 2.56 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
from ursina import Ursina, scene, Entity, load_texture, Button, color, mouse, destroy, held_keys, camera, Vec2, Vec3
from ursina.prefabs.first_person_controller import FirstPersonController
import pickle
import os
import uuid
app = Ursina()
grass_texture = "assets/grass.png"
soil_texture = "assets/soil.png"
stone_texture = "assets/stone.png"
wood_texture = "assets/wood.png"
current_texture = grass_texture
game_data = {}
def save_game():
with open("game_stage.pickle","wb") as f:
pickle.dump(game_data, f)
def load_game():
global game_data
with open("game_stage.pickle","rb") as f:
stored_dict = pickle.load(f)
game_data = {}
for block_id, content, in stored_dict.items():
Voxel(
position=content['pos'],
texture=content['tex'],
block_id=block_id
)
def update():
global current_texture
if held_keys['1']: current_texture = grass_texture
if held_keys['2']: current_texture = stone_texture
if held_keys['3']: current_texture = soil_texture
if held_keys['4']: current_texture = wood_texture
if held_keys['q']: exit(1)
if held_keys["g"]: save_game()
class Sky(Entity):
def __init__(self):
super().__init__(
parent=scene,
model='sphere',
scale=150,
texture=load_texture("assets/sky.png"),
double_sided=True
)
class Voxel(Button):
def __init__(self, position=(0,0,0), texture=current_texture, block_id = None):
self.id = block_id if block_id else str(uuid.uuid4())
super().__init__(
parent=scene,
model='cube',
color=color.white,
texture=texture,
position=position,
origin_y=0.5
)
game_data[self.id] = {
"pos": tuple(self.position),
"tex": current_texture
}
def input(self, key):
if self.hovered:
if key == "left mouse down":
Voxel(position=(self.position + mouse.normal), texture=current_texture)
if key == "right mouse down":
del game_data[self.id]
destroy(self)
class Hand(Entity):
def __init__(self):
super().__init__(parent=camera.ui, model='cube', scale=(0.2,0.3),position=Vec2(0.4,-0.4), rotation=Vec3(150,-10.0) )
sky = Sky()
player = FirstPersonController()
hand = Hand()
if os.path.exists("game_stage.pickle"):
load_game()
else:
for z in range(20):
for x in range(20):
Voxel(position=(x,0,z))
app.run()