Аргумент должен быть pygame.Surface not str - PullRequest
0 голосов
/ 20 июня 2020

Я создаю небольшую игру на совпадение, но застрял при отображении изображения. Когда я пытаюсь отобразить изображение с помощью функции 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

Я не могу понять это. В чем может быть причина ??

...