【问题标题】:Argument must be pygame.Surface not str参数必须是 pygame.Surface 而不是 str
【发布时间】:2020-10-10 19:46:45
【问题描述】:

我正在创建一个小型匹配游戏,但在显示图像时卡住了。 当我尝试使用 blit() 函数显示图像时,它会显示错误。 我尝试在 blit 之前加载图像,但它仍然显示错误。 下面是示例代码:

import pygame
from time import sleep
from pygame import display, image, event, transform
import os
import random

pygame.init()

ASSET_DIR = 'Processed_image'
ASSET_FILES = [i for i in os.listdir(ASSET_DIR)]
for i in range(len(ASSET_FILES)):
   image_path = os.path.join(ASSET_DIR, ASSET_FILES[i])
   print(image_path)

animal_count = dict((img, 0) for img in ASSET_FILES)

def available_animals():
    # All animals whose count is less than two
    return [a for a, c in animal_count.items() if c < 2]

class animal:
    def __init__(self, i):
        self.i = i
        self.row = i // NUM_TILES_SIDE
        self.col = i % NUM_TILES_SIDE

        self.img_name = random.choice(available_animals())
        animal_count[self.img_name] += 1
        self.img_path = os.path.join(ASSET_DIR, self.img_name)
        self.img_load = image.load(self.img_path)
        self.img_load = transform.scale(self.img_load, (IMAGE_SIZE - 2 * MARGIN, IMAGE_SIZE - 2 * MARGIN))

        self.box = self.img_load.copy()
        self.box.fill((200, 200, 200))

        self.skip = False

tiles = [animal(i) for i in range(0, NUM_TILES_TOTAL)]

current_images = []

def find_index(x, y):
    row = y // IMAGE_SIZE
    col = x // IMAGE_SIZE
    index = row * NUM_TILES_SIDE + col
    return index

display.set_caption("Hello")

screen = display.set_mode((960, 960))

matched = image.load('/home/cropped_trump.jpg')

running = True
while running:
    current_events = event.get() # List of events
    for e in current_events:
        if e.type == pygame.QUIT:
            print(e.type)
            running = False

        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_ESCAPE:
                print(e.type)
                running = False

        if e.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            index = find_index(mouse_x, mouse_y)
        
        if index not in current_images:
            current_images.append(index)
        if len(current_images)>2:
            current_images = current_images[1:]
        print(current_images)

    screen.fill((255, 255, 255))

    total_skipped = 0

    for _, tile in enumerate(tiles):
        image_i = tile.img_name if tile.i in current_images else tile.box
        if not tile.skip:
            screen.blit(image_i, (tile.col * IMAGE_SIZE + MARGIN, tile.row * IMAGE_SIZE + MARGIN))
        else:
            total_skipped += 1

    display.flip()

    if len(current_images) == 2:
        index1, index2 = current_images
    if tiles[index1].img_name == tiles[index2].img_name:
        tiles[index1].skip = True
        tiles[index2].skip = True
        sleep(0.2)
        screen.blit(matched, (0, 0))
        sleep(0.2)
        current_images = []

    if total_skipped == len(tiles):
        running = False 

我收到错误:

Traceback (most recent call last):
  File "/home/shashi/Matchers/new.py", line 93, in <module>
    screen.blit(image_i, (tile.col * IMAGE_SIZE + MARGIN, tile.row * IMAGE_SIZE + MARGIN))
TypeError: argument 1 must be pygame.Surface, not str

我无法弄清楚。 可能是什么原因??

【问题讨论】:

  • 这是一个错字。它必须是 image_i = tile.img_load 而不是 image_i = tile.img_name
  • 谢谢,这有帮助,但我发现还有另一个问题,我没有添加 display.flip()。现在完成了。

标签: python string pygame surface blit


【解决方案1】:

这是一个错字。它必须是image_i = tile.img_load 而不是image_i = tile.img_name

image_i = tile.img_name if tile.i in current_images else tile.box

image_i = tile.img_load if tile.i in current_images else tile.box

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多