2014-08-31 03:39:02 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os, pygame
|
|
|
|
|
|
|
|
class Generate:
|
|
|
|
|
|
|
|
def __init__(self, screen):
|
|
|
|
self.inFlight = False
|
2014-08-31 03:41:08 +02:00
|
|
|
self.vx = 40
|
|
|
|
self.vy = 40
|
2014-08-31 03:39:02 +02:00
|
|
|
|
|
|
|
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()
|
2014-08-31 10:15:28 +02:00
|
|
|
self.size = self.image.get_size()
|
2014-08-31 03:39:02 +02:00
|
|
|
|
|
|
|
def throwAt(self, target):
|
|
|
|
self.inFlight = True
|
2014-08-31 10:15:28 +02:00
|
|
|
x, y = target
|
|
|
|
self.target = (x, y - self.size[1])
|
2014-08-31 03:39:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2014-08-31 03:44:49 +02:00
|
|
|
##############################
|
|
|
|
#
|
|
|
|
# Late-Night-Comments:
|
|
|
|
#
|
2014-08-31 03:39:02 +02:00
|
|
|
# OK, this works, but honestly,
|
|
|
|
# it would probably be much more effective
|
|
|
|
# to just be more lenient on isStuck
|
|
|
|
# e.g. accept approximate positions.
|
2014-08-31 03:44:49 +02:00
|
|
|
###############################
|
2014-08-31 03:39:02 +02:00
|
|
|
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)
|