forked from hhhrrrttt222111/PyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCannon-Fire.txt
More file actions
84 lines (65 loc) · 1.58 KB
/
Cannon-Fire.txt
File metadata and controls
84 lines (65 loc) · 1.58 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
# This game is from the official documentation of freegames
# https://pypi.org/project/freegames/
# pip install freegames
# Tap on Screen to Fire
# import modules
from random import randrange
import turtle as t
from freegames import vector
# Set window title, color and icon
t.title("Canon Fire")
root = t.Screen()._root
root.iconbitmap("logo-ico.ico")
t.bgcolor('#99ffbb')
ball = vector(-200, -200)
speed = vector(0, 0)
targets = []
# Functions
# Respond to screen tap
def tap(x, y):
if not inside(ball):
ball.x = -199
ball.y = -199
speed.x = (x + 200) / 25
speed.y = (y + 200) / 25
# Return True if xy within screen
def inside(xy):
return -200 < xy.x < 200 and -200 < xy.y < 200
# Draw ball and targets
def draw():
t.clear()
for target in targets:
t.goto(target.x, target.y)
t.dot(20, '#8000ff')
if inside(ball):
t.goto(ball.x, ball.y)
t.dot(6, '#cc0000')
t.update()
# Move ball and targets
def move():
if randrange(40) == 0:
y = randrange(-150, 150)
target = vector(200, y)
targets.append(target)
for target in targets:
target.x -= 0.5
if inside(ball):
speed.y -= 0.35
ball.move(speed)
dupe = targets.copy()
targets.clear()
for target in dupe:
if abs(target - ball) > 13:
targets.append(target)
draw()
for target in targets:
if not inside(target):
return
t.ontimer(move, 50)
t.setup(420, 420, 370, 0)
t.hideturtle()
t.up()
t.tracer(False)
t.onscreenclick(tap)
move()
t.done()