-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shoot.py
46 lines (39 loc) · 1.02 KB
/
Shoot.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
import turtle
import random
# Set up the screen
win = turtle.Screen()
win.title("Turtle Shooting Game")
win.bgcolor("lightblue")
# Create the shooter turtle
shooter = turtle.Turtle()
shooter.shape("triangle")
shooter.color("black")
shooter.penup()
shooter.goto(0, -250)
shooter.setheading(90)
# Create the target
target = turtle.Turtle()
target.shape("circle")
target.color("red")
target.penup()
target.goto(0, 250)
# Function to fire the bullet
def fire_bullet():
bullet = turtle.Turtle()
bullet.shape("circle")
bullet.color("yellow")
bullet.penup()
bullet.goto(shooter.xcor(), shooter.ycor())
bullet.setheading(90)
bullet.speed(1)
while bullet.ycor() < 300:
bullet.forward(10)
if bullet.distance(target) < 20:
print("Hit!")
target.goto(random.randint(-200, 200), random.randint(0, 250))
bullet.hideturtle()
break
# Keyboard binding
win.listen()
win.onkey(fire_bullet, "space")
win.mainloop()