Turtle Race Documentation
Turtle Race Documentation
1. Overview
This Python script implements a simple graphical Turtle Race game using the Turtle
Graphics module. Players place bets on a turtle by choosing a color and wagering an
amount. The turtles race on the screen, and the player either wins or loses their bet based
on the outcome of the race. The game continues until the player decides to quit or runs out
of money.
4. Key Components
The script consists of the following major components:
5. Code Details
The script includes the following main elements:
1. **Initialization**:
- Colors are predefined and assigned to turtle objects.
- A list of turtle objects is created and stored in `objects`.
2. **Screen Setup**:
- The screen is configured with a fixed size.
- The race track is drawn with a finish line at x=170.
3. **Betting Logic**:
- Players select a turtle by color and place a bet using dialog boxes.
- Validation ensures valid input and sufficient funds.
4. **Race Execution**:
- Turtles move forward by a random distance.
- The race ends when a turtle crosses the finish line.
5. **Endgame**:
- Players are notified of the result.
- The game ends when the player chooses to quit or runs out of money.
"""
--> Turtle Module is used here for creating a simple GUI Application
--> The choice function here chooses a random item from a List
"""
money = 100
while True:
y = -100
for obj in objects:
obj.penup()
obj.goto(-160, y)
y += 50
board_maker.goto(x=-170, y=-160)
board_maker.write(f"Money : {money}" , font=('Arial', 12,
'normal'))
while True:
chosen_turtle = screen.textinput(title="Place Your
Bets !", prompt="Which Turtle Will Win The Race ?").lower().strip()
if chosen_turtle not in colors :
messagebox.showinfo("Invalid Bet", "Enter
Valid Color Name")
else:
break
while True:
bet = int(screen.textinput(title="BET", prompt="How
much do you Bet ?"))
if bet <= 0 :
messagebox.showinfo("Invalid Amount", "Bet
Can't Be Negative Or Zero. ")
if bet > money :
messagebox.showinfo("Invalid Amount", "Can't
Bet More Than What You Have")
else:
break
race_on= True
while race_on:
for obj in objects:
obj.distance = (random()*3)+1 # Makes Each
Turtle Move By A Random Length
if not obj.pos()[0] >= 158 : # Checks
Whether It has Crossed Finish Line
obj.forward(obj.distance)
elif obj.pos()[0] >= 158 :
obj_index = objects.index(obj)
race_on = False
if objects[obj_index].color in chosen_turtle.lower().strip()
:
text = f"Congrats! The
{objects[obj_index].color.title()} Turtle Won!!!"
messagebox.showinfo("HOORAY !!!", text)
money += bet
else:
text = f"Unfortunately, The Turtle You Chose Did Not
Win.\nThe {objects[obj_index].color.title()} Turtle Won."
messagebox.showinfo("OH NO!!!", text)
money -= bet
if money == 0 :
messagebox.showinfo("GAME OVER!!!", "You Have No
Money Left. Hence, You Have Been Kicked Out of The Game")
messagebox.showinfo("L", "Moral Of The Story : Do Not
Gamble /≧▽≦/. ")
break