-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTurtle Race!.py
More file actions
82 lines (67 loc) · 2.28 KB
/
Turtle Race!.py
File metadata and controls
82 lines (67 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/python3
from turtle import * #To import the draw functions
from random import randint #To import the random numbers function
speed(0) #This initialises the speed to max. We can also give it any number to adjust the speed
penup() #To make sure that it does not start writing from the beginning it moves.
goto(-140,140) #Almost top left portion
for i in range(15): #To draw the racing ground and also the numbers
write(i, align='center') #To align the numbers with the lines, by default it is left aligned.
right(90) #To turn right by 90 degree
forward(10) #To move 10 pixels forward
pendown() #Now we need to draw the lines
for j in range(16): #This for loop is for dashed lines in the track
forward(10)
if(j%2==0): #This if statement helps for the dashed lines
penup()
else:
pendown()
penup()
backward(170) #This is to go back, in this case, retrace.
left(90) #This is to turn left by 90 degree
forward(20)
a=Turtle() #This is our first contender
a.color('red') #This specifies the colour
a.shape('turtle') #This specifies the shape
a.penup()
a.goto(-160,110) #Starting point of the race
a.pendown()
for roll in range(4): #This is just to do a barrel roll as it reaches the starting point
a.right(90)
b=Turtle()
b.color('blue')
b.shape('turtle') #You can also add different shapes like arrow, circle, square, triangle or classic
b.penup()
b.goto(-160,80)
b.pendown()
for roll in range(4):
b.right(90)
c=Turtle()
c.color('green')
c.shape('turtle')
c.penup()
c.goto(-160,50)
c.pendown()
for roll in range(4):
c.right(90)
d=Turtle()
d.color('purple')
d.shape('turtle')
d.penup()
d.goto(-160,20)
d.pendown()
for roll in range(4):
d.right(90)
e=Turtle()
e.color('orange')
e.shape('turtle')
e.penup()
e.goto(-160,-10)
e.pendown()
for roll in range(4):
e.right(90)
for turn in range(100): #And the race begins
a.forward(randint(1,5)) #As anyone can see, the maximum the turtle may move is 500 pixels
b.forward(randint(1,5)) #And the minimum they may move is 100
c.forward(randint(1,5)) #But in any case, we will have our winner
d.forward(randint(1,5)) #Thanks to the random function
e.forward(randint(1,5))