forked from AseanK/python-tools-and-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle_racing_bet.py
131 lines (107 loc) · 2.89 KB
/
turtle_racing_bet.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
import turtle
import random
from time import sleep
import os
from sys import platform
# Clears command line
def clear():
if platform == "linux" or platform == "linux2":
# linux
os.system("clear")
elif platform == "darwin":
# OS X
os.system("clear")
elif platform == "win32":
# Windows...
os.system("CLS")
TURTLES = ["red", "orange", "yellow", "green", "blue", "purple", "black"]
# Intro
def intro():
clear()
print("Welcome to Turtle raing bet!")
print("""\nRules are simple
Enter a color of the turtle
Place the amount you want to bet
Watch your turtle win! or lose
""")
inp = input("Press enter to start, 'q' to quit")
if inp == 'q':
exit()
# Get user's betting choice
def get_turtle():
while True:
inp = input("""
Choose a trutle you want to bet:
Red Orange Yellow Green Blue Purple Black\n""").lower()
if inp in TURTLES:
break
else:
print("Please enter from the choice")
return inp
# Get user's bet
def get_bet():
while True:
inp = input("Enter a amount you want to bet\n$")
if inp.isdigit():
inp = int(inp)
if inp > 0:
break
else:
print("You have to bet more than $0")
else:
print(f"{inp} is not a number!")
return inp
# Count down before the game starts
def count_down():
for i in range(5,0, -1):
print(f"Race starts in ...{i}", end="\r")
sleep(.7)
# Turtle racing main, returns the winner color
def main():
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.bgcolor("gray")
all_turtles = []
position = -270
for each_turtle in TURTLES:
new_turtle = turtle.Turtle()
new_turtle.shape("turtle")
new_turtle.color(each_turtle)
new_turtle.pu()
new_turtle.goto(-270, position)
position += 90
all_turtles.append(new_turtle)
while True:
for t in all_turtles:
if t.xcor() > 260:
winner = t.pencolor()
screen.bye()
return winner
speed = random.randint(0, 10)
t.fd(speed)
# Count down before the winner reveal
def print_winner():
dots = ".", "..", "..."
for dot in dots:
print(f"The winner is {dot}", end="\r")
sleep(1)
# Play game, display if the user guessed it right
def start():
user_guess = get_turtle()
user_bet = get_bet()
count_down()
clear()
print("GOOD LUCK!")
winner = main()
clear()
print_winner()
print(f"The winner is {winner.upper()}!!!")
if winner == user_guess:
print(f"\nCONGRATS!! You Won ${user_bet * 3}!")
else:
print(f"\nYou lost ${user_bet} 😓")
print("\nBetter luck next time!")
exit()
intro()
clear()
start()