需要帮助理解pygame错误:AttributeError:'module'对象没有属性'collide__rect'

时间:2013-12-16 19:22:28

标签: pygame

我在这里尝试做的是检测名为Paperboy的精灵与名为Indicator的精灵(23,24,25等......)之间的碰撞。

我收到错误AttributeError:'module'对象没有属性'collide__rect'

不太清楚我在这里做错了什么。

> #Initialize
import pygame
import random
pygame.init()

#Display
screen = pygame.display.set_mode((1180, 900))

class PaperBoy(pygame.sprite.Sprite):
    def __init__(self,startY):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("paperboy.gif")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()
        self.rect.centery = startY
        self.dx= 300
        self.dy= 300


    def update(self):

        #adjust x/y to dx/dy

        self.rect.centerx = self.rect.centerx+self.dx
        self.rect.centery = self.rect.centery+self.dy



        #check Boundaries

        #Check right
        if self.rect.centerx >= 670:
            self.rect.centerx =670
        #Check left
        elif self.rect.centerx <= 220:
            self.rect.centerx = 220
        #Check Bottom
        if self.rect.centery >= 700:
            self.rect.centery = 700
        #Check Top
        elif self.rect.centery <= 200:
            self.rect.centery = 200



    def moveUp(self):
        self.dx=0
        self.dy=-5

    def moveDown(self):
        self.dx =0
        self.dy =5

    def moveLeft(self):
        self.dx =-5
        self.dy = 0

    def moveRight(self):
        self.dx =5
        self.dy =0



class Parcel(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("parcel.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (120,600)

"""
    ================================HUD======================================
"""
#Green Y 
class ItemHUD(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("itemhud.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (1020,200)

#Red A
class WhereHUD(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("where.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (1020, 450)

#Small Green
class TimeHUD(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("timehud.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (915, 850)


#Yellow
class GoldHUD(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("gold.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (1020, 620)


"""
    ==============================PARCEL OBJECTS==============================
"""
class Potion(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("potion.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (950, 200)

class Book(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("book.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (950, 200)

class Net(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("net.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (950, 200)

class Pendant(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("pendant.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (950, 200)

class Tunic(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("tunic.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (950, 200)



"""
   ===================================Rupees==================================
"""

class GreenRupee(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("GreenRupee.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

class BlueRupee(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("BlueRupee.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

class RedRupee(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("RedRupee.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

"""
   ------------------------------------HOUSES---------------------------------
"""
class H23(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("23.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (920, 620)

class Indicator23(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("indicator.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (370,274)

class H24(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("24.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (920, 620)

class Indicator24(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("indicator.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (224,406)

class H25(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("25.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (920, 620)

class Indicator25(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("indicator.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (655,480)

class H26(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("26.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (920, 620)

class Indicator26(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("indicator.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (310,695)

class H28(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("28.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (920, 620)

class Indicator28(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("indicator.png")
        self.rect = self.image.get_rect()
        self.image = self.image.convert()

    def update(self):
        self.rect.center = (655,680)

"""
   ------------------------------------MAIN-----------------------------------
"""
def main():
    pygame.display.set_caption("A Link to the Parcel")
    background = pygame.image.load('village.png').convert()

    allSprites=pygame.sprite.Group()

    #ITEMS
    potion = Potion()
    book = Book()
    net = Net()
    pendant = Pendant()
    tunic = Tunic()

    #HEADS UP DISPLAY
    itemHud = ItemHUD()
    timeHud = TimeHUD()
    goldHud = GoldHUD()
    whereHud = WhereHUD()

    indicator23 = Indicator23()
    indicator24 = Indicator24()
    indicator25 = Indicator25()
    indicator26 = Indicator26()
    indicator28 = Indicator28()

    goldSack = 0

    #HOUSE NUMBERS
    h23 = H23()
    h24 = H24()
    h25 = H25()
    h26 = H26()
    h28 = H28()

    packages = [Potion, Book, Net, Pendant, Tunic]
    packagetype = random.choice(packages)




    #Player
    paperboy = PaperBoy(200)
    parcel = Parcel()
    package = packagetype()

    #Sprites added to AllSprites Group
    allSprites.add(paperboy)
    allSprites.add(parcel)
    allSprites.add(itemHud)
    allSprites.add(timeHud)
    allSprites.add(goldHud)
    allSprites.add(whereHud)



    font = pygame.font.Font(None, 25)




    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            elif event.type == pygame.KEYUP:
                if event.key==pygame.K_UP:
                    paperboy.moveUp()
                elif event.key==pygame.K_DOWN:
                    paperboy.moveDown()
                elif event.key==pygame.K_LEFT:
                    paperboy.moveLeft()
                elif event.key==pygame.K_RIGHT:
                    paperboy.moveRight()

            if pygame.sprite.collide_rect(paperboy, parcel):

                allSprites.add(package)

                if packagetype == packages[0]:
                    allSprites.add(potion)
                    allSprites.add(h23)
                    allSprites.add(indicator23)
                    allSprites.remove(parcel)


                elif packagetype == packages[1]:
                    allSprites.add(book)
                    allSprites.add(h24)
                    allSprites.add(indicator24)
                    allSprites.remove(parcel)

                elif packagetype == packages[2]:
                    allSprites.add(net)
                    allSprites.add(h25)
                    allSprites.add(indicator25)
                    allSprites.remove(parcel)

                elif packagetype == packages[3]:
                    allSprites.add(pendant)
                    allSprites.add(h26)
                    allSprites.add(indicator26)
                    allSprites.remove(parcel)

                elif packagetype == packages[4]:
                    allSprites.add(tunic)
                    allSprites.add(h28)
                    allSprites.add(indicator28)
                    allSprites.remove(parcel)

这是我的代码遇到错误的地方

            if pygame.sprite.collide__rect(paperboy, indicator23) == True:

如果pygame.sprite.collide__rect(paperboy,indicator23)== True:

AttributeError:'module'对象没有属性'collide__rect'

                allSprites.remove(potion)
                allSprites.remove(h23)
                allSprites.remove(indicator23)
                allSprites.add(parcel)

            elif pygame.sprite.collide_rect(paperboy, indicator24) == True:

                allSprites.remove(book)
                allSprites.remove(h24)
                allSprites.remove(indicator24)
                allSprites.add(parcel)

            elif pygame.sprite.collide_rect(paperboy, indicator25) == True:

                allSprites.remove(net)
                allSprites.remove(h25)
                allSprites.remove(indicator25)
                allSprites.add(parcel)

            elif pygame.sprite.collide_rect(paperboy, indicator26) == True:

                allSprites.remove(pendant)
                allSprites.remove(h26)
                allSprites.remove(indicator26)
                allSprites.add(parcel)

            elif pygame.sprite.collide_rect(paperboy, indicator28) == True:

                allSprites.remove(tunic)
                allSprites.remove(h28)
                allSprites.remove(indicator28)
                allSprites.add(parcel)




        fontTitle = font.render("A Link to the Parcel", True, (255,255,255,))


        screen.blit(background, (0, 0))
        screen.blit(fontTitle, [925,25])


        allSprites.clear(screen, background,)
        allSprites.update()
        allSprites.draw(screen)

        pygame.display.flip()
    pygame.quit()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:2)

你有一个错字。您要调用的函数是collide_rect,带有1个下划线。