【问题标题】:Score Counter Loop on Mouse Click in PyGamePyGame中鼠标点击时的计分循环
【发布时间】:2020-11-08 05:02:07
【问题描述】:

我正在用 PyGame 编写一个测试游戏,并且有一个基于回合/阶段的系统(就像在 Yu-Gi-Oh!)但是我遇到的问题是当我想增加回合计数器时(在 Draw Phases 上)它会无限期地增加它,直到我再次按下鼠标单击以释放鼠标。有没有办法打破增量并在增量一次后直接进入下一阶段? 我认为问题与游戏循环本身刷新如此之快有关。也许我可以冻结游戏循环?另一种选择是自动(不是通过鼠标点击)从一个转向转向另一个转向,但我仍然需要按下一个按钮才能进入下一个阶段。有任何想法吗?我可以在游戏循环之外增加它吗?

#The Game Phases (outside of the game loop): 
phases = "Coin Flip","Your Draw Phase", "Your Standby Phase", "Your Main Phase 1", "Your Battle Phase", "Your Main Phase 2", "Your End Phase", "Their Draw Phase", "Their Standby Phase", "Their Main Phase 1", "Their Battle Phase", "Their Main Phase 2", "Their End Phase" #enemy phases


#Code Inside the game loop: 
        #If phase = A Draw Phase then the Turn Counter increments.
        if phasecounter == 1: 
            turncounter += 1

        elif phasecounter == 7:
            turncounter += 1

        #A Mouse Click that can will change the Phase on the Screen (displayed by Text). 
        if event.type == pygame.MOUSEBUTTONDOWN:
                phasecounter += 1

            
            
       #Loops the Phases. 
        if phasecounter > 12: 
                    phasecounter = 1

        currentphase = phases[phasecounter]

【问题讨论】:

    标签: loops pygame click counter mouse


    【解决方案1】:

    在您的游戏循环中添加一个标志来跟踪鼠标按钮的点击。在按钮向下设置标志,并在按钮向上取消设置。设置按钮按下事件的变量。

    试试这个更新:

        mousedown = False  # above game loop
    
        #A Mouse Click that can will change the Phase on the Screen (displayed by Text). 
        if event.type == pygame.MOUSEBUTTONUP:
            mousedown = False
            
        if event.type == pygame.MOUSEBUTTONDOWN and not mousedown: # don't repeat
            mousedown = True
            phasecounter += 1
            if phasecounter in [1,7]:
                turncounter += 1
            if phasecounter > 12: 
                phasecounter = 1
    
        currentphase = phases[phasecounter]
    

    【讨论】:

    • 很好的答案,谢谢。但是它仍然不正确,但是我确实解决了它。我会把代码放在这个问题的答案中。
    【解决方案2】:

    感谢 Mike67,他的解决方案确实极大地解决了这个问题。然而,它仍然只是每 2 回合增加一次。我的解决方案是在没有“你的”或“他们的”的情况下制作阶段,并制作一个布尔值来跟踪轮到谁了:

    whoseturn = 0 #0 = your turn; 1 = their turn
    phases = ["Next Turn","Draw Phase", "Standby Phase", "Main Phase 1", "Battle Phase", "Main Phase 2", "End Phase", "Draw Phase", "Standby Phase", "Main Phase 1", "Battle Phase", "Main Phase 2", "End Phase"]
    

    然后在

    中开启Turn Counter
    if event.type == pygame.MOUSEBUTTONDOWN and not mousedown: # don't repeat
                    mousedown = True
                    mousedown = False
                    phasecounter += 1
                    if phasecounter == 0:
                        turncounter += 1
                    if phasecounter == 7:
                        turncounter += 1
                        
                    if phasecounter == 7: 
                        phasecounter = 0
                        
                    
                        
    
            currentphase = phases[phasecounter]
    

    所以每个列表只需要增加一次而不是两次。现在它会在“下一回合”完美地增加。

    【讨论】:

      猜你喜欢
      • 2021-09-06
      相关资源
      最近更新 更多