是否可以取消隐藏图像?

问题描述 投票:0回答:1

在我的情况下,我的心脏滤罐被涂在屏幕上,并且将心脏滤罐添加到列表中。当我的播放器撞击心脏滤罐时,我希望心脏滤罐和直肠消失。我已经完成所有碰撞的设置,以便当播放器撞击心脏时,其矩形将从列表中删除,但是我无法弄清楚如何从屏幕上取消堵塞/移除心脏罐。这是我的相关代码...

def room_2(): #Only called once, when player enters room
    global tile_rects
    global heart_rects
    global halfheart_rects
    global r_x 
    global r_y
    global room
    room = room2
    tile_rects = []
    heart_rects = [] 
    halfheart_rects = [] #List for halfheart_rects being emptied
    screen.fill((0, 0, 255))
    r_y = 0
    for layer in room2: #Loop through an array, and translate numbers from the array to things                   on screen
        r_x = 0
        for tile in layer:  
            if tile == '4':
                screen.blit(floor_halfheart, (r_x*40- 8, r_y*40)) #Blit heart in location wanted
                halfheart_rects.append(pygame.Rect(r_x*40, r_y*40, 39, 39)) #Add rect to list
            if tile == '3':
                bat_create()
            if tile == '2':
                screen.blit(stone_block, (r_x*40, r_y*40))
                tile_rects.append(pygame.Rect(r_x*40, r_y*40, 40, 40))                     
            if tile == '1':
                screen.blit(stone_wall, (r_x*40, r_y*40))    
                tile_rects.append(pygame.Rect(r_x*40, r_y*40, 39, 39))
            r_x += 1
        r_y += 1 

for hh in halfheart_rects: #Loop through halfheart_rects list
    halfheart_collision = player_rect.colliderect(hh) #Collision between player and single heart
    if halfheart_collision:
        if player_health < player_health_max: 
            player_health += 1
            room[hh.y // 40][hh.x//40] = '0' #Removing heart canister number from array for when nect entering room
            halfheart_rects.remove(hh)

我曾尝试在if player_health < player_health_max:中用彩色矩形覆盖心脏罐的位置,但以下几行仅运行了一次,因此矩形闪烁然后消失。预先感谢!

python list pygame rect blit
1个回答
0
投票

您需要将其转换为2个功能,其中1个用于设置所有功能,而1个用于绘制,因为您应该在每一帧(而不是一次)上抽动心罐。这意味着您不需要解散,就可以不解散旧的将被掩盖。您也可以将它们移至绘制播放器的功能。常规设置应类似于

def draw_screen():
    screen.fill((0, 0, 255))
    player.draw()
    for heart in halfheart_rects:
        screen.blit(floor_halfheart, (hear.x, heart.y)) 

def setup_room_2()
    global tile_rects
    global heart_rects
    global halfheart_rects
    global r_x 
    global r_y
    global room
    room = room2
    tile_rects = []
    heart_rects = [] 
    halfheart_rects = []
    screen.fill((0, 0, 255))
    r_y = 0
    for layer in room2:
        r_x = 0
        for tile in layer:  
            if tile == '4':
                halfheart_rects.append(pygame.Rect(r_x*40, r_y*40, 39, 39))
            if tile == '3':
                bat_create()
            if tile == '2':
                tile_rects.append(pygame.Rect(r_x*40, r_y*40, 40, 40))                     
            if tile == '1':    
                tile_rects.append(pygame.Rect(r_x*40, r_y*40, 39, 39))
            r_x += 1
        r_y += 1 


while running:
    draw_screen()
    if player_enters_room_1:
        setup_room_1()
    elif player_enters_room_2:
        setup_room_2()
for hh in halfheart_rects:
    halfheart_collision = player_rect.colliderect(hh)
    if halfheart_collision:
        if player_health < player_health_max: 
            player_health += 1
            room[hh.y // 40][hh.x//40] 
            halfheart_rects.remove(hh)

for循环的末尾很糟糕,因为当您从列表中删除一个循环时,可以说它的第二个,第三个变成第二个,因此循环会跳过它,转到第三个(第四个)

如果这没有帮助,将代码放在您绘制所有内容的地方,我会看一下。

© www.soinside.com 2019 - 2024. All rights reserved.