-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
175 lines (134 loc) · 4.96 KB
/
main.py
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
from modules.position import *
from modules import settings
from modules import events
from modules import saves
from modules import audio
from modules import calc
import pygame
import random
import sys
import os
os.system("cls || clear")
# System arguments.
for arg in sys.argv[1:]:
if arg.startswith("@"):
seed = arg.removeprefix("@")
seed = int(seed) if seed.isnumeric() else calc.str_to_seed(seed)
if saves.get_saved_seed() != seed:
print(f"Using custom seed: {seed} (cleared previous save: {saves.get_saved_seed()})\n")
saves.remove_save()
else:
print(f"Using custom seed: {seed} (same as in previous sesssion)\n")
settings.SEED = seed
if arg.lower() == "--no-save":
settings.AVOID_SAVE = True
print("Avoiding save:\n * Not applying saved changes to world.",
" * Not saving future changes in this session.\n", sep="\n")
if arg.lower() == "--clear-save":
saves.remove_save()
print("Cleared save. Clean world will be generated and \
upcoming changes will be saved.\n")
if arg.lower() in ("--help", "help", "h", "-h", "--h"):
print("""
--- HELP --
Available startup options:
--no-save: Do not apply saved changes and don't save upcoming changes.
--clear-save: Removes saved file.
@SEED: (Replace 'SEED' with an value) Sets custom seed for world.
(Removes current save file.)
Game controls are displayed after startup.
(For ~10FPS boost use `pygame-ce` instead of `pygame` library.)
""")
print("""
Controls:
(Keyboard)
WSAD : Move player.
Space: Jump.
(Mouse)
Move cursor : Highlights block.
Scroll : Selects block.
Left button : Destroy highlighted block.
Right button : Places selected block on highlighted block.
Middle button: Move player using PathFinding algorithm.
\n
""")
random.seed(settings.SEED)
saves.init()
pygame.init()
pygame.event.set_allowed([
pygame.QUIT,
pygame.KEYDOWN,
pygame.MOUSEBUTTONDOWN,
pygame.MOUSEWHEEL
])
screen = pygame.display.set_mode(
(settings.SCREEN_WIDTH, settings.SCREEN_HEIGHT)
)
screen.set_alpha(None)
from modules import weather
from modules import player
from modules import world
w = world.World()
p = player.Player(screen, w)
p.render()
weather.start_weather_thread()
clock = pygame.time.Clock()
while 1:
clock.tick()
mx, my = pygame.mouse.get_pos()
events.EventLoop.execute_all_loops()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
p.move(AngleDirection.W, True)
if event.key == pygame.K_d:
p.move(AngleDirection.E, True)
if event.key == pygame.K_w:
p.move(AngleDirection.N, True)
if event.key == pygame.K_s:
p.move(AngleDirection.S, True)
if event.key == pygame.K_SPACE:
p.start_jump()
if event.type == pygame.MOUSEBUTTONDOWN:
# Left click
if pygame.mouse.get_pressed()[0]:
h_block = w.get_highlighted_block(mx, my)
if h_block:
w.set_at(h_block.block.coordinate, None)
audio.play_sfx_break()
if h_block.block.coordinate == p.pos:
p.fall()
# Middle click
if pygame.mouse.get_pressed()[1]:
h_block = w.get_highlighted_block(mx, my)
if h_block:
p.pathfind_move(h_block.block.coordinate)
# Right click
if pygame.mouse.get_pressed()[2]:
h_block = w.get_highlighted_block(mx, my)
if not h_block:
continue
block = h_block.block
face = h_block.face
if face == BlockFace.TOP:
new_coord = h_block.block.coordinate.add_z(1)
if face == BlockFace.LEFT:
new_coord = h_block.block.coordinate.add_y(1)
if face == BlockFace.RIGHT:
new_coord = h_block.block.coordinate.add_x(1)
if new_coord != p.pos.add_z(1) and new_coord != p.pos.add_z(2):
if new_coord.z in range(settings.CHUNK_MAX_HEIGHT - 1):
w.set_at(
new_coord,
p.get_selected_block_class()(new_coord)
)
audio.play_sfx_put()
if event.type == pygame.MOUSEWHEEL:
p.update_picked_block(event.y)
p.render()
caption_fps = f"{round(clock.get_fps())} FPS"
caption_pos = f"{p.pos.x}/{p.pos.y}/{p.pos.z}"
caption_ch = f"{w.current_chunk.x}, {w.current_chunk.y}"
pygame.display.set_caption(f"{caption_fps} | {caption_pos} ({caption_ch})")