-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
221 lines (185 loc) · 7.68 KB
/
Copy pathmain.py
File metadata and controls
221 lines (185 loc) · 7.68 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
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
import ensurepackages
ensurepackages.ensurepackages()
import ensureappdir
ensureappdir.ensureappdir()
import pygame
import sys, os
import time
import json
import utils
import titlescr
import seedscr
import savescr
import worldsscr
import creditsscr
import keybindsscr
import settingsscr
import renderer
GAME_BLOCK_SCALE = 24
GAME_WIDTH, GAME_HEIGHT = GAME_BLOCK_SCALE * 64, GAME_BLOCK_SCALE * 32
KEYBINDS = keybindsscr.KEYBINDS
def turn_alphanumeric_to_int(string):
return sum(ord(char) for char in string)
def main():
pygame.init()
screen = pygame.display.set_mode((GAME_WIDTH, GAME_HEIGHT))
pygame.display.set_caption("Mineplace")
utils.SoundManager.checkmixer_online()
settings_file = ""
if os.name == "nt":
settings_file = os.getenv("APPDATA")
else:
settings_file = os.path.expanduser("~")
settings_file = os.path.join(settings_file, "mineplace", "settings.json")
if not os.path.exists(settings_file):
os.makedirs(os.path.dirname(settings_file), exist_ok=True)
with open(settings_file, "w") as f:
f.write(json.dumps({
"volume": 100,
"panorama_blur": 8
}))
else:
with open(settings_file) as f:
settings = json.loads(f.read())
utils.SettingsManager.apply_settings(settings)
titlescreen = titlescr.TitleScreen(screen)
option = titlescreen.run()
if option == "exit":
sys.exit()
if option == "settings":
settingsscreen = settingsscr.SettingsScreen(screen)
settingsscreen.run()
main()
if option == "credits":
creditsscreen = creditsscr.CreditsScreen(screen)
creditsscreen.run()
main()
if option == "keybinds":
keybindsscreen = keybindsscr.KeybindsScreen(screen)
keybindsscreen.run()
main()
if option == "load":
worldsscreen = worldsscr.WorldsScreen(screen)
worldname = worldsscreen.run()
if worldname == "exit":
main()
data = utils.WorldManager.load_world(worldname)
world = data["world"]
renderworld = []
for y in range(len(world)):
row = []
for x in range(len(world[y])):
row.append(world[y][x].copy())
renderworld.append(row)
r = renderer.Renderer(screen, GAME_WIDTH // GAME_BLOCK_SCALE, GAME_HEIGHT // GAME_BLOCK_SCALE, GAME_BLOCK_SCALE)
spawn = utils.Player.find_best_spawn(world)
player = utils.Player(spawn[0], spawn[1])
tick = 0
isNight = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == KEYBINDS.close_menus):
running = False
if event.type == pygame.KEYDOWN:
if event.key == KEYBINDS.prt_sc:
folder_to_save = ""
if os.name == "nt":
folder_to_save = os.getenv("APPDATA")
else:
folder_to_save = os.path.expanduser("~")
folder_to_save = os.path.join(folder_to_save, "mineplace", "screenshots")
if not os.path.exists(folder_to_save):
os.makedirs(folder_to_save)
date = time.strftime("%Y-%m-%d")
_time = time.strftime("%H-%M-%S")
pygame.image.save(screen, f"{folder_to_save}/screenshot_{date}_{_time}_{tick}.png")
if event.key == pygame.K_F3:
added_up = [player.pos[0] + player.blockselector[0], player.pos[1] + player.blockselector[1]]
print(f"Block: {world[added_up[1]][added_up[0]]}")
print(f"BlockSel pos: {added_up}")
player.keydown(world, event, tick)
keys = pygame.key.get_pressed()
player.keypress(world, keys, tick)
r.render(renderworld, player, isNight)
pygame.display.flip()
world = utils.update_world(world, tick)
renderworld = []
for y in range(len(world)):
row = []
for x in range(len(world[y])):
row.append(world[y][x].copy())
renderworld.append(row)
if tick % (60 * 60 * 10) == 0:
if tick != 0:
isNight = not isNight
tick += 1
# Save the world! (Not in that way)
# We already have the world name, so we don't need to ask for it
utils.WorldManager.save_world(worldname, world)
main()
elif option == "new":
seedscreen = seedscr.SeedScreen(screen)
seed = seedscreen.run()
if seed == "exit":
main()
world = utils.WorldGenerator(turn_alphanumeric_to_int(seed), GAME_WIDTH // GAME_BLOCK_SCALE, GAME_HEIGHT // GAME_BLOCK_SCALE, scale=0.1)
world = world.generate()
if seed == "flat":
world = utils.FlatWorldGenerator(GAME_WIDTH // GAME_BLOCK_SCALE, GAME_HEIGHT // GAME_BLOCK_SCALE)
world = world.generate()
renderworld = []
for y in range(len(world)):
row = []
for x in range(len(world[y])):
row.append(world[y][x].copy())
renderworld.append(row)
r = renderer.Renderer(screen, GAME_WIDTH // GAME_BLOCK_SCALE, GAME_HEIGHT // GAME_BLOCK_SCALE, GAME_BLOCK_SCALE)
spawn = utils.Player.find_best_spawn(world)
player = utils.Player(spawn[0], spawn[1])
tick = 0
isNight = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == KEYBINDS.close_menus):
running = False
if event.type == pygame.KEYDOWN:
if event.key == KEYBINDS.prt_sc:
folder_to_save = ""
if os.name == "nt":
folder_to_save = os.getenv("APPDATA")
else:
folder_to_save = os.path.expanduser("~")
folder_to_save = os.path.join(folder_to_save, "mineplace", "screenshots")
if not os.path.exists(folder_to_save):
os.makedirs(folder_to_save)
date = time.strftime("%Y-%m-%d")
_time = time.strftime("%H-%M-%S")
pygame.image.save(screen, f"{folder_to_save}/screenshot_{date}_{_time}_{tick}.png")
player.keydown(world, event, tick)
keys = pygame.key.get_pressed()
player.keypress(world, keys, tick)
r.render(renderworld, player, isNight)
pygame.display.flip()
world = utils.update_world(world, tick)
renderworld = []
for y in range(len(world)):
row = []
for x in range(len(world[y])):
row.append(world[y][x].copy())
renderworld.append(row)
if tick % ( 60 * 60 * 10 ) == 0:
if tick != 0:
isNight = not isNight
tick += 1
# Save the world! (Not in that way)
savescreen = savescr.SaveScreen(screen)
name = savescreen.run()
if name == "exit" or name == "":
main()
utils.WorldManager.save_world(name, world)
main()
pygame.quit()
if __name__ == "__main__":
main()