无法在pygame中使sprite不可见

时间:2013-10-28 13:18:57

标签: python python-2.7 pygame sprite invisible

所以我正在制作一个基本的游戏,我在游戏地图上有几个“障碍”和“门户”。前者是玩家无法触及的点,后者是改变等级的点。

我试图让它们变得不可见,所以在地图上只有一个不可见的矩形,碰撞检测会注意到,但是现在,当我把它暴露到地图时,我有丑陋的黑色斑点。

我尝试使用脏矩形,但这似乎没有那么好用。

我的代码(或者至少是处理障碍和门户网站的地方):

class Barrier(pygame.sprite.DirtySprite):
    '''class that builds up the invisible barriers in the game'''

    #constructor function
    def __init__(self, posX, posY, width, height): #create a self variable to refer to the object
    #call up the parent's constructor
        pygame.sprite.DirtySprite.__init__(self)
        self.dirty = 1
        self.visible = 0

        #Make the barrier.
        self.image = pygame.Surface([width, height])

        #debug code that makes sure that the barrier is in the right place
        #self.image.fill(white)

        # place the top left corner of the barrier at the given location
        self.rect = self.image.get_rect()
        self.rect.y = posY
        self.rect.x = posX

barriers = pygame.sprite.Group() #global barrier list used in all maps
portals = pygame.sprite.Group() #global portal list used in all maps


class Portal(pygame.sprite.DirtySprite):
    '''class that builds up the portals in the game'''

    #constructor function
    def __init__(self, posX, posY, width, height): #create a self variable to refer to the object
    #call up the parent's constructor
        pygame.sprite.DirtySprite.__init__(self)

        self.dirty = 1
        self.visible = 0

        #Make the barrier.
        self.image = pygame.Surface([width, height])

        #debug code that makes sure that the barrier is in the right place
        self.image.fill(black)

        # place the top left corner of the barrier at the given location
        self.rect = self.image.get_rect()
        self.rect.y = posY
        self.rect.x = posX

def LoadInside():

    barriers.empty()
    portals.empty()
    #Load up the level image
    whichLevel = 1

    background_image = pygame.image.load("House.png").convert()

    #a list of all the barriers in the room
    room_barrier_list = pygame.sprite.Group()

    #make a barrier out of all of the objects in the room
    barrierTopWall = Barrier(0,125,661,7)
    barrierLeftWall= Barrier(5, 130,5, 300)
    barrierBottomWallLeft= Barrier(10,414,292,7)
    barrierBottomWallRight= Barrier(364,412,298,7)
    barrierRightWall= Barrier(649,126,5, 294)
    bed = Barrier(19,199,62,93)
    smallTableChairs = Barrier(273,220,97,39)
    pot = Barrier(300,288,34,31)
    table = Barrier(493,242,151,42)
    chair1 = Barrier(459,255,28,35)
    chair2 = Barrier(490,296,31,28)
    chair3 = Barrier(553,293,31,28)
    chair4 = Barrier(621,292,31,28)

    #make a portal to get out
    door = Portal(300,413, 64,10)


    #add the barriers to the lists
    room_barrier_list.add(barrierTopWall, barrierLeftWall, barrierBottomWallLeft, barrierRightWall)

    barriers.add(room_barrier_list)

    all_sprites_list.add(barriers)

    room_barrier_list.add(smallTableChairs,pot,table,chair1,chair2,chair3,chair4, barrierBottomWallRight, bed)
    barriers.add(room_barrier_list)
    all_sprites_list.add(barriers)

    #add the portal to the list
    all_sprites_list.add(door)

    mainScreen.blit(background_image, [0,0])


#
# The above code handles the room barriers and portals
#


while done==False:

    for event in pygame.event.get(): #user did something
        if event.type == pygame.QUIT: #if the user hit the close button
                done=True

                # Move the player if an arrow key is pressed
        key = pygame.key.get_pressed()
        if key[pygame.K_LEFT]:
            faceWhatDirection = 'left' # set the faceWhatDirection variable
            player.updateLeft() # call up the animation function
            player.move(-10, 0)

        if key[pygame.K_RIGHT]:
            faceWhatDirection = 'right' # set the faceWhatDirection variable
            player.updateRight() # call up the animation function
            player.move(10, 0)

        if key[pygame.K_UP]:
            faceWhatDirection = 'up' # set the faceWhatDirection variable
            player.updateUp() # call up the animation function
            player.move(0, -10)

        if key[pygame.K_DOWN]:
            faceWhatDirection = 'down' # set the faceWhatDirection variable
            player.updateDown() # call up the animation function
            player.move(0, 10)


        #if the user presses the space bar, the attack button
        if key[pygame.K_SPACE]:

                bullet = Bullet()
                bullet_list.append(bullet) #adds bullet to the bullet list
                all_sprites_list.add(bullet) #adds the bullet to the sprite list to be drawn

                #puts the bullet in the same location as player (this needs to be changed to what direction player faces)
                bullet.rect.x = player.rect.x
                bullet.rect.y = player.rect.y + 15 # the + 15 places it at a location that is not on the player's face!
                bullet.bulletDirection = faceWhatDirection

                #checks to see what direction to move the bullet in
    for bullet in bullet_list:
        bullet.move_bullet()

        #see if the bullet hit anything
        barrier_hit_list = pygame.sprite.spritecollide(bullet, barriers, False)

        for barrier in barrier_hit_list:
        #remove the bullet if it hit something
            bullet_list.remove(bullet)
            all_sprites_list.remove(bullet)

    mainScreen.fill(black)#makes the background white, and thus, the white part of the images will be invisible


    if whichLevel == 1:

        LoadInside()


    else:
        LoadOutside()


    #draw the sprites
    all_sprites_list.draw(mainScreen)



    #limit the game to 20 fps
    clock.tick(20)

    #update the screen on the regular
    pygame.display.flip()

pygame.quit()

1 个答案:

答案 0 :(得分:4)

安静轻松,

使用Group.update()方法对sprite进行了blitting。

all_sprites_list.draw(mainScreen)

不要为all_sprites_list添加障碍。 然后他们永远不会在屏幕上搞砸了。

barrier_hit_list仍可用于碰撞。