Skip to main content

Python Turtle Graphics Code 11

 Input:



#import required libraries

import turtle

import random

import time


#setup the screen

screen=turtle.Screen()

screen.bgcolor("red")

screen.setup(800, 600)


#define and intialise all required variables to control the game

mike_steps = 0

tom_steps = 0


FINISH_LINE = 360

chance = 0

step_set = [10, 20, 30, 40, 50, 60]

steps=0

countdown=10

bet="TOM"


#set assistant turtle to draw finish line

mypen = turtle.Turtle()

mypen.hideturtle()

mypen.pencolor("yellow")


#set assistant counter_turtle

counter_turtle = turtle.Turtle()

counter_turtle.hideturtle()

counter_turtle.pencolor("white")

counter_turtle.penup()

counter_turtle.setpos(-150,50)

counter_turtle.pendown()


#display information about the game

about_game = "Tom and Mike are the two turtles\nto race to the finish line.\nLet us see who wins!\nBy: Tejasvi Vashishtha"

counter_turtle.write(about_game,font=("arial", 20, "bold"))

counter_turtle.penup()

time.sleep(5.0)


#draw the finish line

mypen.penup()

mypen.setpos(-250,200)

mypen.pendown()

mypen.width(25)

mypen.forward(500)

mypen.penup()

mypen.setpos(-150,-100)

counter_turtle.setpos(-50,-100)

mypen.pencolor("white")


#set up the player turtles Tom and Mike

players = ["tom","mike"]

tom = turtle.Turtle()

mike = turtle.Turtle()

tom.fillcolor("red")

mike.fillcolor("orange")

tom.pencolor("blue")

mike.pencolor("green")


tom.shape("turtle")

mike.shape("turtle")


tom.shapesize(2)

mike.shapesize(2)


tom.penup()

mike.penup()

tom.speed(1)

mike.speed(1)

tom.left(90)

mike.left(90)

tom.setpos(-200,-200)

mike.setpos(200,-200)


tom.pendown()

mike.pendown()

tom.width(2)

mike.width(2)


tom.write("TOM",font=("arial", 15,"bold"))

mike.write("MIKE",font=("arial", 15,"bold"))


#set up the loop to accept correct name to bet on from the user

while bet != "TOM" or bet != "MIKE":

    bet = turtle.textinput("BET IS ON!","Type the name Tom or Mike on whom you wanna bet.")


    if bet==None:

        screen.bye()

    bet = bet.upper()

    if bet=="TOM":

        tom.write("Thank you!",font=("arial", 16,"bold"))

        break

    elif bet=="MIKE":

        mike.write("Thank you!",font=("arial", 16,"bold"))

        break


#begin race countdown

counter_turtle.pendown()

for count in range(0,10):

    counter_turtle.write(countdown,font=("impact", 50,"bold"))

    countdown = countdown - 1

    time.sleep(1.0)

    counter_turtle.clear()


#begin the race and declare the winner

for x in range(1,200):

    r = random.randint(0,5)

    steps = step_set[r]


    if chance == 0:

        tom.forward(steps)

        tom_steps = tom_steps + steps

        tom.write(str(tom_steps))

        chance = 1

    else:

        mike.forward(steps)

        mike_steps = mike_steps + steps

        mike.write(str(mike_steps))

        chance=0


    if tom_steps>=FINISH_LINE:

        mypen.write("YOU WIN!",font=("impact", 50,"bold"))

        break

    if mike_steps>=FINISH_LINE:

        mypen.write("YOU WIN!",font=("impact", 50,"bold"))

        break




Output:



Comments

Post a Comment