importrandomimportturtle# function to check whether turtle# is in Screen or notdefisInScreen(win,turt):# getting the end points of turtle screenleftBound=-win.window_width()/2rightBound=win.window_width()/2topBound=win.window_height()/2bottomBound=-win.window_height()/2# getting the current position of the turtleturtleX=turt.xcor()turtleY=turt.ycor()# variable to store whether in screen or notstillIn=True# condition to check whether in screen or notifturtleX>rightBoundorturtleX<leftBound:stillIn=FalseifturtleY>topBoundorturtleY<bottomBound:stillIn=False# returning the resultreturnstillIn# function to check whether both turtle have# different position or notdefsameposition(Red,Blue):ifRed.pos()==Blue.pos():returnFalseelse:returnTrue# main functiondefmain():# screen initialization for turtlewn=turtle.Screen()# Turtle Red initialization# instantiate a new turtle object# called 'Red'Red=turtle.Turtle()# set pencolor as redRed.pencolor("red")# set pensize as 5Red.pensize(5)# set turtleshape as turtleRed.shape('turtle')pos=Red.pos()# Turtle Blue initialization# instantiate a new turtle object# called 'Blue'Blue=turtle.Turtle()# set pencolor as blueBlue.pencolor("blue")# set pensize as 5Blue.pensize(5)# set turtleshape as turtleBlue.shape('turtle')# make the turtle invisibleBlue.hideturtle()# don't draw when turtle movesBlue.penup()# move the turtle to a location 50# units away from RedBlue.goto(pos[0]+50,pos[1])# make the turtle visibleBlue.showturtle()# draw when the turtle movesBlue.pendown()# variable to store whether turtles# are in screen or notmT=TruejT=True# loop for the gamewhilemTandjTandsameposition(Red,Blue):# coin flip for RedcoinRed=random.randrange(0,2)# angle for Red# random.randrange(0, 180)angleRed=90# condition for left or right# based on coinifcoinRed==0:Red.left(angleRed)else:Red.right(angleRed)# coin flip for BluecoinBlue=random.randrange(0,2)# angle for Blue# random.randrange(0, 180)angleBlue=90# condition for left or right based# on coinifcoinBlue==0:Blue.left(angleBlue)else:Blue.right(angleBlue)# draw for RedRed.forward(50)# draw for BlueBlue.forward(50)# checking whether turtles are in the# screen or notmT=isInScreen(wn,Blue)jT=isInScreen(wn,Red)# set pencolor for Blue and Red as blackRed.pencolor("black")Blue.pencolor("black")# condition check for draw or winifjT==TrueandmT==False:# writing resultsRed.write("Red Won",True,align="center",font=("arial",15,"bold"))elifmT==TrueandjT==False:# writing resultsBlue.write("Blue Won",True,align="center",font=("arial",15,"bold"))else:# writing resultsRed.write("Draw",True,align="center",font=("arial",15,"bold"))Blue.write("Draw",True,align="center",font=("arial",15,"bold"))# exit on closewn.exitonclick()# Calling main functionmain()