1
1
from ursina import *
2
2
from ursina .prefabs .first_person_controller import FirstPersonController
3
+ import pickle # added
4
+ import os # added
3
5
4
6
app = Ursina ()
5
7
6
- grass_texture = load_texture ("assets/grass.png" )
7
- soil_texture = load_texture ("assets/soil.png" )
8
- stone_texture = load_texture ("assets/stone.png" )
9
- wood_texture = load_texture ("assets/wood.png" )
8
+ #removed load_texture
9
+ grass_texture = "assets/grass.png"
10
+ soil_texture = "assets/soil.png"
11
+ stone_texture = "assets/stone.png"
12
+ wood_texture = "assets/wood.png"
10
13
11
14
sky_texture = load_texture ("assets/sky.png" )
12
15
@@ -19,6 +22,10 @@ def update():
19
22
if held_keys ['3' ]: current_texture = stone_texture
20
23
if held_keys ['4' ]: current_texture = wood_texture
21
24
25
+ # added
26
+ if held_keys ['g' ]:
27
+ save_game ()
28
+
22
29
if held_keys ['left mouse' ] or held_keys ['right mouse' ]:
23
30
hand .active ()
24
31
else :
@@ -57,6 +64,9 @@ def passive(self):
57
64
self .position = Vec2 (0.4 , - 0.4 )
58
65
59
66
67
+
68
+ game_data = []
69
+
60
70
class Voxel (Button ):
61
71
def __init__ (self , position = (0 , 0 , 0 ), texture = grass_texture ):
62
72
super ().__init__ (
@@ -73,16 +83,37 @@ def input(self, key):
73
83
if self .hovered :
74
84
if key == "left mouse down" :
75
85
voxel = Voxel (position = self .position + mouse .normal , texture = current_texture )
86
+ pos = self .position + mouse .normal
87
+ game_data .append ([(pos .x ,pos .y ,pos .z ),current_texture ])
76
88
if key == 'right mouse down' :
77
89
destroy (self )
78
90
79
91
80
- for z in range (15 ):
81
- for x in range (15 ):
82
- voxel = Voxel ((x , 0 , z ))
92
+
93
+ def save_game ():
94
+ with open ("game_stage.pickle" , "wb" ) as file_ :
95
+ pickle .dump (game_data , file_ , - 1 )
96
+
97
+ def load_basic_game ():
98
+ for z in range (- 20 ,15 ):
99
+ for x in range (- 20 ,15 ):
100
+ voxel = Voxel ((x , 0 , z ),texture = grass_texture )
101
+ game_data .append ([(x ,0 ,z ),grass_texture ])
102
+
103
+ def load_saved_game ():
104
+ saved_game = pickle .load (open ("game_stage.pickle" , "rb" , - 1 ))
105
+ for data in saved_game :
106
+ voxel = Voxel (data [0 ],data [1 ])
107
+
108
+ if os .path .isfile ("game_stage.pickle" ):
109
+ load_saved_game ()
110
+ else :
111
+ load_basic_game ()
112
+ save_game ()
83
113
84
114
player = FirstPersonController ()
85
115
sky = Sky ()
86
116
hand = Hand ()
87
117
118
+
88
119
app .run ()
0 commit comments