-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
95 lines (77 loc) · 2.29 KB
/
run.py
File metadata and controls
95 lines (77 loc) · 2.29 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
85
86
87
88
89
90
91
92
93
94
95
import pygame
import math
wScreen = 1200
hScreen = 500
win = pygame.display.set_mode((wScreen, hScreen))
class ball(object):
def __init__(self, x, y, radius, color):
self.x = x
self.y = y
self.radius = radius
self.color = color
def draw(self, win):
pygame.draw.circle(win, (0,0,0), (self.x, self.y), self.radius)
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius-1)
@staticmethod
def ballPatch(startx, starty, power, ang, time):
velx = math.cos(angle) * power
vely = math.sin(angle) * power
distX = velx * time
distY = (vely * time) + ((-4.9 * (time)**2)/2)
newx = round(distX + startx)
newy = round(starty - distY)
return(newx, newy)
def redrawWindow():
win.fill((64,64,64))
golfBall.draw(win)
pygame.draw.line(win, (255,255,255), line[0], line[1])
pygame.display.update()
def findAngle(pos):
sX = golfBall.x
sY = golfBall.y
try:
angle = math.atan((sY - pos[1]) / (sX - pos[0]))
except:
angle = math.pi / 2
if pos[1] < sY and pos[0] > sX:
angle = abs(angle)
elif pos[1] < sY and pos[0] < sX:
angle = math.pi - angle
elif pos[1] > sY and pos[0] < sX:
angle = math.pi + abs(angle)
elif pos[1] > sY and pos[0] > sX:
angle = (math.pi * 2) - angle
return angle
golfBall = ball(300, 494, 5, (255,255,255))
x = 0
y = 0
time = 0
power = 0
angle = 0
shoot = False
run = True
while run:
if shoot:
if golfBall.y < 500 - golfBall.radius:
time += 0.05
po = ball.ballPatch(x,y,power,angle,time)
golfBall.x = po[0]
golfBall.y = po[1]
else:
shoot = False
golfBall.y = 494
pos = pygame.mouse.get_pos()
line = [(golfBall.x, golfBall.y), pos]
redrawWindow()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if shoot == False:
shoot = True
x = golfBall.x
y = golfBall.y
time = 0
power = math.sqrt((line[1][1] - line[0][1])**2 + (line[1][0]-line[0][0])**2)/8
angle = findAngle(pos)
pygame.quit()