You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pygame开发记忆卡游戏时遇TypeError错误求助

Pygame记忆卡配对游戏:TypeError 错误修复

问题重现

你创建了图片对的二维列表:

# 加载图片
image1 = pygame.image.load("1.png")
image2 = pygame.image.load("2.png")
image3 = pygame.image.load("3.png")
image4 = pygame.image.load("4.png")
# 创建图片对列表
images = [[image1, image2], [image3, image4]]

运行时触发错误:

screen.blit(image, (i * (size[0] // len(images)), 0))
TypeError: argument 1 must be pygame.Surface, not list

错误根源

images是二维列表,循环遍历的时候,image变量拿到的是子列表(比如[image1, image2]),但screen.blit()要求第一个参数必须是pygame.Surface对象(也就是单张图片),不是列表,所以报错。

另外这个列表结构也不符合记忆卡的逻辑——记忆卡需要每张目标图片出现两次,而不是把配对的图片放在子列表里。

修复方案

1. 重构图片列表

把图片改成一维列表,每张图片重复两次,这样才是记忆卡的正确结构:

# 加载图片
image1 = pygame.image.load("1.png")
image2 = pygame.image.load("2.png")
image3 = pygame.image.load("3.png")
image4 = pygame.image.load("4.png")
# 每张图片出现两次,组成记忆卡组
images = [image1, image1, image2, image2, image3, image3, image4, image4]

2. 洗牌打乱顺序

对重构后的列表洗牌,确保每次游戏的卡片顺序都不一样:

random.shuffle(images)

3. 修正翻牌与匹配逻辑

  • 初始化score变量(原代码里没定义直接用了,会报错)
  • 限制每次最多翻两张牌,避免重复点击同一张已翻牌
  • 匹配成功后更新分数,不匹配则延迟后隐藏卡片

修复后的完整代码

import pygame
import random

# 初始化Pygame
pygame.init()

# 设置窗口大小和标题
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("图片记忆卡游戏")

# 加载图片
image1 = pygame.image.load("1.png")
image2 = pygame.image.load("2.png")
image3 = pygame.image.load("3.png")
image4 = pygame.image.load("4.png")
# 构建记忆卡组:每张图片出现两次
images = [image1, image1, image2, image2, image3, image3, image4, image4]

# 洗牌打乱顺序
random.shuffle(images)

# 存储已翻牌的索引
flipped = []
# 初始化分数
score = 0

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # 处理鼠标点击
        if event.type == pygame.MOUSEBUTTONDOWN:
            # 最多同时翻两张牌
            if len(flipped) < 2:
                x, y = pygame.mouse.get_pos()
                card_width = size[0] // len(images)
                index = x // card_width
                # 避免重复点击已翻的牌
                if index not in flipped:
                    flipped.append(index)

            # 翻了两张牌,判断是否匹配
            if len(flipped) == 2:
                if images[flipped[0]] == images[flipped[1]]:
                    score += 1
                    # 匹配成功,清空翻牌列表准备下一轮
                    flipped = []
                else:
                    # 不匹配,让玩家看清楚后隐藏
                    pygame.time.wait(1000)
                    flipped = []

    # 绘制游戏界面
    screen.fill((255, 255, 255))
    card_width = size[0] // len(images)
    for i, image in enumerate(images):
        if i in flipped:
            # 显示正面图片
            screen.blit(image, (i * card_width, 0))
        else:
            # 显示卡片背面(黑色矩形)
            pygame.draw.rect(screen, (0, 0, 0), (i * card_width, 0, card_width, size[1]))

    # 更新显示
    pygame.display.flip()

# 退出游戏
pygame.quit()

内容的提问来源于stack exchange,提问作者Darlene

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:15:27