# The Tortoise and the Hare Simulation with Python Code

Everyone knows this historical story!!! The tortoise and the Hare had a race. Sheer determination and dedication made the hare win the race. We, in this post, are going to create that story. No exactly ;). It is just a simulation of the famous race but it doesn’t end with tortoise’s win. While inching towards the winning line, both the contestants face various hurdle and distractions which are used to define their journey.

First let’s understand the presumptions about speed accelerators and decelerators for these two.

The hare may

• Hop at full speed
• Hop at a slow speed
• Slip a bit from bumps on the way
• Slip to a great extent due to hillocks
• Sleep to rest

The tortoise never sleeps on the way. He may

• Plod at full speed
• Slip
• Plod at slow speed

With these premises about our contenders, class race is created with two methods to represent their movement- moveRabbit and  moveTortoise. The above mentioned motion specifiers are used as conditions to define the distance covered (forward or backward) by each of them. The movement of each contender is selected on the basis of a random number generated between 1 and 10. This generated number is checked against certain values and a value is returned.

## The tortoise and the Hare- Race Path

The interface is created with the GraphWin object from graphics.py.  gif images of rabbit and tortoise are used to display their motion. For this Image method from graphics.py is used. It accepts first argument as a point and second argument as the name of the image file (.gif) to be displayed.

The Y coordinates of the tortoise and the hare are not changed. With every call to the methods of race class (moveRabbit and  moveTortoise ) the X coordinates of the contestants are updated and moved to the new position. undraw() method is used to clear a contestant at its last position before moving.

Simulation starts when a viewer clicks in the window. Simulation ends when either of the participants crosses the finish line (depicted by word Winner). The winner is declared and its prize (carrot or bag of insects) is displayed.

Program Code

```import random
import time
from graphics import *

class race:
def __init__(self,turtle,rabbit):
self.turtle=turtle
self.rabbit=rabbit

def  moveTurtle(self):
r=random.randrange(9)+1
turtleMoves=[3,6]
if (r>=1 and r<=5):
self.turtle=turtleMoves[0]
elif (r==6 or r==7):
self.turtle=-turtleMoves[1]
else:
self.turtle=1
return self.turtle

def  moveRabbit(self):
r=random.randrange(9)+1
rabbitMoves=[9,12,2]
if (r==3 or r==4):
self.rabbit=rabbitMoves[0]
elif (r==5):
self.rabbit=-rabbitMoves[1]
elif (r>=6 and r<=8):
self.rabbit=rabbitMoves[2]
elif (r>8):
self.rabbit=-2

return self.rabbit

def main():
timer=0
markLn=0
#create race path
workArea = GraphWin('Simulation of Turtle and Rabbit Race', 800, 200) # give title and dimensions
workArea.setBackground('white')
stx=50
sty=20
p1=Image(Point(20,100),'start.gif')
p1.draw(workArea)
p1=Image(Point(750,100),'winner.gif')
p1.draw(workArea)
ln=Line(Point(stx,sty),Point(stx,sty+160))
ln.draw(workArea)
ln=Line(Point(700,sty),Point(700,sty+160))
ln.draw(workArea)

#set players at start line
x1=30
x2=30
rPos=Point(x1,40)
p1=Image(rPos,'rabbit.gif')
p1.draw(workArea)
tPos=Point(x2,140)
p2=Image(tPos,'tortoise.gif')
p2.draw(workArea)
#Ask viewer to start race by clicking
strt=Text(Point(workArea.width/2,workArea.height/2),"Get Set Go!!! click to start race")
strt.setSize(24)
strt.setFace('times roman')
strt.setTextColor('green')
strt.draw(workArea)
workArea.getMouse()
strt.undraw()
race1=race(1,1)
while (x1<=720 and x2<=720):
x1=x1+race1.moveRabbit()*5
if x1<1:
x1=30
rPos=Point(x1,40)
p1.undraw()
p1=Image(rPos,'rabbit.gif')
p1.draw(workArea)
x2=x2+race1.moveTurtle()*5
if x2<1:
x2=30
tPos=Point(x2,140)
p2.undraw()
p2=Image(tPos,'tortoise.gif')
p2.draw(workArea)
timer+=1
time.sleep(1)

if (x1>=x2):
msg=Text(Point(workArea.width/2,workArea.height/2),"Wonderful! Rabbit gets a")
winner='rabbit.gif'
prize='carrot.gif'
else:
msg=Text(Point(workArea.width/2,workArea.height/2),"Wow!!! Turtle gets")
winner='tortoise.gif'
prize='sack.gif'
#diaplay winner
p1.undraw()
p2.undraw()
msg.setSize(24)
msg.setFace('times roman')
msg.setTextColor('orange')
msg.draw(workArea)
p2=Image(Point(workArea.width/2-50,workArea.height/2-50),winner)
p2.draw(workArea)
p2=Image(Point(workArea.width/2+100,workArea.height/2+50),prize)
p2.draw(workArea)

main()

```

## 2 Comments

1. Pascal Fröhlich said:

Is this code old or does it just not run in Pycharm?
I ended up copying it straight into Pycharm and it gave me 5 error messages in (lines: 111, 45, 877, 4061, 4006)

February 2, 2021
Reply
• admin said:

Hello Pascal. This code is OK. Save the code as .py file and run from python command window. Make sure that you have the images with same file names as used in the code.

February 2, 2021
Reply