I'm currently working on a program for my class where you click the turtle and it moves to a different spot for you to click on it again, the more you click the turtle the more the score goes up. I have implemented a timer with an "if" statement so the game stops when the timer goes to zero, however, the if statement I wrote doesn't work the way I intended it. It says I have put in a bad input for that line of code. Does anyone know how to help fix this?
Here is the Code
#-----import statements-----
import turtle as trtl
import random as rand
#-----game configuration----
dizzy = trtl.Turtle()
dizzy_color = "red"
dizzy_size = 3
dizzy_shape = "circle"
#Score
score = 0
score_writer = trtl.Turtle()
font_setup = ("Arial", 20, "normal")
#Timer
counter = trtl.Turtle()
font_setup = ("Arial", 20, "normal")
timer = 5
counter_interval = 1000 #1000 represents 1 second
timer_up = False
#-----initialize turtle-----
dizzy.shape(dizzy_shape)
#dizzy.shapesize(dizzy_size)
dizzy.fillcolor(dizzy_color)
#This is for where the Score will be written
score_writer.penup()
score_writer.hideturtle()
score_writer.goto(-200,160)
score_writer.showturtle()
score_writer.pendown()
#this is where the timer will be written
counter.penup()
counter.hideturtle()
counter.goto(-200,140)
counter.showturtle()
counter.pendown()
#-----game functions--------
def countdown(): #this controls the timer
global timer, timer_up
counter.clear()
if timer <= 0:
counter.write("Time's Up", font=font_setup)
timer_up = True
else:
counter.write("Timer: " + str(timer), font=font_setup)
timer -= 1
counter.getscreen().ontimer(countdown, counter_interval)
def change_position(): #this makes the turtle(dizzy) got to a different spot once clicked
new_xpos = rand.randint(-100, 100)
new_ypos = rand.randint(-50, 50)
dizzy.penup()
dizzy.hideturtle()
dizzy.goto(new_xpos,new_ypos)
dizzy.showturtle()
dizzy.pendown()
def update_score_for_dizzy():#handles the scoring system
global score
score += 1
score_writer.clear()
score_writer.write(score, font=font_setup)
def dizzy_clicked(x,y):
update_score_for_dizzy()
change_position()
if timer_up = True(): #this is where the issue is
in function spot_click
get global timer
if timer is not done
update the score
change turtle position
otherwise
hide the turtle
#-----events----------------
dizzy.onclick(update_score_for_dizzy)
dizzy.onclick(dizzy_clicked)
wn = trtl.Screen()
wn.ontimer(countdown, counter_interval)
wn.mainloop()
Aucun commentaire:
Enregistrer un commentaire