Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法使精灵在pygame中不可见_Python_Python 2.7_Pygame_Sprite_Invisible - Fatal编程技术网

Python 无法使精灵在pygame中不可见

Python 无法使精灵在pygame中不可见,python,python-2.7,pygame,sprite,invisible,Python,Python 2.7,Pygame,Sprite,Invisible,所以我正在制作一个基本的pygame,在游戏地图上有几个“屏障”和“门户”。前者是玩家无法触摸的点,后者是改变水平的点 我试图使它们不可见,所以在地图上有一个不可见的矩形,碰撞检测会注意到,但是现在,当我将它点到地图上时,我有丑陋的黑色斑点 我试着使用脏矩形,但效果似乎不太好 我的代码(或至少是处理屏障和门户的点): 这很简单 精灵通过Group.update()方法进行了blite 不要在“所有精灵”列表中添加障碍。 然后他们就再也不会出现在屏幕上了 但是障碍物列表仍然可以用于碰撞。它非常简单

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

我试图使它们不可见,所以在地图上有一个不可见的矩形,碰撞检测会注意到,但是现在,当我将它点到地图上时,我有丑陋的黑色斑点

我试着使用脏矩形,但效果似乎不太好

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

这很简单

精灵通过Group.update()方法进行了blite

不要在“所有精灵”列表中添加障碍。 然后他们就再也不会出现在屏幕上了

但是障碍物列表仍然可以用于碰撞。

它非常简单

精灵通过Group.update()方法进行了blite

不要在“所有精灵”列表中添加障碍。 然后他们就再也不会出现在屏幕上了


但是障碍物列表仍然可以用于碰撞。

你不能让矩形透明吗?你不能让矩形透明吗?总是简单的事情。那就是通宵睡对你脑袋的影响。谢谢总是简单的事情。那就是通宵睡对你脑袋的影响。谢谢
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()
all_sprites_list.draw(mainScreen)