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

Popular posts from this blog

Why Do Glass Windows Shatter During Fire?

  You might have noticed in movies and also in real life that whenever there is a fire inside a building, the glass windows crack and break. The openings of the windows in the walls of the room on fire play a key role in the spread of fire. Windows are those parts of the buildings that generally have less resistance to fire in comparison to that of the walls. During a fire,  there is a increase in temperature inside the room. The glass pane is not subjected to uniform heat from the fire. Glass windows are fitted within the frames, and the thickness of the frame is larger in comparison to that of the glass pane. There is an intense heat flow that is suddenly experienced on one side of a glass pane. As a result, the heat focussed on the central region of the glass causes it to expand. While the edges of the pane happen to be relatively cooler,  which induces stress in the material. This shifting gradient is known to shatter the glass to pieces.

Preface of Planes

    The Plane is a book of planes that were not known. The book starts with stories of planes like: the fuel got over, the plane stole all the money, the plane landed after 34 years and more. This book is fiction book. Some stories are real. Enjoy it and read it.

Python Turtle Graphics Code 1

 Input: Output: