简体   繁体   中英

Turtle Background Change {python}

I am trying to change my background color when a specific number appears on my screen I am using a turtle to make the number appear on the screen. The thing is whenever the number appears on the screen and then the color changes the number pauses. I will like the color to change every 30 seconds.

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()

It looks like you have an infinite loop here:

  while famloop: 

What would cause famloop to change to False?

Bobby

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.

I wanted the color to change after every 30 seconds

The following rework of my earlier example does that. Tossing the second timer, it just keys off the main countdown to change the color every 30 seconds:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM