-
Notifications
You must be signed in to change notification settings - Fork 0
/
DodgeGame.py
67 lines (54 loc) · 1.32 KB
/
DodgeGame.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
60
61
62
63
64
65
66
67
import turtle
import random
win = turtle.Screen()
win.title("Turtle Dodge Game")
win.bgcolor("lightgray")
win.setup(width=600, height=600)
# Create the player turtle
player = turtle.Turtle()
player.shape("turtle")
player.color("blue")
player.penup()
player.goto(0, -250)
# Create the falling obstacle
obstacle = turtle.Turtle()
obstacle.shape("circle")
obstacle.color("red")
obstacle.penup()
obstacle.speed(0)
obstacle.goto(random.randint(-280, 280), 250)
score = 0
# Function to move the player left and right
def move_left():
x = player.xcor()
x -= 20
if x < -280:
x = -280
player.setx(x)
def move_right():
x = player.xcor()
x += 20
if x > 280:
x = 280
player.setx(x)
# Function to drop the obstacle
def drop_obstacle():
global score
y = obstacle.ycor()
y -= 20
obstacle.sety(y)
if obstacle.ycor() < -300:
obstacle.goto(random.randint(-280, 280), 250)
score += 1
print(f"Score: {score}")
if player.distance(obstacle) < 20:
print(f"Game Over! Final Score: {score}")
win.bye()
win.ontimer(drop_obstacle, 100)
# Keyboard bindings
win.listen()
win.onkey(move_left, "Left")
win.onkey(move_right, "Right")
# Start dropping obstacles
drop_obstacle()
win.mainloop()