我如何制作一组精灵并随机选择一个?

时间:2013-12-12 20:50:41

标签: python pygame sprite

这是我的第一篇文章。我需要为我的最终项目制作一个有效的游戏。基本上这个想法是让角色与一个物品精灵发生碰撞,并在屏幕上显示一个随机物品,告诉玩家物品是什么以及在哪里拍摄。

#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


"""
        mousex, mousey = pygame.mouse.get_pos()
        self.rect.centery = mousey
        #Check X boundary.
        if mousex >= 670:
            self.rect.right = 670
        elif mousex <= 210:
            self.rect.left = 210
        else:
            self.rect.centerx = mousex

        #Check Y boundary.
        if mousey >= 670:
            self.rect.top = 670
        elif mousey >= 250:
            self.rect.top = 250
        if mousex >= 250 and mousey >= 220:
            self.rect.left = 250
            self.rect.top = 670

        else:
            self.rect.centery = mousey
            """
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 = (200,600)
        """(random.randint(300,800)),(random.randint(300,800))"""

"""
    ================================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 = (950,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 = (950, 350)

#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 = (915, 700)


"""
    =================================HUD OBJECTS==============================
"""


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

    allSprites=pygame.sprite.Group()

    parcel = Parcel()

    #Heads up Display 
    itemHud = ItemHUD()
    timeHud = TimeHUD()
    goldHud = GoldHUD()
    whereHud = WhereHUD()


    #Player
    paperboy = PaperBoy(200)

    #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)

    goldSack = 0


    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()





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


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


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

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

if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:1)

如果我明白你的意思,你就有很多Sprite种类型,你想随意选择一种。要做到这一点,只需将它们全部放在list中,使用random.choice选择一个,然后实例化它。像这样:

class OneKindOfParcel(Parcel): # etc.
class AnotherKindOfParcel(Parcel): # etc.
class AThirdKindOfParcel(Parcel): # etc.

parcels = [OneKindOfParcel, AnotherKindOfParcel, AThirdKindOfParcel]

# … later …

parceltype = random.choice(parcels)
parcel = parceltype()
allSprites.add(parcel)

你可能想给它一个位置,并显示一些关于它的名称和位置等等,但我想你知道如何做到这一点。

答案 1 :(得分:0)

Quick and dirty是一个项目列表,然后使用random.choice模块中的random