Guest User

Untitled

a guest
Jul 2nd, 2014
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import pygame
  2. import math
  3. from itertools import cycle
  4.  
  5. # some simple vector helper functions, stolen from http://stackoverflow.com/a/4114962/142637
  6. def magnitude(v):
  7. return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))
  8.  
  9. def add(u, v):
  10. return [ u[i]+v[i] for i in range(len(u)) ]
  11.  
  12. def sub(u, v):
  13. return [ u[i]-v[i] for i in range(len(u)) ]
  14.  
  15. def dot(u, v):
  16. return sum(u[i]*v[i] for i in range(len(u)))
  17.  
  18. def normalize(v):
  19. vmag = magnitude(v)
  20. return [ v[i]/vmag for i in range(len(v)) ]
  21.  
  22. screen = pygame.display.set_mode((300, 300))
  23. clock = pygame.time.Clock()
  24.  
  25. class Ball(object):
  26. def __init__(self, path):
  27. self.x, self.y = (0, 0)
  28. self.speed = 2.5
  29. self.color = (200, 200, 200)
  30. self.path = cycle(path)
  31. self.set_target(next(self.path))
  32.  
  33. @property
  34. def pos(self):
  35. return self.x, self.y
  36.  
  37. # for drawing, we need the position as tuple of ints
  38. # so lets create a helper property
  39. @property
  40. def int_pos(self):
  41. return map(int, self.pos)
  42.  
  43. @property
  44. def target(self):
  45. return self.t_x, self.t_y
  46.  
  47. @property
  48. def int_target(self):
  49. return map(int, self.target)
  50.  
  51. def next_target(self):
  52. self.set_target(self.pos)
  53. self.set_target(next(self.path))
  54.  
  55. def set_target(self, pos):
  56. self.t_x, self.t_y = pos
  57.  
  58. def update(self):
  59. # if we won't move, don't calculate new vectors
  60. if self.int_pos == self.int_target:
  61. return self.next_target()
  62.  
  63. target_vector = sub(self.target, self.pos)
  64.  
  65. # a threshold to stop moving if the distance is to small.
  66. # it prevents a 'flickering' between two points
  67. if magnitude(target_vector) < 2:
  68. return self.next_target()
  69.  
  70. # apply the balls's speed to the vector
  71. move_vector = [c * self.speed for c in normalize(target_vector)]
  72.  
  73. # update position
  74. self.x, self.y = add(self.pos, move_vector)
  75.  
  76. def draw(self):
  77. pygame.draw.circle(screen, self.color, self.int_pos, 4)
  78.  
  79. pygame.init()
  80. quit = False
  81.  
  82. path = [(26, 43),
  83. (105, 110),
  84. (45, 225),
  85. (145, 295),
  86. (266, 211),
  87. (178, 134),
  88. (250, 56),
  89. (147, 12)]
  90.  
  91. path2 = [(26, 43),
  92. (105, 10),
  93. (45, 125),
  94. (150, 134),
  95. (150, 26),
  96. (107, 12)]
  97.  
  98. ball = Ball(path)
  99. ball.speed = 1.9
  100.  
  101. ball2 = Ball(path2)
  102. ball2.color = (200, 200, 0)
  103.  
  104. balls = [ball, ball2]
  105.  
  106. while not quit:
  107. quit = pygame.event.get(pygame.QUIT)
  108. pygame.event.poll()
  109.  
  110. map(Ball.update, balls)
  111.  
  112. screen.fill((0, 0, 0))
  113.  
  114. map(Ball.draw, balls)
  115.  
  116. pygame.display.flip()
  117. clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment