【问题标题】:How can I click inside of my window, made with graphics.py, while my in-game timer continues?如何在我的游戏内计时器继续时单击由 graphics.py 制作的窗口内部?
【发布时间】:2020-09-19 01:03:36
【问题描述】:

我正在为我的班级创建一个程序,其中涉及使用graphics.py by John Zellesdefinitions that each function within graphics.py can do 创建一个简单的游戏。

我的游戏基本上有一个可见的计时器和计数器,用于检查窗口被点击了多少次。用户应该在窗口中单击以启动 60 秒计时器。然后,游戏会显示一个数字,告诉用户他们必须在窗口中点击多少次才能赢得游戏。

如果在玩家完成点击次数之前计时器用完,那么他们就输了。

但是,在我的代码中,每当我单击窗口开始游戏时,计时器就会启动,但它不会显示消息告诉我需要在窗口中单击多少次才能赢得游戏。它只是等待计时器完成,然后让我在窗口内单击。

'''

def main():

    win = GraphWin('Test', 300, 300)
    win.setBackground("green")

    message = Text(Point(150, 30), "Click to start the game")
    message.draw(win)

    win.getMouse()
    start = True

    sec = 5

    timer = start
    secondsRemain = 5

    for i in range(secondsRemain):
        message.undraw()
        sec -= 1
        message = Text(Point(150, 30), "You have left: " + str(sec+1))
        message.draw(win)
        time.sleep(1)
        if sec == 0:
            message.undraw()
            finalMessage = Text(Point(150, 30), "You LOST!")
            finalMessage.draw(win)
        else:
            continue

     clickCounter = randrange(200)
     while clickCounter > 0:
         clicker = Text(Point(200, 50), "Click the window " + str(clickCounter) +" times!")
         clicker.draw(win)
         win.getMouse()
         clickCounter -= 1
         clicker.undraw()
         if clickCounter == 0:
             eye1.undraw()

     win.getMouse()
     win.close()

main()

'''

我也不想再导入任何库(如果可能的话)。我想主要且只使用 graphics.py 文件。谢谢!

【问题讨论】:

    标签: python for-loop time while-loop timer


    【解决方案1】:

    我可以看到一些关于这个的小修复。通过使用 time.sleep(1),您实际上导致脚本在该行停止并忽略所有其他逻辑,然后非常快速地执行所有其他逻辑,再次跳回睡眠状态。我的解决方案是通过使用 time.time() 连续检查时间而不是睡眠来替换它,因此您不需要任何其他库,因为您已经在使用时间。此外,通过在另一个循环之外有一个循环,它使得跟踪何时调用线路变得更加复杂,所以我为你替换了它。我希望对以下脚本的修改可以帮助您更快地飘动我发现的屏幕增加了一些紧迫性。

    # Edited by Bryce
    
    from graphics import *
    from random import *
    import time
    
    def main():
    
        win = GraphWin('Face Hitter!', 300, 300)
        win.setBackground("blue")
        
        clickCounter = randint(50, 100) ## Adjust minimum and maximum for difficulty
    
        message = Text(Point(150, 30), "Click to start the game")
        clicker = Text(Point(200, 50), "Click the window " + str(clickCounter) +" times!")
        message.draw(win)
    
        win.getMouse()
        start = True
    
        initialTime = time.time()
        secondsRemain = 60 ## A full minute
    
    
        while (secondsRemain > 0):
            print(secondsRemain)
            secondsRemain = 60 - (time.time() - initialTime) ## Check how long has elapsed since originally checking the time
            message.undraw()
            message = Text(Point(150, 30), "You have left: " + str(secondsRemain))
            message.draw(win)
            if secondsRemain < 0:
                message.undraw()
                finalMessage = Text(Point(150, 30), "You LOST!")
                finalMessage.draw(win)
    
            clicker.undraw()
            clicker = Text(Point(200, 50), "Click the window " + str(clickCounter) +" times!")
            clicker.draw(win)
            if (win.checkMouse()):
                clicker.undraw()
                clickCounter -= 1
            if clickCounter == 0:
                clicker.undraw()
                clicker = Text(Point(200, 50), "YOU WON!")
                clicker.draw(win)
                #eye1.undraw() ## Remove the # when using it, eye1 wasn't declared in this section of the code so I could test it
                print("Undraw eye - End of game")
                break
    
        print("Waiting for mouse click to quit...")
        win.getMouse()
        win.close()
    
    main()
    

    祝你的程序 Omar 好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      相关资源
      最近更新 更多