微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在pygame中关闭对话框,直到玩家位于指定区域附近

如何解决如何在pygame中关闭对话框,直到玩家位于指定区域附近

我正在用python创建游戏,并且只要播放器位于我使用rect函数设置的定义区域内,便希望在屏幕上将对话框(png文件)涂白。如果我加载精灵并尝试直接对其进行blit,由于某种原因它没有显示出来,有人可以帮忙吗?我附上了下面代码的相关部分,整个代码都在我的Github上:

dialogueBox = pygame.image.load('dialogue Box.png')
npc1 = pygame.Rect(192,226,26,32)
player_rect = playerImg.get_rect(topleft=(playerX,playerY))

if player_rect.colliderect(npc1):
    screen.blit(dialogueBox,(0,0))
    if playerX_change > 0:
        player_rect.right = npc1.left
    elif playerX_change < 0:
        player_rect.left = npc1.right
    playerX = player_rect.x  # NPC near patch 


player_rect = playerImg.get_rect(topleft=(playerX,0))
    if playerY_change < 0:
        player_rect.top = npc1.bottom
    elif playerY_change > 0:
        player_rect.bottom = npc1.top
    playerY = player_rect.y  # NPC near patch 1

解决方法

该框未显示,因为屏幕已清除并在redrawgamewindow()中重绘。

在全局名称空间中添加变量current_dialogue

current_dialogue = None

在主应用程序循环中设置变量。 current_dialogue被分配了一个元组((dialoguebox,(0,0))),该元组存储有关对话框的信息。

def game():
    global current_dialogue

    # [...]
 
    while running:
        current_dialogue = None
        
        # [...]

        if player_rect.colliderect(npc1):
            current_dialogue = (dialoguebox,0))
            # [...]

   
        # [...]

        redrawgamewindow()
 
        # [...]

并在redrawgamewindow()的末尾绘制对话框。使用asterisk(*) operator打开元组(scr.blit(*current_dialogue))的包装:

def redrawgamewindow():
    # [...]
    scr.fill((0,0))

    # [...]

    if current_dialogue:
        scr.blit(*current_dialogue)

注意,仅当矩形相交时,对话框才会显示。如果您想让对话框显示玩家是在 near 附近,那么您必须使用增加的矩形进行碰撞测试。使用inflate()来生成一个更改了大小的新矩形:

if player_rect.inflate(5,5).colliderect(npc1):
    current_dialogue = (dialoguebox,0))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。