Карта памяти Pygame со списком изображенийPython

Программы на Python
Ответить
Anonymous
 Карта памяти Pygame со списком изображений

Сообщение Anonymous »

Я пытаюсь создать игру с картами памяти, используя pygame, где я перечисляю совпадающие карты:
# Load the images
image1 = pygame.image.load("1.png")
image2 = pygame.image.load("2.png")
image3 = pygame.image.load("3.png")
image4 = pygame.image.load("4.png")
# Create a list of image pairs
images = [[image1, image2], [image3, image4]]

Когда я запускаю код, получаю следующее сообщение об ошибке:
screen.blit(image, (i * (size[0] // len(images)), 0))
TypeError: argument 1 must be pygame.Surface, not list

Мой код выглядит так:
import pygame
import random

# Initialize Pygame
pygame.init()

# Set the window size and caption
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Image Memory Card Game")

# Load the images
image1 = pygame.image.load("1.png")
image2 = pygame.image.load("2.png")
image3 = pygame.image.load("3.png")
image4 = pygame.image.load("4.png")
# Create a list of image pairs
images = [[image1, image2], [image3, image4]]

# Shuffle the image pairs
random.shuffle(images)

# Create a list to store the flipped cards
flipped = []

# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Handle player input
if event.type == pygame.MOUSEBUTTONDOWN:
# Get the coordinates of the clicked card
x, y = pygame.mouse.get_pos()
# Get the index of the clicked card
index = x // (size[0] // len(images))
# Flip the card
flipped.append(index)

if len(flipped) > 2:
# Hide the flipped cards
flipped = []

if len(flipped) == 2:
# Check if the flipped cards match
if images[flipped[0]] == images[flipped[1]]:
# Increment the player's score
score += 1
else:
# Wait for the player to try again
pygame.time.wait(1000)

# Draw the game
screen.fill((255, 255, 255))
for i, image in enumerate(images):
if i in flipped:
# Draw the flipped card
screen.blit(image, (i * (size[0] // len(images)), 0))
else:
# Draw the back of the card
pygame.draw.rect(screen, (0, 0, 0), (i * (size[0] // len(images)), 0, (size[0] // len(images)), size[1]))

# Update the display
pygame.display.flip()

# End the game
pygame.quit()


Подробнее здесь: https://stackoverflow.com/questions/751 ... -of-images
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»