Я пытаюсь создать игру с картами памяти, используя 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
Карта памяти Pygame со списком изображений ⇐ Python
Программы на Python
-
Anonymous
1737207132
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()
Подробнее здесь: [url]https://stackoverflow.com/questions/75188368/pygame-memory-card-with-list-of-images[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия