/
338.16.py
70 lines (53 loc) · 1.66 KB
/
338.16.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
68
69
70
# Problem 16
from projectile import Projectile
from tracker import Tracker
from graphics import *
from time import sleep
sleep(0.005)
from target import Target
def main ():
#Introduction
print ("This program uses a cannonball to shoot at a target.")
#Create graphics window
win = GraphWin(200, 200)
win.setCoords(0.0, 0.0, 25.0, 25.0)
target = Target(win)
flag = False
targetHit = False
while not flag:
#Get inputs
print ()
a = eval(input("Enter the launch angle in degrees: "))
v = eval(input("Enter the initial velocity in meters per second: "))
h = eval(input("Enter the initial height in meters: "))
#Create tracker
projectile = Projectile(a, v, h)
tracker = Tracker(win, projectile)
time = 0.0
while projectile.getY() >= -5:
time += .0005
projectile.update(time)
tracker.update()
#Calculate if cannonball hit target
points = target.points()
center = tracker.circ.getCenter()
center_x = center.getX()
center_y = center.getY()
radius = tracker.circ.getRadius()
for point in points:
x = point.getX()
y = point.getY()
square_dist = (center_x-x) ** 2 + (center_y-y) ** 2
if square_dist <= radius ** 2:
targetHit = True
if targetHit:
print ("\nYou hit the target!")
flag = True
else:
flag = False
print ("\nTry again!")
#Close window
win.getMouse()
win.close()
if __name__ == '__main__':
main ()