-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathballoonpop.py
More file actions
84 lines (78 loc) · 2.37 KB
/
Copy pathballoonpop.py
File metadata and controls
84 lines (78 loc) · 2.37 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import turtle, random, time
balloons = []
balloonspeed = []
colors = ["white", "blue", "black", "pink", "orange", "grey", "green"]
def color() -> str:
hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "b", "C", "D", "E", "F"]
return "#" + random.choice(hex) + random.choice(hex) + random.choice(hex) + random.choice(hex) + random.choice(hex) + random.choice(hex)
def newballoon():
balloon = turtle.Turtle()
balloon.speed(0)
balloon.up()
balloon.shape("circle")
balloon.color(color())
balloon.goto(random.randint(-450, 450), 450)
balloons.append(balloon)
balloonspeed.append(random.randint(2, 7)/2)
def move(dx):
dart.goto(dart.xcor()+dx, -450)
screen = turtle.Screen()
screen.setup(1000, 1000)
screen.bgcolor("lightblue")
maxballoons = 15
dart = turtle.Turtle()
dart.speed(0)
dart.up()
dart.color("red")
dart.shape("triangle")
dart.left(90)
dart.goto(0, -450)
screen.listen()
screen.onkeypress(lambda: move(-15), "a")
screen.onkeypress(lambda: move(15), "d")
stats = {
"hit": 0,
"miss": 0,
"total": 0
}
infot = turtle.Turtle()
infot.up()
infot.hideturtle()
infot.goto(-450, 410)
tps = 0
secstart = time.time()
score = 0
while True:
for i in balloons:
if i.ycor() < -440:
if dart.distance(i) < 20:
score += 2
print("Balloon hit! (+2)")
stats["hit"] += 1
else:
score -= 1
print("Missed balloon. (-1)")
stats["miss"] += 1
print(f"Score: {score}")
stats["total"] += 1
i.hideturtle()
balloons.remove(i)
else:
i.goto(i.xcor(), i.ycor() - balloonspeed[balloons.index(i)]*(len(balloons)/1.5))
if len(balloons) == 0:
newballoon()
elif len(balloons) < maxballoons:
chance = random.random()
if chance < 0.02:
newballoon()
if score > 29:
break
if time.time() - secstart > 1:
infot.clear()
infot.write(f"Score: {score}\nTPS: {tps}\nTotal balloons: {len(balloons)}", font=("arial", 16))
tps = 0
secstart = time.time()
else:
tps += 1
print("You win, stats:")
print(f" Hit balloons: {stats["hit"]}\n Missed balloons: {stats["miss"]}\n Total balloons: {stats["total"]}")