-
Notifications
You must be signed in to change notification settings - Fork 38
/
turtle-race.py
71 lines (64 loc) · 2.07 KB
/
turtle-race.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
from turtle import Turtle, Screen
import random, time
from tkinter import *
from tkinter import messagebox
final_text=''
screen = Screen()
scr_width=800
scr_height=600
screen.setup(width=scr_width, height=scr_height)
turtles = []
color = ['red', 'blue', 'green', 'yellow', 'orange', 'black', 'purple']
interval = 30
y_end = -70
color_text = ', '.join([col.capitalize() for col in color])
correct_color = False
while not correct_color:
bet = screen.textinput('Welcome to Turtle Races ! ', "Which Turtle would you like to bet on?: "+color_text)
if bet.lower() not in color:
bet = screen.textinput('Welcome to Turtle Races ! ',
"Incorrect choice ! Which Turtle would you like to bet on?: "+color_text)
else:
correct_color=True
break
for i in range(len(color)):
turt = Turtle(shape='turtle')
turt.color(color[i])
turt.penup()
y_pos=y_end+interval*i
turt.setpos(-400, y_pos)
turtles.append(turt)
if correct_color:
race_on = True
else:
race_on=False
#print(race_on)
for i in reversed(range(4)):
col=turt.color()[0]
turt.write(str(i+1), font=('Arial, 48'))
time.sleep(1)
turt.color('white')
turt.write(str(i + 1), font=('Arial, 48'))
turt.color(col)
while race_on:
for turt in turtles:
steps = random.randint(0, 10)
turt.forward(steps)
if turt.xcor() > (scr_width/2)-10: #turtle size is 20 so 10 is half that (center of mass)
race_on = False
turt_winner = turt.color()
result_text=f'The winner is the {turt.color()[0]} turtle'
final_text+=f'\n\nYou bet on the {bet} turtle'
final_text+=f'\n\nThe winner was the {turt_winner[0].capitalize()} turtle'
if turt_winner[0] == bet:
title_text='You won!'
final_text+='\n\nYou won the bet !'
else:
title_text='You lost!'
final_text+='\n\nYou lost the bet :('
#from tkinter import *
from tkinter import messagebox
# root = Tk()
#messagebox.showinfo('Final Results of the Race', result_text) # Results
messagebox.showinfo(result_text, final_text) # The alert.
#root.mainloop()