Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9982cdf

Browse files
Save Game Update
1 parent 5621fc4 commit 9982cdf

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

‎main.py‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from ursina import *
22
from ursina.prefabs.first_person_controller import FirstPersonController
3+
import pickle # added
4+
import os # added
35

46
app = Ursina()
57

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"
1013

1114
sky_texture = load_texture("assets/sky.png")
1215

@@ -19,6 +22,10 @@ def update():
1922
if held_keys['3']: current_texture = stone_texture
2023
if held_keys['4']: current_texture = wood_texture
2124

25+
# added
26+
if held_keys['g']:
27+
save_game()
28+
2229
if held_keys['left mouse'] or held_keys['right mouse']:
2330
hand.active()
2431
else:
@@ -57,6 +64,9 @@ def passive(self):
5764
self.position = Vec2(0.4, -0.4)
5865

5966

67+
68+
game_data = []
69+
6070
class Voxel(Button):
6171
def __init__(self, position=(0, 0, 0), texture=grass_texture):
6272
super().__init__(
@@ -73,16 +83,37 @@ def input(self, key):
7383
if self.hovered:
7484
if key == "left mouse down":
7585
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])
7688
if key == 'right mouse down':
7789
destroy(self)
7890

7991

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()
83113

84114
player = FirstPersonController()
85115
sky = Sky()
86116
hand = Hand()
87117

118+
88119
app.run()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /