-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
142 lines (127 loc) · 4.33 KB
/
Main.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'''
Title: A simple Turtle Race Game
Author: Daljeet Singh Chhabra
Language: Python
Date Created: 13-08-2018
Last Modified: 13-08-2018
'''
from turtle import *
from random import randint
import time
class Game:
def __init__(self):
self.color = ["red", "white", "blue", "yellow"] # List of turtle colors
self.turt = [] # list name turt
self.x = 0
def path(self):
for i in range(14):
pencolor("white") # Loop for making lanes
write(i)
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)
if i == 20:
x, y = position() # To find position of last lane and store it in x,y
self.x = x
penup()
ht() # To hide the sign of turtle
pendown()
def positionTurtle(self):
y = 230
for i in range(4):
self.turt.append(Turtle()) # Placing the turtles on starting line
self.turt[i].right(360)
self.turt[i].color(self.color[i])
self.turt[i].shape("turtle")
self.turt[i].penup()
self.turt[i].goto(-270, y)
y = y - 40
self.turt[i].pendown()
def countdown(self):
w = Turtle()
w.color("black")
w.penup()
w.goto(0, 10)
w.pendown()
w.write("3", move=False, align="center", font=("Arial", 50, "normal"))
time.sleep(1)
w.clear()
w.write("2", move=False, align="center", font=("Arial", 50, "normal"))
time.sleep(1)
w.clear()
w.write("1", move=False, align="center", font=("Arial", 50, "normal"))
time.sleep(1)
w.clear()
w.write("Go", move=False, align="center", font=("Arial", 50, "normal"))
time.sleep(1)
penup()
def move(self):
for i in range(140):
self.turt[0].forward(randint(1, 5)) # To move Turtles at random speed
a, b = self.turt[0].position()
if self.x <= a: # To check the Winner
self.turt[0].right(360)
ht()
penup()
goto(0, -70)
pendown()
pencolor("red")
write("Red Colour Turtle won the game", move=False, align="center",
font=("Times Roman New", 25, "normal"))
break
self.turt[1].forward(randint(1, 5))
a, b = self.turt[1].position()
if self.x <= a:
self.turt[1].right(360)
ht()
penup()
goto(0, -70)
pendown()
pencolor("white")
write("White Colour Turtle won the game", move=False, align="center",
font=("Times Roman New", 25, "normal"))
break
self.turt[2].forward(randint(1, 5))
a, b = self.turt[2].position()
if self.x <= a:
self.turt[2].right(360)
ht()
penup()
goto(0, -70)
pendown()
pencolor("blue")
write("Blue Colour Turtle won the game", move=False, align="center",
font=("Times Roman New", 25, "normal"))
break
self.turt[3].forward(randint(1, 5))
a, b = self.turt[3].position()
if self.x <= a:
ht()
self.turt[3].right(360)
penup()
goto(0, -70)
pendown()
pencolor("Yellow")
write("Yellow Colour Turtle won the game", move=False, align="center",
font=("Times Roman New", 25, "normal"))
break
def main():
# Setting the window
sc = Screen()
sc.bgcolor("green") # Setting the Background to Green
penup()
goto(-250, 250)
# Object declaration of the class Game
ob = Game()
# Function Calls for the Race
ob.path()
ob.positionTurtle()
ob.countdown()
ob.move()
done()
main()