-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_display.py
More file actions
327 lines (257 loc) · 11.8 KB
/
game_display.py
File metadata and controls
327 lines (257 loc) · 11.8 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import pygame
from game_init import load_fonts, calculate_window_size, GAME_CONFIG
# 颜色定义
COLORS = {
'bg_top': (15, 15, 35),
'bg_bottom': (5, 5, 20),
'grid_line': (40, 40, 60),
'border': (80, 80, 120),
'border_glow': (120, 120, 180),
'text_white': (255, 255, 255),
'text_gold': (255, 215, 0),
'text_cyan': (0, 255, 255),
'panel_bg': (25, 25, 40, 220),
}
def draw_3d_block(surface, x, y, size, color, alpha=255):
"""绘制3D效果的方块"""
temp_surface = pygame.Surface((size, size), pygame.SRCALPHA)
# 基础颜色
base_color = color + (alpha,)
# 高光颜色(更亮)
highlight = tuple(min(255, c + 80) for c in color) + (alpha,)
# 阴影颜色(更暗)
shadow = tuple(max(0, c - 80) for c in color) + (alpha,)
# 主方块
pygame.draw.rect(temp_surface, base_color, (0, 0, size, size))
# 高光(左上角)- 斜角效果
points_high = [(0, 0), (size, 0), (size - 4, 4), (4, 4), (4, size - 4), (0, size)]
pygame.draw.polygon(temp_surface, highlight, points_high)
# 阴影(右下角)- 斜角效果
points_shadow = [(size, size), (size, 0), (size - 4, 4), (size - 4, size - 4), (4, size - 4), (0, size)]
pygame.draw.polygon(temp_surface, shadow, points_shadow)
# 内芯(正常颜色)
inner_size = size - 8
pygame.draw.rect(temp_surface, base_color, (4, 4, inner_size, inner_size))
# 内边框高光
pygame.draw.rect(temp_surface, (255, 255, 255, 80), (4, 4, inner_size, inner_size), 1)
surface.blit(temp_surface, (x, y))
def draw_gradient_background(screen, width, height):
"""绘制渐变背景带星空效果"""
# 基础渐变
for y in range(height):
ratio = y / height
r = int(COLORS['bg_top'][0] * (1 - ratio) + COLORS['bg_bottom'][0] * ratio)
g = int(COLORS['bg_top'][1] * (1 - ratio) + COLORS['bg_bottom'][1] * ratio)
b = int(COLORS['bg_top'][2] * (1 - ratio) + COLORS['bg_bottom'][2] * ratio)
pygame.draw.line(screen, (r, g, b), (0, y), (width, y))
# 绘制星星
star_positions = [
(80, 60), (200, 40), (350, 80), (450, 120), (150, 150),
(300, 200), (100, 280), (400, 320), (180, 380), (320, 450),
(120, 520), (380, 580), (220, 620), (480, 680)
]
for i, (sx, sy) in enumerate(star_positions):
brightness = 150 + (i * 13) % 105
size = 1 + (i % 3)
pygame.draw.circle(screen, (brightness, brightness, 220), (sx, sy), size)
def draw_game_board(screen, game_board, config, offset_x, offset_y):
"""绘制游戏棋盘"""
cell_size = config['cell_size']
rows = len(game_board)
cols = len(game_board[0])
board_width = cols * cell_size
board_height = rows * cell_size
# 绘制棋盘背景
board_rect = pygame.Rect(offset_x - 4, offset_y - 4, board_width + 8, board_height + 8)
pygame.draw.rect(screen, (15, 15, 25), board_rect, border_radius=4)
pygame.draw.rect(screen, COLORS['border'], board_rect, 2, border_radius=4)
# 绘制网格线
for i in range(cols + 1):
x = offset_x + i * cell_size
pygame.draw.line(screen, COLORS['grid_line'],
(x, offset_y), (x, offset_y + board_height), 1)
for i in range(rows + 1):
y = offset_y + i * cell_size
pygame.draw.line(screen, COLORS['grid_line'],
(offset_x, y), (offset_x + board_width, y), 1)
# 绘制已固定的方块
for y, row in enumerate(game_board):
for x, cell in enumerate(row):
if cell:
draw_3d_block(screen,
offset_x + x * cell_size,
offset_y + y * cell_size,
cell_size, cell)
def draw_current_block(screen, block, block_x, block_y, color, config, offset_x, offset_y):
"""绘制当前移动的方块"""
cell_size = config['cell_size']
for y, row in enumerate(block):
for x, cell in enumerate(row):
if cell:
draw_3d_block(screen,
offset_x + (block_x + x) * cell_size,
offset_y + (block_y + y) * cell_size,
cell_size, color)
def draw_ghost_block(screen, block, block_x, drop_y, color, config, offset_x, offset_y):
"""绘制方块落点预览(幽灵方块)"""
cell_size = config['cell_size']
ghost_color = tuple(max(0, min(255, c // 4 + 30)) for c in color)
for y, row in enumerate(block):
for x, cell in enumerate(row):
if cell:
rect = pygame.Rect(
offset_x + (block_x + x) * cell_size + 2,
offset_y + (drop_y + y) * cell_size + 2,
cell_size - 4, cell_size - 4)
pygame.draw.rect(screen, ghost_color, rect, border_radius=3)
pygame.draw.rect(screen, ghost_color + (150,), rect, 2, border_radius=3)
def draw_next_block_preview(screen, next_block, next_color, font, config, sidebar_x, start_y):
"""绘制下一个方块预览"""
cell_size = 28
panel_width = 160
panel_height = 120
# 预览面板背景
panel_rect = pygame.Rect(sidebar_x, start_y, panel_width, panel_height)
pygame.draw.rect(screen, (25, 25, 40), panel_rect, border_radius=6)
pygame.draw.rect(screen, COLORS['border'], panel_rect, 2, border_radius=6)
# 标题
title = font.render("NEXT", True, COLORS['text_gold'])
screen.blit(title, (sidebar_x + 10, start_y + 8))
# 计算居中位置
if next_block:
block_width = len(next_block[0]) * cell_size
block_height = len(next_block) * cell_size
start_x = sidebar_x + (panel_width - block_width) // 2
block_y = start_y + 40 + (panel_height - 40 - block_height) // 2
for y, row in enumerate(next_block):
for x, cell in enumerate(row):
if cell:
draw_3d_block(screen,
start_x + x * cell_size,
block_y + y * cell_size,
cell_size, next_color)
def draw_ui_panel(screen, score, elapsed_time, level, lines_cleared, lines_needed,
font, small_font, sidebar_x, start_y):
"""绘制UI信息面板"""
panel_width = 160
panel_items = [
("LEVEL", level, COLORS['text_gold']),
("SCORE", score, COLORS['text_cyan']),
("TIME", f"{elapsed_time}s", COLORS['text_white']),
("LINES", f"{lines_cleared}/{lines_needed}", COLORS['text_white'])
]
y_pos = start_y
for label, value, color in panel_items:
# 标签背景
label_bg = pygame.Rect(sidebar_x, y_pos, panel_width, 50)
pygame.draw.rect(screen, (30, 30, 50), label_bg, border_radius=4)
pygame.draw.rect(screen, COLORS['border'], label_bg, 1, border_radius=4)
# 标签
label_text = small_font.render(label, True, (150, 150, 170))
screen.blit(label_text, (sidebar_x + 8, y_pos + 6))
# 值
value_text = font.render(str(value), True, color)
screen.blit(value_text, (sidebar_x + 8, y_pos + 22))
y_pos += 60
def draw_pause_overlay(screen, font, small_font, width, height):
"""绘制暂停遮罩"""
# 半透明遮罩
overlay = pygame.Surface((width, height), pygame.SRCALPHA)
overlay.fill((0, 0, 0, 180))
screen.blit(overlay, (0, 0))
# 暂停文字
pause_text = font.render("PAUSED", True, COLORS['text_gold'])
text_rect = pause_text.get_rect(center=(width // 2, height // 2 - 20))
screen.blit(pause_text, text_rect)
# 提示文字
hint_text = small_font.render("Press SPACE to continue", True, (200, 200, 200))
hint_rect = hint_text.get_rect(center=(width // 2, height // 2 + 20))
screen.blit(hint_text, hint_rect)
def draw_game(screen, block, block_x, block_y, color, game_board, score,
elapsed_time, level, small_font, font, medium_font, config,
next_block=None, next_color=None, ghost_y=None,
lines_cleared=0, lines_needed=10):
"""主绘制函数"""
width, height = screen.get_size()
# 计算布局
padding = config['padding']
cell_size = config['cell_size']
board_width = config['board_cols'] * cell_size
# 棋盘位置
board_x = padding
board_y = padding + 20
# 侧边栏位置
sidebar_x = board_x + board_width + padding
# 绘制背景
draw_gradient_background(screen, width, height)
# 绘制标题
title = medium_font.render("TETRIS", True, COLORS['text_gold'])
screen.blit(title, (board_x, 10))
# 绘制游戏棋盘
draw_game_board(screen, game_board, config, board_x, board_y)
# 绘制幽灵方块
if ghost_y is not None:
draw_ghost_block(screen, block, block_x, ghost_y, color, config, board_x, board_y)
# 绘制当前方块
draw_current_block(screen, block, block_x, block_y, color, config, board_x, board_y)
# 绘制下一个方块预览
if next_block and next_color:
draw_next_block_preview(screen, next_block, next_color, small_font, config, sidebar_x, board_y)
# 绘制UI面板(位于预览下方)
draw_ui_panel(screen, score, elapsed_time, level, lines_cleared, lines_needed,
font, small_font, sidebar_x, board_y + 140)
# 绘制操作提示
tiny_font = pygame.font.SysFont(None, 18)
hints = [
("Controls:", COLORS['text_gold']),
("Left/Right - Move", (180, 180, 180)),
("Up - Rotate", (180, 180, 180)),
("Down - Soft Drop", (180, 180, 180)),
("Shift - Hard Drop", (180, 180, 180)),
("Space - Pause", (180, 180, 180))
]
y = board_y + 420
for text, color in hints:
hint_text = tiny_font.render(text, True, color)
screen.blit(hint_text, (sidebar_x, y))
y += 22
def display_game_over(screen, font, small_font, score=0, level=0):
"""显示游戏结束画面"""
width, height = screen.get_size()
# 渐变背景
for y in range(height):
ratio = y / height
color = (int(40 * (1 - ratio) + 10 * ratio),
int(20 * (1 - ratio) + 10 * ratio),
int(20 * (1 - ratio) + 20 * ratio))
pygame.draw.line(screen, color, (0, y), (width, y))
# 游戏结束文字
game_over_text = font.render("GAME OVER", True, (255, 80, 80))
text_rect = game_over_text.get_rect(center=(width // 2, height // 2 - 60))
screen.blit(game_over_text, text_rect)
# 显示最终得分和关卡
score_text = small_font.render(f"Score: {score}", True, (255, 215, 0))
score_rect = score_text.get_rect(center=(width // 2, height // 2))
screen.blit(score_text, score_rect)
level_text = small_font.render(f"Level: {level}", True, (255, 255, 255))
level_rect = level_text.get_rect(center=(width // 2, height // 2 + 40))
screen.blit(level_text, level_rect)
pygame.display.flip()
pygame.time.wait(3000)
def display_level_complete(screen, font, small_font, level):
"""显示关卡完成画面"""
width, height = screen.get_size()
# 半透明遮罩
overlay = pygame.Surface((width, height), pygame.SRCALPHA)
overlay.fill((0, 100, 0, 180))
screen.blit(overlay, (0, 0))
# 恭喜文字
congrats_text = font.render("LEVEL CLEAR!", True, (100, 255, 100))
text_rect = congrats_text.get_rect(center=(width // 2, height // 2 - 30))
screen.blit(congrats_text, text_rect)
# 下一关提示
next_level_text = small_font.render(f"Next Level: {level + 1}", True, (255, 255, 255))
next_rect = next_level_text.get_rect(center=(width // 2, height // 2 + 20))
screen.blit(next_level_text, next_rect)
pygame.display.flip()
pygame.time.wait(2000)