forked from Sonu2345/HactoberFest23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle.py
59 lines (46 loc) · 1.15 KB
/
turtle.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
54
55
56
57
58
59
import turtle
# Create the game window
wn = turtle.Screen()
wn.title("Turtle Game")
wn.bgcolor("white")
# Create the player turtle
player = turtle.Turtle()
player.shape("turtle")
player.color("blue")
player.speed(0)
player.penup()
player.goto(0, 0)
player.direction = "stop"
# Functions to control the player's movement
def move_up():
player.direction = "up"
def move_down():
player.direction = "down"
def move_left():
player.direction = "left"
def move_right():
player.direction = "right"
# Keyboard bindings
wn.listen()
wn.onkeypress(move_up, "Up")
wn.onkeypress(move_down, "Down")
wn.onkeypress(move_left, "Left")
wn.onkeypress(move_right, "Right")
# Main game loop
while True:
wn.update()
# Move the player
if player.direction == "up":
y = player.ycor()
player.sety(y + 20)
if player.direction == "down":
y = player.ycor()
player.sety(y - 20)
if player.direction == "left":
x = player.xcor()
player.setx(x - 20)
if player.direction == "right":
x = player.xcor()
player.setx(x + 20)
# Close the game window when done (unreachable in this example)
wn.mainloop()