PYGAME精灵不会在新的位置产卵

时间:2013-12-17 00:02:47

标签: python pygame

好吧,所以游戏的目标是跑过包裹。这将显示包裹包含的物品以及从哪里取出物品。将包裹放到该位置后,我们的想法是为下一个包裹提供一个新的随机生成的项目和位置。我的问题是项目和位置似乎不会产生到一个新的位置,只是第一次滚动随机,并继续吐出相同的项目和位置。谁能帮助我?

这是我的代码

#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 = (430,420)

"""
    ================================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)
    indicators = [Indicator23, Indicator24, Indicator25,
                  Indicator26, Indicator28]
    indicatortype = random.choice(indicators)
    #Player
    paperboy = PaperBoy(200)
    parcel = Parcel()
    package = packagetype()
    where = indicatortype()
    #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):
            if packagetype == packages[0]:
                allSprites.add(package)
                allSprites.add(where)
                allSprites.remove(parcel)
            elif packagetype == packages[1]:
                allSprites.add(package)
                allSprites.add(where)
                allSprites.remove(parcel)
            elif packagetype == packages[2]:
                allSprites.add(package)
                allSprites.add(where)
                allSprites.remove(parcel)
            elif packagetype == packages[3]:
                allSprites.add(package)
                allSprites.add(where)
                allSprites.remove(parcel)
            elif packagetype == packages[4]:
                allSprites.add(package)
                allSprites.add(where)
                allSprites.remove(parcel)

        if pygame.sprite.collide_rect(paperboy, where) == True:
            allSprites.remove(package)
            allSprites.add(parcel)
            allSprites.remove(where)
        if pygame.sprite.collide_rect(paperboy, where) == True:
            allSprites.remove(package)
            allSprites.add(parcel)
            allSprites.remove(where)
        if pygame.sprite.collide_rect(paperboy, where) == True:
            allSprites.remove(package)
            allSprites.add(parcel)
            allSprites.remove(where)
        if pygame.sprite.collide_rect(paperboy, where) == True:
            allSprites.remove(package)
            allSprites.add(parcel)
            allSprites.remove(where)
        if pygame.sprite.collide_rect(paperboy, where) == True:
            allSprites.remove(package)
            allSprites.add(parcel)
            allSprites.remove(where)

        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 :(得分:0)

这是一些伪代码:

在播放器类的更新方法中执行以下操作:

if self.rect.colliderect(parcelLocation):
    parcelLocation.kill()

    newparcel = Parcel((random_x, random_y))
    newLocation = Location((random_x, random_y))

在Parcel和Location sprite类中为它们提供一个名为pos的参数,这样当它们获得(random_x, random_y)时,每次实例化一个新的

时,它们将被放在这个随机位置。

希望这有点帮助

祝你好运! :)