因此,我正在制作一个基本的pygame,并且在游戏地图上有几个“障碍”和“门户”。前者是玩家无法触摸的地方,而后者则是改变等级的地方。

我试图使它们不可见,因此在地图上只有一个不可见的矩形,碰撞检测会注意到该矩形,但是现在,当我将其蒙皮到地图上时,我看到了难看的黑色斑点。

我尝试使用脏矩形,但这似乎效果不佳。

我的代码(或至少是处理障碍和门户的地点):

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

最佳答案

它很安静,

精灵用Group.update()方法钝化。

all_sprites_list.draw(mainScreen)


不要将障碍添加到all_sprites_list。
然后它们再也不会在屏幕上流血了。

但是barrier_hit_list仍然可以用于碰撞。

关于python - 无法在PyGame中使 Sprite 不可见,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19635896/

10-13 03:32