-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturtleGame.py
More file actions
145 lines (133 loc) · 3.22 KB
/
turtleGame.py
File metadata and controls
145 lines (133 loc) · 3.22 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
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
143
144
145
#Importing Libraries
import turtle
import random
#Input the name of players
p1 = input("Enter the name of Player 1: ")
p2 = input("Enter the name of Player 2: ")
#Setting up the screen and Race track
t = turtle.Turtle()
s = turtle.Screen()
t.hideturtle()
s.title("Turtle Race Game")
s.setup(1.0, 1.0)
t.speed(0)
t.home()
t.right(90)
t.forward(200)
t.left(90)
t.forward(150)
t.forward(-300)
t.penup()
t.home()
t.pendown()
t.left(90)
t.forward(200)
t.penup()
x, y = -150, 250
t.goto(x, y)
t.pendown()
t.right(90)
for i in range(12):
if i%2==0:
t.begin_fill()
t.fillcolor("black")
for i in range(4):
t.forward(25)
t.right(90)
t.end_fill()
x+=25
t.penup()
t.goto(x,y)
t.pendown()
else:
t.begin_fill()
t.fillcolor("white")
for i in range(4):
t.forward(25)
t.right(90)
t.end_fill()
x+=25
t.penup()
t.goto(x,y)
t.pendown()
t.penup()
x, y = -150, 225
t.goto(x, y)
t.pendown()
for i in range(12):
if i%2==0:
t.begin_fill()
t.fillcolor("white")
for i in range(4):
t.forward(25)
t.right(90)
t.end_fill()
x+=25
t.penup()
t.goto(x,y)
t.pendown()
else:
t.begin_fill()
t.fillcolor("black")
for i in range(4):
t.forward(25)
t.right(90)
t.end_fill()
x+=25
t.penup()
t.goto(x,y)
t.pendown()
t.penup()
t.goto(0, -225)
t.write("Start Line", font=15, align="center")
t.penup()
t.goto(0, 255)
t.write("Finish Line", font=15, align="center")
#Displaying Player Info
t.penup()
t.goto(-600, 350)
t.write(f"Player 1: {p1}", font=["Arial", 25, "bold"], align="left")
t.penup()
t.goto(600, 350)
t.write(f"Player 2: {p2}", font=["Arial", 25, "bold"], align="right")
#Plotting two racing turtles
t1 = turtle.Turtle()
t1.shape("turtle")
t1.color("blue")
t1.penup()
t1.goto(-75, -200)
t1.left(90)
t2 = turtle.Turtle()
t2.shape("turtle")
t2.color("red")
t2.penup()
t2.goto(75, -200)
t2.left(90)
#Defining motion of the turtles
t1Count, t2Count = 0, 0
while True:
t1Step = random.randint(0,2)
t1Count += t1Step
t2Step = random.randint(0,2)
t2Count += t2Step
t1.forward(t1Step)
t2.forward(t2Step)
if t1Count>t2Count:
s.colormode(255)
s.bgcolor(0,255,255)
else:
s.colormode(255)
s.bgcolor(250, 150, 175)
if(t1Count>=390 or t2Count>=390):
break
#displaying the result
winner = p1 if t1Count>t2Count else p2
t.penup()
t.home()
t.goto(0, -325)
t.color((50, 0, 180))
t.write(f"Winner is {winner} !!", font=["Funnel Display", 35, "normal"], align="center")
t.goto(0, -350)
t.write(f"Won by {abs(t1Count-t2Count)} steps", font=["Funnel Display", 15, "normal"], align="center")
#Restricting the closing of the game window
turtle.done()