-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraw.py
53 lines (42 loc) · 1.09 KB
/
Draw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import turtle
import random
# Set up the screen
win = turtle.Screen()
win.title("Turtle Drawing Game")
win.bgcolor("white")
# Create a turtle
draw_turtle = turtle.Turtle()
draw_turtle.shape("turtle")
draw_turtle.color("black")
draw_turtle.speed(1)
# Functions to control the turtle
def move_up():
draw_turtle.setheading(90)
draw_turtle.forward(50)
def move_down():
draw_turtle.setheading(270)
draw_turtle.forward(50)
def move_left():
draw_turtle.setheading(180)
draw_turtle.forward(50)
def move_right():
draw_turtle.setheading(0)
draw_turtle.forward(50)
def change_color():
colors = ["red", "green", "blue", "yellow", "purple"]
draw_turtle.color(random.choice(colors))
def pen_up():
draw_turtle.penup()
def pen_down():
draw_turtle.pendown()
# Keyboard bindings
win.listen()
win.onkey(move_up, "Up")
win.onkey(move_down, "Down")
win.onkey(move_left, "Left")
win.onkey(move_right, "Right")
win.onkey(change_color, "space")
win.onkey(pen_up, "u")
win.onkey(pen_down, "d")
# Keep the window open
win.mainloop()