0% found this document useful (0 votes)
22 views4 pages

Turtle Race Documentation

The Turtle Race is a Python script that allows players to bet on turtles racing in a graphical interface using the Turtle Graphics module. Players can select a turtle by color, place bets, and watch the race, with the game continuing until they run out of money or choose to quit. The script includes components for turtle initialization, graphical setup, betting logic, race execution, and endgame notifications.

Uploaded by

Aayush Pokhrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

Turtle Race Documentation

The Turtle Race is a Python script that allows players to bet on turtles racing in a graphical interface using the Turtle Graphics module. Players can select a turtle by color, place bets, and watch the race, with the game continuing until they run out of money or choose to quit. The script includes components for turtle initialization, graphical setup, betting logic, race execution, and endgame notifications.

Uploaded by

Aayush Pokhrel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Turtle Race - Python Script 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.

2. Dependencies and Requirements


The following Python modules are required to run this script:
- `turtle`: For creating the graphical user interface and managing the turtle objects.
- `tkinter`: For displaying message boxes and getting user input.
- `random`: For generating random movements for the turtles.

Ensure Python 3 is installed along with the required modules.

3. Instructions to Run the Code


1. Save the script as `Turtle_Race.py`.
2. Run the script in a Python environment that supports GUI, such as IDLE.
3. Follow the on-screen prompts to place your bets and watch the race.
4. The game continues until you run out of money or choose to quit.

4. Key Components
The script consists of the following major components:

- **Turtle Initialization**: Creates turtle objects and assigns them colors.


- **Graphical Setup**: Configures the screen and draws the race track.
- **Betting System**: Takes user input for selecting a turtle and placing bets.
- **Race Logic**: Randomly moves the turtles forward and determines the winner.
- **Game Loop**: Allows players to continue or quit based on their balance.

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.

6. Full Python Code


Below is the complete Python code for the Turtle Race game.

from turtle import Turtle, Screen, done


from tkinter import messagebox
from random import random, sample

"""
--> Turtle Module is used here for creating a simple GUI Application
--> The choice function here chooses a random item from a List

"""
money = 100

colors = ["red", "orange", "blue", "green", "pink", "purple"]


objects = []

for color in colors :


obj = Turtle(shape="turtle") # Creates Turtle Object
obj.color(color)
obj.color = color
objects.append(obj) # Changes the color of said object and adds
to list

#objects = sample(objects, len(objects))


screen = Screen() # Screen object is the literal
screen.setup(width = 430, height = 430)

while True:

y = -100
for obj in objects:
obj.penup()
obj.goto(-160, y)
y += 50

# Draws Finish Line


board_maker = Turtle()
board_maker.hideturtle()
board_maker.penup()
board_maker.goto(x=170, y=170)
board_maker.pendown()
board_maker.goto(x=170, y=-160)

board_maker.goto(x=-170, y=-160)
board_maker.write(f"Money : {money}" , font=('Arial', 12,
'normal'))

# Takes User Opinion via. Dialog Box

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

messagebox.showinfo("Balance", f"You Have {money} Rupees


Left")

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

cont = screen.textinput(title="Proceed. ", prompt="Would You


Like To Continue ? ").lower().strip()
if "no" or "quit" or "stop" in cont :
break

# Makes Sure Window Remains Open and Does Not Crash

You might also like