Когда я создавал игру я столкнулся с проблемой. При попадании в игрока коробки - он проигрывает, но питон мне выдает ошибку TypeError: 'Hero' object is not iterable. Вот код:
import pygame
from random import randint
import time
import sys
import os
name = os.getlogin()
path = r'C:\\Users\\' + name + r'\\Desktop\\'
imhero = path + r'hero.png'
imobject = path + r'object_1.png'
pygame.init()
x_start = 20
y_start = 400
BLACK = (0,0,0)
WHILE = (255,255,255)
BLUE = (0,255, 225)
Salmon = (255, 160, 122)
FPS = 50
play = True
motion = "STOP"
clock = pygame.time.Clock()
global sc
sc = pygame.display.set_mode((500,500))
x = x_start
y = y_start
x_object_1 = randint(1,500)
y_object_1 = 0
class Object(pygame.sprite.Sprite):
def __init__(self, x_object_1, y_object_1,filename):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(filename)
self.image.set_colorkey((255,255,255))
self.rect = self.image.get_rect(center=(x_object_1, y_object_1))
self.image.fill(BLACK)
#self.sc.blit(self.image,self.rect)
class Hero(pygame.sprite.Sprite):
def __init__(self,x,y,filename):
pygame.sprite.Sprite.__init__(self)
self.image_hero = pygame.image.load(filename)
self.image_hero.set_colorkey((255,255,255))
self.rect_hero = self.image_hero.get_rect(center = (x,y))
self.image_hero.fill(Salmon)
#self.screen.blit(self.image_hero,self.rect_hero)
object1 = Object (x_object_1,y_object_1,imobject)
hero = Hero(x,y,imhero)
while play:
sc.fill(BLUE)
pygame.display.update()
for i in pygame.event.get():
if i.type == pygame.QUIT:
exit()
elif i.type == pygame.KEYDOWN:
if i.key == pygame.K_LEFT:
motion = 'LEFT'
elif i.key == pygame.K_RIGHT:
motion = 'RIGHT'
elif i.type == pygame.KEYUP:
if i.key in [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN]:
motion = 'STOP'
if motion == 'LEFT':
Hero.rect[0] -= 3
elif motion == 'RIGHT':
Hero.rect[0] += 3
if pygame.sprite.spritecollideany(object1, hero) != None:
play = False
break
Если я уберу комментарии, то питон выдает следущее: AttributeError: 'Object' object has no attribute 'sc'