/rock.py

https://bitbucket.org/geordiemhall/fishy
Python | 141 lines | 68 code | 57 blank | 16 comment | 2 complexity | 26ace6467116ff959514efa78afcbbf5 MD5 | raw file
  1. '''
  2. Rock
  3. =================================='''
  4. from vector2d import Vector2D
  5. from graphics import egi, rgba
  6. from random import uniform, randrange
  7. from math import sqrt, sin, cos, pi
  8. from matrix33 import Matrix33
  9. import copy
  10. class Rock(object):
  11. """Rock object"""
  12. def __init__(self, world=None):
  13. self.world = world
  14. self.pos = Vector2D()
  15. self.vel = Vector2D()
  16. self.scale = Vector2D() * 30
  17. self.maxSpeed = uniform(5, 8)
  18. self.boundingRadius = uniform(25, 80)
  19. self.edges = randrange(7, 12)
  20. self.rotation = uniform(-0.05/3, 0.05/3)
  21. self.color = rgba('25a000')
  22. self.heading = Vector2D(0, 1)
  23. self.side = self.heading.perp()
  24. self.rockShape = self.getRockShape()
  25. # Rocks have a constant speed
  26. def calculateAcceleration(self, delta):
  27. accel = Vector2D()
  28. return accel
  29. # Calculates velocity based on our acceleration
  30. def calculateVelocity(self, delta):
  31. # new velocity
  32. vel = self.vel + self.acceleration * delta
  33. # check for limits of new velocity
  34. max = self.maxSpeed
  35. vel.truncate(max)
  36. return vel
  37. def update(self, delta):
  38. ''' update vehicle position and orientation '''
  39. self.acceleration = self.calculateAcceleration(delta)
  40. self.vel = self.calculateVelocity(delta)
  41. # update position
  42. self.pos += self.vel * delta
  43. # update heading is non-zero velocity (moving)
  44. if self.vel.lengthSq() > 0.00000001:
  45. self.heading = self.vel.get_normalised()
  46. self.side = self.heading.perp()
  47. self.world.wrap_around(self.pos)
  48. # Draw ourselves
  49. def render(self):
  50. egi.set_pen_color(self.color)
  51. renderShape = self.getRenderShape()
  52. egi.closed_shape(renderShape)
  53. # Returns our base shape, appropriately rotated and translated into world space
  54. def getRenderShape(self):
  55. matrix = Matrix33()
  56. matrix.rotate_update(self.rotation)
  57. matrix.transform_vector2d_list(self.rockShape)
  58. renderShape = copy.deepcopy(self.rockShape)
  59. matrix = Matrix33()
  60. matrix.translate_update(self.pos.x, self.pos.y)
  61. matrix.transform_vector2d_list(renderShape)
  62. return renderShape
  63. def speed(self):
  64. return self.vel.length()
  65. def speedSqrt(self):
  66. return sqrt(self.speed())
  67. # Quick collision detection
  68. def containsPoint(self, point):
  69. return self.pos.distanceSq(point) < self.boundingRadius**2
  70. # Returns a rocky looking shape based on our boundingRadius and edges
  71. def getRockShape(self):
  72. radius = self.boundingRadius
  73. edges = self.edges
  74. angle = 2 * pi / edges
  75. pts = []
  76. variance = 0.18
  77. variation = (1 - variance, 1 + variance)
  78. for i in range(edges):
  79. v = Vector2D(cos(i * angle), sin(i * angle)) * radius
  80. v.x *= uniform(variation[0],variation[1])
  81. v.y *= uniform(variation[0],variation[1])
  82. pts.append(v)
  83. return pts