So I am making a game in Python, and I am almost done, though I am stumped. There are a few things I want:
- I want to make a 30 second timer. The player has 30 seconds to collect as many pellets as he/she can.
- Second, after the timer is complete, the turtle is not controllable anymore.
What do I need to do to fix these?
Screen setup
import turtle
import math
import random
#screen
wn=turtle.Screen()
wn.bgcolor("lightblue")
speed=1
wn.tracer(2)
#Score Variable
score=0
#Turtle Player
spaceship= turtle.Turtle()
spaceship.pensize(1)
spaceship.color("red")
spaceship.penup()
turtle.delay(3)
spaceship.shapesize(1,1)
add=1
#Create Goals
maxpoints = 6
points = []
for count in range(maxpoints):
points.append(turtle.Turtle())
points[count].color("green")
points[count].shape("circle")
points[count].penup()
points[count].goto(random.randint(-300,300), random.randint(-200,200))
#Border
border = turtle.Turtle()
border.penup()
border.goto(-300,-200)
border.pendown()
border.pensize(5)
border.color("darkblue")
for side in range(2):
border.forward(600)
border.left(90)
border.forward(400)
border.left(90)
#Functions
def left():
spaceship.left(30)
def right():
spaceship.right(30)
def increasespeed():
global speed
speed += 1
def decreasespeed():
global speed
speed -= 1
def iscollision(turtle1,turtle2):
collect = math.sqrt(math.pow(turtle1.xcor()-turtle2.xcor(),2)+ math.pow(turtle1.ycor()-turtle2.ycor(),2))
if collect <20:
return True
else:
return False
Keyboard binding to move turtle around
#Keyboard Bindings
turtle.listen()
turtle.onkey(left,"Left")
turtle.onkey(right,"Right")
turtle.onkey(increasespeed ,"Up")
turtle.onkey(decreasespeed ,"Down")
turtle.onkey(quit, "q")
pen=100
while True:
spaceship.forward(speed)
#Boundary
if spaceship.xcor()>300 or spaceship.xcor()<-300:
spaceship.left(180)
if spaceship.ycor()>200 or spaceship.ycor()<-200:
spaceship.left(180)
#Point collection
for count in range(maxpoints):
if iscollision(spaceship, points[count]):
points[count].goto(random.randint(-300,300), random.randint(-200,200))
score=score+1
add+=1
spaceship.shapesize(add)
#Screen Score
border.undo()
border.penup()
border.hideturtle()
border.goto(-290,210)
scorestring = "Score:%s" %score
border.write(scorestring,False,align="left",font=("Arial",16,"normal"))
At the end of my program after the timer finishes, I want the turtle to stop moving; the user is unable to move the turtle.