Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. class Centipede:
  2. def __init__(self):
  3. self.location = PVector(random(width), random(height))
  4. self.velocity = PVector(0, 0)
  5. self.acceleration = PVector(0, 0)
  6. self.top_speed = 3
  7.  
  8. #mouse is a private variable (set using trailing double underscores)
  9. follow = PVector(0, 0)
  10. self.dir = PVector(0, 0)
  11.  
  12. def update(self):
  13. follow = PVector(0,0)
  14. self.dir = PVector.sub(follow, self.location)
  15.  
  16. self.dir.normalize()
  17. self.dir.mult(0.5)
  18.  
  19. self.acceleration = self.dir
  20. self.acceleration.mult(0.15)
  21. self.acceleration.mult(random(2))
  22.  
  23. self.velocity.add(self.acceleration)
  24. self.velocity.limit(self.top_speed)
  25. self.location.add(self.velocity)
  26.  
  27. def display(self):
  28. stroke(0)
  29. fill(255)
  30. ellipse(self.location.x, self.location.y, 16, 16)
  31.  
  32. class Mover:
  33. def __init__(self):
  34. self.location = PVector(random(width), random(height))
  35. self.velocity = PVector(0, 0)
  36. self.acceleration = PVector(0, 0)
  37. self.top_speed = 3
  38.  
  39. #mouse is a private variable (set using trailing double underscores)
  40. __mouse = PVector(0, 0)
  41. self.dir = PVector(0, 0)
  42.  
  43. def update(self):
  44.  
  45. __mouse = PVector(mouseX, mouseY)
  46. self.dir = PVector.sub(__mouse, self.location)
  47.  
  48. self.dir.normalize()
  49. self.dir.mult(0.5)
  50.  
  51. self.acceleration = self.dir
  52. self.acceleration.mult(0.15)
  53. self.acceleration.mult(random(2))
  54.  
  55. self.velocity.add(self.acceleration)
  56. self.velocity.limit(self.top_speed)
  57. self.location.add(self.velocity)
  58.  
  59.  
  60. def display(self):
  61. stroke(0)
  62. fill(255)
  63. ellipse(self.location.x, self.location.y, 16, 16)
  64.  
  65. def checkEdges(self):
  66. if self.location.x > width:
  67. self.location.x = 0
  68. elif self.location.x < 0:
  69. self.location. x = width
  70.  
  71. if self.location.y > height:
  72. self.location.y = 0
  73. elif self.location.y < 0:
  74. self.location.y = height
  75.  
  76. centipedes = [0] * 20
  77.  
  78. def setup():
  79. size (640, 360)
  80.  
  81. global mover
  82. mover = Mover()
  83.  
  84. global centipedes
  85. for index in range(20):
  86. centipedes[index] = Centipede()
  87. centipedes[index].follow = PVector.sub(mover.location, centipedes[index].location)
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. def draw():
  95. background(0)
  96. mover.update()
  97. mover.checkEdges()
  98. mover.display()
  99. for index in range(20):
  100. centipedes[index].update()
  101. centipedes[index].display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement