sairate 5488a216f2 feat(第7讲 字母卡牌): 实现记忆游戏并添加开始界面
- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能
- 添加 youxijiemian.py 文件,创建游戏开始界面
- 使用 turtle 和 tkinter 模块分别实现游戏和界面
- 支持选择不同难度的游戏模式
2025-11-16 13:29:06 +08:00

128 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pgzrun
import random
import time
import pygame
WIDTH = 1000
HEIGHT = 725
TOTAL_TIME = 30 # 初始倒计时
# 载入中文字体Windows 推荐使用 SimHei 或 Microsoft YaHei
try:
FONT = pygame.font.SysFont("SimHei", 80) # 黑体
SMALL_FONT = pygame.font.SysFont("SimHei", 50)
except:
FONT = pygame.font.Font(None, 80)
SMALL_FONT = pygame.font.Font(None, 50)
def draw():
screen.fill((30, 30, 30))
if win:
text1 = FONT.render("游戏成功!", True, (255, 255, 0))
text2 = SMALL_FONT.render(f"用时:{TOTAL_TIME - remain_time:.1f}s", True, (255, 255, 255))
screen.surface.blit(text1, text1.get_rect(center=(WIDTH/2, HEIGHT/2 - 50)))
screen.surface.blit(text2, text2.get_rect(center=(WIDTH/2, HEIGHT/2 + 50)))
elif lose:
text1 = FONT.render("游戏失败!", True, (255, 0, 0))
text2 = SMALL_FONT.render("时间到", True, (255, 255, 255))
screen.surface.blit(text1, text1.get_rect(center=(WIDTH/2, HEIGHT/2 - 50)))
screen.surface.blit(text2, text2.get_rect(center=(WIDTH/2, HEIGHT/2 + 50)))
else:
t = SMALL_FONT.render(f"{max(0, remain_time):.1f}s", True, (255, 255, 0))
screen.surface.blit(t, t.get_rect(center=(900, 662)))
for grid in grids:
screen.draw.filled_rect(Rect(grid['pos'], (side, side)), grid['color'])
for fx in effects:
fx_font = pygame.font.SysFont("SimHei", int(fx['size']))
txt = fx_font.render(fx['text'], True, pygame.Color(fx['color']))
screen.surface.blit(txt, txt.get_rect(center=fx['pos']))
def update():
global remain_time, lose, effects
remain_time = TOTAL_TIME - (time.time() - start_t)
if remain_time <= 0 and not win:
lose = True
remain_time = 0
new_fx = []
for fx in effects:
x, y = fx['pos']
fx['pos'] = (x, y - 1.5)
fx['life'] -= 0.05
fx['size'] += 0.5
if fx['life'] > 0:
new_fx.append(fx)
effects = new_fx
def base_color():
return (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
def dif_color(base):
r = min(base[0] + random.randint(8, 12), 255)
g = min(base[1] + random.randint(8, 12), 255)
b = min(base[2] + random.randint(8, 12), 255)
return (r, g, b)
def game():
global grids, side, dc
side = 700 // n * 0.9
space = 700 // n * 0.1
grids = []
bc = base_color()
dc = dif_color(bc)
i = 1
dif_index = random.randint(1, n * n)
for row in range(n):
for col in range(n):
info = {
'pos': (space + col * (side + space),
space + row * (side + space)),
'color': dc if i == dif_index else bc
}
grids.append(info)
i += 1
def on_mouse_down(pos):
global n, win, start_t, effects
if win or lose:
return
for grid in grids:
grid_rect = Rect(grid['pos'], (side, side))
if grid_rect.collidepoint(pos):
if grid['color'] == dc:
start_t += 10
effects.append({
'text': '+10s',
'pos': pos,
'size': 60,
'color': 'green',
'life': 1.0
})
if n > 10:
win = True
else:
n += 1
game()
else:
start_t -= 3
effects.append({
'text': '-3s',
'pos': pos,
'size': 60,
'color': 'red',
'life': 1.0
})
break
# 初始化
n = 3
win = False
lose = False
start_t = time.time()
remain_time = TOTAL_TIME
effects = []
game()
pgzrun.go()