【问题标题】:Turtle Background Change {python}海龟背景变化{python}
【发布时间】:2021-03-03 22:12:55
【问题描述】:

当屏幕上出现特定数字时,我正在尝试更改背景颜色 我正在使用乌龟使数字出现在屏幕上。问题是每当数字出现在屏幕上然后颜色改变数字就会暂停。我希望颜色每 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()

【问题讨论】:

  • 这可能不合适,但是关于您刚刚删除的帖子和您的猜数字游戏,您可能想在codereview.stackexchange.com 上发布您的代码,这真的很有帮助;)

标签: python turtle-graphics python-turtle


【解决方案1】:

看起来你这里有一个无限循环:

  while famloop: 

什么会导致 famloop 变为 False?

鲍比

【讨论】:

  • 我刚才说的是真的,因为我希望循环持续运行。
  • 如果 wn.bgcolor(background_colors[cc]) 没有改变 famloop 的值,那么你第一次调用 background_change 时会一直循环。
【解决方案2】:
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()

【讨论】:

  • 这有点像我想做的。我应该提出我的问题,即我希望每 30 秒后颜色改变一次。感谢您抽出时间来完成所有这些工作。
  • @FutureTJ,我已经重新设计(简化)了我的示例以按照您的描述工作。
  • 谢谢!我不知道它会这么简单,我只是让自己变得更复杂。
猜你喜欢
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多