forked from 000Nobody/2D-Minecraft
/
game.py
215 lines (165 loc) · 6.46 KB
/
game.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
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
import logging
import time
import sys
import pygame
from data.scripts.classes.player import Player
from data.scripts.classes.terrain import Terrain
from data.scripts.classes.hotbar import Hotbar
from data.scripts.classes.inventory import Inventory
from data.scripts.core_functions import draw, distance
from data.variables import (
TILE_SIZE, scroll, SCROLL_STIFF, RENDER_DISTANCE
)
MIN_ACTION_TIME = 0.05 # effectively 20 ticks
LOG = logging.getLogger(__name__)
LOG.setLevel('DEBUG')
font = pygame.font.Font('data/fonts/minecraft_font.ttf', 30)
def quit_game():
pygame.quit()
sys.exit()
class Text:
def __init__(self, text, x, y):
# used to print the status of the variables
self._text = font.render(text, True, (0, 0, 0))
self._text_rect = self._text.get_rect()
self._text_rect.topleft = (x, y)
def draw(self, screen):
screen.blit(self._text, self._text_rect)
def process_inventory(player, terrain, hotbar, inventory):
"""Process inventory events"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if event.type == pygame.KEYDOWN:
# control Q to exit
if event.key == pygame.K_q:
if pygame.key.get_mods() and pygame.KMOD_CTRL:
quit_game()
if event.key == pygame.K_e:
inventory.toggle()
hotbar.toggle()
mx, my = pygame.mouse.get_pos()
# inventory.get_selected_item(mx, my)
def process_actions(player, terrain, hotbar, inventory):
"""Process all actions"""
# LOG.debug('Process action')
#######################################################################
# get held buttons (for setting and breaking blocks
mouse_held = pygame.mouse.get_pressed()
if mouse_held[0]: # left click for placing
player.place_block(terrain, hotbar)
elif mouse_held[2]: # right click for breaking
player.break_block(terrain, hotbar)
else:
player.reset_break(terrain)
#######################################################################
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if event.type == pygame.KEYDOWN:
# control Q to exit
if event.key == pygame.K_q:
if pygame.key.get_mods() and pygame.KMOD_CTRL:
quit_game()
if event.key == pygame.K_a:
player.moving_left = True
if event.key == pygame.K_d:
player.moving_right = True
if event.key == pygame.K_e:
inventory.toggle()
hotbar.toggle()
if event.key == pygame.K_SPACE or event.key == pygame.K_w:
player.jumping = True
try:
if int(pygame.key.name(event.key)) != 0:
hotbar.selected_slot = int(pygame.key.name(event.key))
except ValueError:
pass
if event.type == pygame.KEYUP:
if event.key == pygame.K_d:
player.moving_right = False
if event.key == pygame.K_a:
player.moving_left = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
if hotbar.selected_slot != 9:
hotbar.selected_slot += 1
else:
hotbar.selected_slot = 1
if event.button == 5:
if hotbar.selected_slot != 1:
hotbar.selected_slot -= 1
else:
hotbar.selected_slot = 9
def main(full_screen=False, window_size=None, creative=False, lighting=True):
"""Main program loop.
Parameters
----------
full_screen : bool
Run pygame in full screen.
window_size : tuple
Size of the window in ``(x, y)``
"""
pygame.init()
clock = pygame.time.Clock()
if full_screen:
flags = pygame.FULLSCREEN | pygame.DOUBLEBUF | pygame.HWSURFACE
screen = pygame.display.set_mode((0, 0), flags)
info = pygame.display.Info()
window_size = info.current_w, info.current_h
else:
if window_size is None:
# still windowed and not genuinely full screen
info = pygame.display.Info()
window_size = info.current_w, info.current_h - 50
screen = pygame.display.set_mode(window_size)
terrain = Terrain(lighting=lighting)
start_x = 0
start_y = terrain.ground_level(start_x) - 2
player = Player(
(start_x, start_y), TILE_SIZE-10, TILE_SIZE*2-10, 9, 13, creative=creative
)
scroll[0] = (player.rect.x - scroll[0] - (window_size[0]/2 + player.width/2 - 50))
scroll[1] = (player.rect.y - scroll[1] - (window_size[1]/2 + player.height/2 - 100))
hotbar = Hotbar(window_size)
inventory = Inventory(window_size)
last_action_time = 0
t_last_fps = 0
text = None
while True:
clock.tick(60)
t_start = time.time()
time_since_last_action = time.time() - last_action_time
if time_since_last_action > MIN_ACTION_TIME:
# Process inventory events only if inventory visible
if inventory.visible:
process_inventory(player, terrain, hotbar, inventory)
else:
process_actions(player, terrain, hotbar, inventory)
last_action_time = time.time()
# Process player and terrain only if inventory hidden
if not inventory.visible:
mx, my = pygame.mouse.get_pos()
scroll[0] += int(
(player.rect.x - scroll[0] - (window_size[0]/2 + player.width/2 - 50)) / SCROLL_STIFF
)
scroll[1] += int(
(player.rect.y - scroll[1] - (window_size[1]/2 + player.height/2 - 100)) / SCROLL_STIFF
)
# consider unloading world blocks here
player.get_selected_block(terrain, mx, my)
terrain.update(player)
player.update(terrain)
hotbar.update()
items = [terrain, player, hotbar, inventory]
if text:
items.append(text)
terrain.player_position = player.coords
draw(screen, items)
show_fps = True
if show_fps:
if time.time() - t_last_fps > 1:
t_last_fps = time.time()
t_total = time.time() - t_start
t_fps = 1/t_total
text = Text(f"Theoretical FPS: {t_fps:.0f}", 20, 20)