是否可以取消照亮图像?

2024-05-29 03:30:52 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的场景中,我将心脏容器快速显示在屏幕上,并将心脏容器的矩形添加到列表中。当我的玩家击中心脏筒时,我希望心脏筒和直肠消失。我已经设置好了所有的碰撞,这样当玩家击中心脏时,它的rect将从列表中删除,但我不知道如何从屏幕上取消blit/删除心脏容器。这是我的相关代码

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:中用一个彩色的rect覆盖心脏容器所在的位置,但是下面的行只运行一次,所以rect闪烁然后消失。提前谢谢


Tags: rectforifhhscreenglobal容器room
1条回答
网友
1楼 · 发布于 2024-05-29 03:30:52

您需要将其转换为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循环很糟糕,因为当你从列表中删除一个时,假设它是第二个,第三个变成第二个,所以循环跳过它并转到第三个(第四个)

如果这没有帮助,把代码放在你画所有东西的地方,我会看一看

相关问题 更多 >

    热门问题