[pygame错误消息] type object' sprite'没有属性' sprites'

时间:2016-12-04 07:01:06

标签: python pygame

我是pygame的新手,并决定在我的游戏中实现一些精灵,而不仅仅是在物体周围移动。我希望得到它所以每次玩家遇到障碍时我都会增加分数,但是我在碰撞方法上遇到了很多麻烦。但是,每当我运行程序时,我都会收到此错误。

from classyforryan import ourMusic, sprites
import pygame
import os
import time

pygame.init() #short for initialize does return a tuple of successful intilizaton
colors={"black":(0,0,0), "white": (255, 255, 255), "red": (255, 0, 0), "green": (0, 255, 0), "purple": (164, 66, 244), "pink" :(252, 25, 123)}
gameDisplay= pygame.display.set_mode((800, 600))

bg=pygame.image.load('clouds_converted.jpg')
moore=pygame.image.load("baemoore_converted.png")
jump = False
fall = False

cube = pygame.image.load("cube.png")
player=sprites(moore, 50, 544)
ob = sprites(cube, 2125, 580)
ob1 = sprites(cube, 2125, 580)
ob2 = sprites(cube, 2125, 580)


pygame.display.set_caption("lets play!")
gameExit = False
clock = pygame.time.Clock()


class sprite(pygame.sprite.Sprite):
    def __init__(self, ob, ob1, ob2, image, location):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("1136693.jpg") #edited to add self. :)
        self.spritesgroup = pygame.sprite.Group() #sprite group
        self.spritesgroup.add(ob)
        self.spritesgroup.add(ob1)
        self.spritesgroup.add(ob2)


while not gameExit:
    clock.tick(40)
    timer = pygame.time.get_ticks()
    print(timer)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            gameExit=True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                gameExit= True
    pygame.display.update()
    gameDisplay.blit(bg,[0,0])
    time.sleep(.05)
    player.pos()
    ob.pos()
    ob.left()
    ob1.pos()
    ob2.pos()

blocks_hit_list = pygame.sprite.spritecollide(player, sprite.spritesgroup, True)
    for block in blocks_hit_list:
        score +=1
    print(score)
pygame.quit() #unintiliazes pygames
quit() #this will exit out of python

建议非常感谢。 这是游戏循环:

import pygame
import os
import time
gameDisplay= pygame.display.set_mode((800, 600))

class sprites(pygame.sprite.Sprite):
    def __init__(self, image, xcoor, ycoor):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
        self.rectx=xcoor
        self.recty=ycoor
    def pos(self):
        gameDisplay.blit(self.image, [self.rectx, self.recty])
    def left(self):
        dist = 7
        self.rectx = self.rectx - dist

以下是导入的所有类:

$('#play-button').click(function(){ audio.play(); });}

1 个答案:

答案 0 :(得分:0)

pygame.sprite.spritecollide的{​​{3}}表示该参数应该是一组精灵。看起来您创建了一个用于保存该组的类sprite,但您将该组分配给局部变量而不是实例成员。我为这些添加了self.,并更改了您遇到错误的行,以便引用它。我猜这是你想要的,但不能确定。但/ / p>

class sprite(pygame.sprite.Sprite):
    def __init__(self, ob, ob1, ob2, image, location):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image)
        self.spritesgroup = pygame.sprite.Group() #sprite group
        self.spritesgroup.add(ob)
        self.spritesgroup.add(ob1)
        self.spritesgroup.add(ob2)
        #sprites.add(player)

# ...            

sprite1 = sprite(ob, ob1, ob2, "1136693.jpg", "location")
blocks_hit_list = pygame.sprite.spritecollide(player, sprite1.spritesgroup, True)