Back

Pelican crossing answers - 5

Here is the final program for the Pelican crossing model. You can copy and paste this straight into Python and run it.

import turtle
import time
import winsound

wn = turtle.Screen()
wn.bgcolor("white")

Fred = turtle.Turtle()

def light(diameter, colour, xpos, ypos):
    Fred.penup()
    Fred.setpos(xpos, ypos)
    Fred.pendown()
    Fred.dot(diameter, colour)

light(100,"black",0,0) # red light
light(100,"black",0,-100) # yellow light
light(100,"green",0,-200) # green light

light(30,"lightblue",150,-175) # wait light
light(30,"red",150,-205) # red man
light(30,"lightblue",150,-235) # green man
Fred.ht()

#This models when you press the WAIT button
input("Press <ENTER> to cross the road!") # Press ENTER to cross
light(30,"purple",150,-175)#This colour represents the WAIT light on

#This models the cars stopping and the lights changing
time.sleep(3) # wait 3s before stopping cars
light(100,"yellow",0,-100) # yellow light on
light(100,"black",0,-200) # green light off
time.sleep(2) # wait 2s

#This models you crossing the road (no flashing lights or buzzers)
light(100,"red",0,0) # red light on
light(100,"black",0,-100) # yellow light off
light(30,"lightblue",150,-175) # wait light off
light(30,"lightblue",150,-205) # red man off

#add a buzzer and make the green man flash
for seconds in range(10): # do the following code 10 times
    light(30,"green",150,-235) # green man on
    winsound.Beep(600,250) #beep for a quarter of a second
    time.sleep(0.25) #wait for a quarter of a second
    light(30,"black",150,-235) # green man off
    time.sleep(0.25) #wait for a quarter of a second

# Stop people crossing.
light(100,"black",0,0) # red light off
for counter in range(10):
    light(100,"yellow",0,-100) # yellow light on
    light(30,"green",150,-235) # green man on
    time.sleep(0.25) #wait for a quarter of a second
    light(100,"black",0,-100) # yellow light off
    light(30,"black",150,-235) # green man off
    time.sleep(0.25) #wait for a quarter of a second

#Cars carry on normally again
light(100,"black",0,-100) # yellow light on
light(100,"green",0,-200) # green light on
light(30,"red",150,-205) # red man on
light(30,"lightblue",150,-235) # green man off

Here is a picture o the program:

pelican6

Back