我试图改变我的背景颜色,当一个特定的数字出现在我的屏幕上时,我正在使用一个海龟,使数字出现在屏幕上。问题是,每当数字出现在屏幕上,然后颜色改变,数字就会暂停。我想让颜色每30秒改变一次。
import turtle
import random
#font on Screen
font_setup = ("Arial", 20, "normal")
#backgoround colors
background_colors = ["red", "blue", "green", "orange", "purple", "gold", "azure","beige", "yellow"]
wn = turtle.Screen()
#turtle for timer
counter = turtle.Turtle()
counter.goto(-100,180)
counter.pu()
counter.ht()
#random
cc = random.randint(1,10)
#counter and the background defnition, might be the cause of the problem
counter_interval = 1000
timer = 300
f = 0
famloop = True
def background_change():
global f
while famloop:
wn.bgcolor(background_colors[cc])
f -= 1
def countdown():
global timer, timer_up
counter.clear()
if timer <= 0:
counter.write("Time's Up", font=font_setup)
timer_up = True
else:
#background_change()
counter.write("Timer: " + str(timer), font=font_setup)
if timer == 290:
wn.bgcolor(background_change())
if timer == 280:
wn.bgcolor(background_change())
timer -= 1
counter.getscreen().ontimer(countdown, counter_interval)
wn.ontimer(countdown, counter_interval)
wn.listen()
wn.mainloop()
看起来这里有一个无限循环:
while famloop:
什么会导致famloop变为False?
博比
It's difficult to decode from your text and code what you're trying to do -- if the following doesn't help, please go back and rework your description of the problem you're trying to solve.
我想让颜色每30秒改变一次
对前面的示例进行的以下修改就是这样做的。抛出第二个计时器,它只是按键关闭主倒计时,每30秒改变一次颜色:
from turtle import Screen, Turtle
from itertools import cycle
FONT_SETUP = ('Arial', 24, 'normal')
BACKGROUND_COLORS = ['red', 'blue', 'green', 'orange', 'purple', 'gold', 'azure', 'beige', 'yellow']
COUNTER_INTERVAL = 1000 # milliseconds
BACKGROUND_INTERVAL = 30 # seconds
color = cycle(BACKGROUND_COLORS)
timer = 300 # seconds
def countdown():
global timer
counter.clear()
if timer > 0:
if timer % BACKGROUND_INTERVAL == 0:
screen.bgcolor(next(color))
counter.write("Timer: " + str(timer), align='center', font=FONT_SETUP)
timer -= 1
screen.ontimer(countdown, COUNTER_INTERVAL)
else:
counter.write("Time's Up", align='center', font=FONT_SETUP)
screen = Screen()
counter = Turtle()
counter.hideturtle()
counter.penup()
countdown()
screen.mainloop()