Back

The levels program - Answers

Here is the final code. You can easily copy and paste this straight into Python's Idle and run it from there.

import turtle, sys

#Initialise the variables
size = 0
colour= ""
speed= 0

#Draw the window you will play the game in
window = turtle.Screen()
window.bgcolor("lightyellow")
window.setup(800, 800)
#Create a turtle object called Fred
Fred = turtle.Turtle()

#Draw a dot and move it across the screen
def alien(size, colour, speed):
    Fred.ht()
    Fred.up()
    Fredx = -370
    Fredy = 0
    Fred.setpos(Fredx, Fredy)

    while Fred.xcor()< 360:
        window.delay(speed)
        Fredx = Fredx + 10
        Fred.setpos(Fredx, Fredy)
        Fred.dot(size, colour)

#Display a menu. Player selects option.
level = input("""
Please select a level:

1 = Easy
2 = Medium
3 = Hard

""")
print("You have selected level",level)

#Load up values depending on level selected.
if int(level) == 1:
    size = 100
    colour = "blue"
    speed = 25
if int(level) == 2:
    size = 30
    colour = "red"
    speed = 10
if int(level) == 3:
    size = 10
    colour = "black"
    speed = 1

alien(size, colour, speed)

Back