Python 如何在我的乌龟程序中添加倒计时?

Python 如何在我的乌龟程序中添加倒计时?,python,python-3.x,Python,Python 3.x,我正试图用海龟做一个游戏,玩家必须在设定的时间内点击任意分布的圆圈,但如何在游戏中创建计时器却让我感到困惑。每当我尝试设置计时器时,它都会在游戏开始前倒计时,或者在1圈产生后倒计时。我想让它倒计时游戏开始,并让游戏结束后,计时器结束 我还试图创建一个记分计数器,但我是否使用了circle.onclick错误 这就是我一直想做的: from turtle import Turtle, Screen from random import random, randint import time CU

我正试图用海龟做一个游戏,玩家必须在设定的时间内点击任意分布的圆圈,但如何在游戏中创建计时器却让我感到困惑。每当我尝试设置计时器时,它都会在游戏开始前倒计时,或者在1圈产生后倒计时。我想让它倒计时游戏开始,并让游戏结束后,计时器结束

我还试图创建一个记分计数器,但我是否使用了circle.onclick错误

这就是我一直想做的:

from turtle import Turtle, Screen
from random import random, randint
import time

CURSOR_SIZE = 20

def score():
    num=0
    print("Score: ",num)

def my_circle(color):

    radius = randint(10, 50)

    circle = Turtle('circle', visible=False)
    circle.shapesize(radius / CURSOR_SIZE)
    circle.color(color)
    circle.penup()

    while True:
        nx = randint(2 * radius - width // 2, width // 2 - radius * 2)
        ny = randint(2 * radius - height // 2, height // 2 - radius * 2)

        circle.goto(nx, ny)

        for other_radius, other_circle in circles:
            if circle.distance(other_circle) < 2 * max(radius, other_radius):
                break
        else:
            break

    circle.showturtle()

    circle.onclick(lambda x, y, t=circle: t.hideturtle())
    circle.onclick(num=num+1)

    return radius, circle


screen = Screen()
screen.bgcolor("lightgreen")
screen.title("Speed Clicker")

width, height = screen.window_width(), screen.window_height()

circles = []

for _ in range(0, 20):

    for i in range(40):
        print(str(40-i)+" seconds remain")
        time.sleep(1)

    rgb = (random(), random(), random())

    circles.append(my_circle(rgb))


screen.mainloop()
从海龟导入海龟,屏幕
从random导入random,randint
导入时间
光标大小=20
def分数():
num=0
打印(“分数:”,num)
定义我的圆圈(颜色):
半径=randint(10,50)
圆圈=海龟('圆圈',可见=假)
圆.形状大小(半径/光标大小)
圆形。颜色(颜色)
circle.penup()
尽管如此:
nx=randint(2*radius-width//2,width//2-radius*2)
ny=randint(2*半径-高度//2,高度//2-半径*2)
圆。转到(nx,ny)
对于其他_半径,圆中的其他_圆:
如果圆距离(其他圆)<2*max(半径,其他圆半径):
打破
其他:
打破
circle.showturtle()
onclick(lambda x,y,t=circle:t.hideturtle())
circle.onclick(num=num+1)
返回半径
screen=screen()
屏幕颜色(“浅绿色”)
屏幕标题(“快速点击器”)
宽度,高度=screen.window\u width(),screen.window\u height()
圆圈=[]
对于范围(0,20)内的uu:
对于范围(40)内的i:
打印(str(40-i)+“剩余秒数”)
时间。睡眠(1)
rgb=(随机(),随机(),随机())
圆。附加(my_圆(rgb))
screen.mainloop()

我最不感兴趣的是,是否有可能将计时器和分数打印在游戏上,而不是在Python Shell上。

我做了一些修改,这似乎奏效了。我已经把比分和剩下的时间放在了比赛的标题栏上。谢谢

import turtle
from random import random, randint
import time

CURSOR_SIZE = 20

num=0
def increase_score():
    global num 
    num += 1

def my_circle(color):

    radius = randint(10, 50)

    circle = turtle.Turtle('circle', visible=False)
    circle.shapesize(radius / CURSOR_SIZE)
    circle.color(color)
    circle.penup()

    while True:
        nx = randint(2 * radius - width // 2, width // 2 - radius * 2)
        ny = randint(2 * radius - height // 2, height // 2 - radius * 2)

        circle.goto(nx, ny)

        for other_radius, other_circle in circles:
            if circle.distance(other_circle) < 2 * max(radius, other_radius):
                break
        else:
            break

    circle.showturtle()
    circle.onclick(lambda x,y,t=circle: (circle.hideturtle(), increase_score()))

    return radius, circle


screen = turtle.Screen()
screen.bgcolor("lightgreen")
screen.title("Speed Clicker")

width, height = screen.window_width(), screen.window_height()

circles = []

gameLength = 5
# Higher number means faster blocks
# 1-10
difficulty = 10
startTime = time.time()
while True:
    time.sleep(1/difficulty)

    rgb = (random(), random(), random())

    timeTaken = time.time() - startTime

    circles.append(my_circle(rgb))
    screen.title('SCORE: {}, TIME LEFT: {}'.format(num,int(round(gameLength - timeTaken,0))))

    if time.time() - startTime > gameLength: 
        break

screen.title('FINISHED! FINAL SCORE: {}'.format(num))

screen.mainloop()
导入海龟
从random导入random,randint
导入时间
光标大小=20
num=0
def增加_分数():
全局数
num+=1
定义我的圆圈(颜色):
半径=randint(10,50)
圆圈=海龟。海龟('圆圈',可见=假)
圆.形状大小(半径/光标大小)
圆形。颜色(颜色)
circle.penup()
尽管如此:
nx=randint(2*radius-width//2,width//2-radius*2)
ny=randint(2*半径-高度//2,高度//2-半径*2)
圆。转到(nx,ny)
对于其他_半径,圆中的其他_圆:
如果圆距离(其他圆)<2*max(半径,其他圆半径):
打破
其他:
打破
circle.showturtle()
circle.onclick(lambda x,y,t=circle:(circle.hideturtle(),increase_score())
返回半径
screen=turtle.screen()
屏幕颜色(“浅绿色”)
屏幕标题(“快速点击器”)
宽度,高度=screen.window\u width(),screen.window\u height()
圆圈=[]
游戏长度=5
#数字越大,块速度越快
# 1-10
难度=10
startTime=time.time()
尽管如此:
时间。睡眠(1/困难)
rgb=(随机(),随机(),随机())
timetake=time.time()-startTime
圆。附加(my_圆(rgb))
screen.title('SCORE:{},TIME LEFT:{}'。格式(num,int(round(gameLength-timetake,0)))
如果time.time()-startTime>gameLength:
打破
screen.title('完成!最终分数:{}'。格式(num))
screen.mainloop()

哇,这太完美了。非常感谢much@chankim伟大的请标记为最佳答案,如果它解决了您的问题,请向上投票!谢谢