I'm trying to build a memory card game using pygame where I list the cards that are a match:
# 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]]
When I run the code, get this error message:
screen.blit(image, (i * (size[0] // len(images)), 0))
TypeError: argument 1 must be pygame.Surface, not list
My code looks like this:
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()
You get that error message because images
is a list in which each item is a list with two items. A list of images looks like this:
images = [[image1, image2], [image3, image4]]
images = [image1, image2, image3, image4]
If you want the list to contain each element twice, you can duplicate the list:
images = [image1, image2, image3, image4] * 2
Also, the rectangles that are drawn instead of the hidden images are too large. The rectangles should be size of the images:
while running:
# [...]
# Draw the game
screen.fill((255, 255, 255))
for i, image in enumerate(images):
pos = (i * (size[0] // len(images)), 0)
if i in flipped:
screen.blit(image, pos)
else:
pygame.draw.rect(screen, (0, 0, 0), (pos, image.get_size()))
# Update the display
pygame.display.flip()
If you have 2 different images that should match (for example: image1
and image2
), I suggest to create a list of tuples:
images = [(image1, 1), (image2, 1), (image3, 2), (image4, 2)]
Compare the 2nd element of the tuple.
if images[flipped[0]][1] == images[flipped[1]][1]:
Draw the 1st element of the tuple:
for i, image in enumerate(images):
pos = (i * (size[0] // len(images)), 0)
if i in flipped:
screen.blit(image[0], pos)
else:
pygame.draw.rect(screen, (0, 0, 0), (pos, image[0].get_size()))