You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.7 KiB

import pygame
import os
# 初始化 Pygame
pygame.init()
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("小人动画")
clock = pygame.time.Clock()
# 加载帧序列
frames = []
frame_folder = "frames"
for frame_file in sorted(os.listdir(frame_folder)):
if frame_file.endswith(".png"):
frame = pygame.image.load(os.path.join(frame_folder, frame_file))
# 等比例缩放帧图片
frame_width, frame_height = frame.get_size()
scale_factor = min(window_width / frame_width, window_height / frame_height)
scaled_frame = pygame.transform.scale(frame, (int(frame_width * scale_factor), int(frame_height * scale_factor)))
frames.append(scaled_frame)
# 设置字体(使用支持中文的字体文件)
font_path = "c:/Windows/Fonts/simhei.ttf" # 替换为你的中文字体文件路径
font = pygame.font.Font(font_path, 36) # 使用中文字体,字号 36
# 播放动画
current_frame = 0
running = True
show_message = False # 是否显示提示框
message = "别乱点!" # 提示框内容
message_timer = 0 # 提示框显示时间
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN: # 检测鼠标点击
mouse_pos = pygame.mouse.get_pos() # 获取鼠标点击位置
print(f"Mouse clicked at: {mouse_pos}") # 打印鼠标点击位置
show_message = True # 显示提示框
message_timer = pygame.time.get_ticks() # 记录当前时间
# 绘制当前帧
window.fill((0, 0, 0)) # 清空屏幕
# 将帧居中显示
frame_width, frame_height = frames[current_frame].get_size()
x = (window_width - frame_width) // 2
y = (window_height - frame_height) // 2
window.blit(frames[current_frame], (x, y))
# 显示提示框
if show_message:
# 绘制提示框背景
message_surface = font.render(message, True, (255, 255, 255)) # 白色文字
message_rect = message_surface.get_rect(center=(window_width // 2, window_height - 50)) # 提示框位置在窗口底部
pygame.draw.rect(window, (0, 0, 0), message_rect.inflate(20, 10)) # 黑色背景
window.blit(message_surface, message_rect)
# 5 秒后隐藏提示框
if pygame.time.get_ticks() - message_timer > 5000: # 检查是否超过 5 秒
show_message = False
# 更新帧索引
current_frame = (current_frame + 1) % len(frames)
# 更新屏幕
pygame.display.flip()
clock.tick(24) # 控制帧率
# 退出 Pygame
pygame.quit()