在我的游戏内计时器继续运行时,如何在由graphics.py制成的窗口内单击?

问题描述

我正在为我的课程创建一个程序,其中涉及使用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,"You have left: " + str(sec+1))
        message.draw(win)
        time.sleep(1)
        if sec == 0:
            message.undraw()
            finalMessage = Text(Point(150,"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文件。谢谢你!

解决方法

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

# 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,"You have left: " + str(secondsRemain))
        message.draw(win)
        if secondsRemain < 0:
            message.undraw()
            finalMessage = Text(Point(150,"You LOST!")
            finalMessage.draw(win)

        clicker.undraw()
        clicker = Text(Point(200,"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,"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祝您好运!