import pygame
import sys
# 初始化Pygame
pygame.init()
# 设置屏幕大小
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置颜色
WHITE = (255, 255, 255)
# 加载角色图像
character_image = pygame.image.load(r'C:\Users\dell\PycharmProjects\造梦西游重传\polllll\tuongyong\R-C.jpg') # 确保路径正确
character_image = pygame.transform.scale(character_image, (character_image.get_width() // 4, character_image.get_height() // 4))
# 初始化角色位置
character_position = character_image.get_rect(center=(screen_width // 2, screen_height - character_image.get_height()))
# 初始化无双模式和残影列表
invincible_mode = False
shadow_list = []
# 设置残影数量限制
max_shadows = 5
# 游戏主循环
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
invincible_mode = not invincible_mode
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
character_position.x -= 10
if keys[pygame.K_RIGHT]:
character_position.x += 10
# 更新屏幕
screen.fill(WHITE)
# 如果无双模式开启,添加残影
if invincible_mode:
# 创建一个新的残影,设置为当前角色的位置
shadow = pygame.Surface(character_image.get_size(), pygame.SRCALPHA)
shadow.blit(character_image, (0, 0))
shadow.set_alpha(150) # 设置较高的透明度
shadow_list.append((shadow, character_position.topleft))
# 限制残影数量
if len(shadow_list) > max_shadows:
shadow_list.pop(0)
# 绘制所有残影
for i, (shadow, pos) in enumerate(shadow_list):
# 随着时间的推移,减少透明度
new_alpha = max(0, shadow.get_alpha() - (i * 10))
shadow.set_alpha(new_alpha)
screen.blit(shadow, pos)
# 绘制角色
screen.blit(character_image, character_position)
pygame.display.flip()
clock.tick(60) # 提高游戏循环速度以实现更流畅的动画
讲上面图片的内容替换和画布自己研究一下相应的功能即可,上面是说效果按空格键触发