Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle # import the turtle module
- import random
- import time
- # This function turns playerTurtle to the right by 90 degrees. It is
- # called when the user presses the right arrow key.
- def turn_right():
- playerTurtle.right(90)
- def turn_left():
- playerTurtle.left(90)
- def relocate_FoodTurtle():
- x = random.randint(-200,200)
- y = random.randint(-200,200)
- foodTurtle.penup()
- foodTurtle.goto(x,y)
- def relocate_FoodTurtle():
- x = random.randint(-200,200)
- y = random.randint(-200,200)
- foodTurtle.penup()
- foodTurtle.goto(x,y)
- playerTurtle = turtle.Turtle() # create a new turtle
- # -- Main Section
- window = turtle.Screen() # get the screen where turtles live
- turtle.setup(500, 500) # set the screen to 500 x 500 in size
- playerTurtle = turtle.Turtle() # create a new turtle
- playerTurtle.shape("turtle") # set shape to turtle
- playerTurtle.penup() # suspend drawing
- foodTurtle = turtle.Turtle()
- foodTurtle.shape("square") # set shape to square
- foodTurtle.fillcolor("blue") # set the turtle fill color to blue
- foodTurtle.speed(0) # set speed to 0, the fastest speed
- foodTurtle.penup() # suspend drawing
- foodTurtle.goto(100, 100) # move the turtle to 100, 100
- window.onkey(turn_right, "Right") # call turn_right when Right pressed
- window.onkey(turn_left, "Left")
- window.listen()
- speed = 1
- blocks = 0
- startTime = time.time()
- game = True
- while game == True:
- playerTurtle.forward(speed)
- if playerTurtle.distance(foodTurtle) <= 15:
- print("hit") # prints "hit" to the console screen
- relocate_FoodTurtle()
- blocks = blocks + 1
- if blocks%2 == 0:
- speed = speed + 1
- if playerTurtle.xcor() < -250 or playerTurtle.xcor() > 250 or playerTurtle.ycor() > 250 or playerTurtle.xcor() < -250:
- playerTurtle.goto(0,0)
- if blocks == 10:
- game = False
- endTime = time.time()
- print("Elapsed time: " + str(endTime – startTime))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement