Turtle Race

May 20th, 2021
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from turtle import Turtle
2. from random import randint
3.
4. def startPos(myTurtle,x,y):
5.     #set starting positions for the myTurtle object
6.     myTurtle.penup()
7.     myTurtle.goto(x,y)
8.     myTurtle.pendown()
9.
10. def designTurtle(myTurtle,colour,shape):
11.     #set colour and shape of the myTurtle object
12.     myTurtle.color(colour)
13.     myTurtle.shape(shape)
14.
15. def checkWinner(myTurtle):
16.     #check if the x value of myTurtle is greater than 200
17.     return (myTurtle.pos()[0]) > 200
18.
19. def declareWinner(t1,t2,t3):
20.     if t1.pos()[0] > t2.pos()[0] and t1.pos()[0] > t3.pos()[0]:
21.         return 'Turtle 1'
22.     elif t2.pos()[0] > t1.pos()[0] and t2.pos()[0] > t3.pos()[0]:
23.         return 'Turtle 2'
24.     else:
25.         return 'Turtle 3'
26.
27. #Main Program
28. t1 = Turtle()
29. designTurtle(t1,'red','turtle')
30.
31. t2 = Turtle()
32. designTurtle(t2,'green','arrow')
33.
34. t3 = Turtle()
35. designTurtle(t3,'blue','triangle')
36.
37. startPos(t1,-200,200)
38. startPos(t2,-200,150)
39. startPos(t3,-200,100)
40.
41.
42. winner = False
43. while not winner:
44.     t1.forward(randint(1,9))
45.     t2.forward(randint(2,8))
46.     t3.forward(randint(3,7))
47.     winner = (checkWinner(t1) or checkWinner(t2) or checkWinner(t3))
48.
49. print('The winner is {}'.format(declareWinner(t1,t2,t3)))
50.
51.