-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingenerator.py
79 lines (62 loc) · 2.11 KB
/
pingenerator.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
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*- #
import os, pygame
class Generate:
def __init__(self, screen):
self.inFlight = False
self.vx = 40
self.vy = 40
self.screen = screen
screen_width, screen_height = screen.get_size()
self.pos = (screen_width / 2, screen_height)
self.image = pygame.image.load(os.path.join('img','pin.png')).convert_alpha()
self.size = self.image.get_size()
def throwAt(self, target):
self.inFlight = True
x, y = target
self.target = (x, y - self.size[1])
def isStuck(self):
if self.pos == self.target:
# print('Pin stuck!')
return True
def blitme(self):
self.screen.blit(self.image, self.pos)
def moveTowardsTarget(self):
##############################
#
# Late-Night-Comments:
#
# OK, this works, but honestly,
# it would probably be much more effective
# to just be more lenient on isStuck
# e.g. accept approximate positions.
###############################
x, y = self.pos
tx, ty = self.target
xToCover = tx - x;
yToCover = ty - y;
# Please someone turn this into python ;)
if abs(xToCover) < self.vx:
self.vx = self.vx/2
if abs(yToCover) < self.vy:
self.vy = self.vy/2
if xToCover < 0:
xstep = -1 * self.vx
elif xToCover == 0:
xstep = 0
else:
xstep = 1* self.vx
newx = x + xstep
if yToCover < 0:
ystep = -1 * self.vy # = d * v (direction * speed)
elif yToCover == 0:
ystep = 0
else:
ystep = 1 * self.vy
newy = y + ystep
# print('DEBUG :: pin target: ' + str(tx) + ',' + str(ty))
# print('DEBUG :: pin position: ' + str(newx) + ',' + str(newy))
# print('DEBUG :: pin distance: ' + str(xToCover) + ',' + str(yToCover))
# print('DEBUG :: pin speed: ' + str(self.vx) + ',' + str(self.vy))
# print('')
self.pos = (newx, newy)