Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 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. self.dir = PVector.sub(__mouse, self.location)
  14.  
  15. self.dir.normalize()
  16. self.dir.mult(0.5)
  17.  
  18. self.acceleration = self.dir
  19. self.acceleration.mult(0.15)
  20. self.acceleration.mult(random(2))
  21.  
  22. self.velocity.add(self.acceleration)
  23. self.velocity.limit(self.top_speed)
  24. self.location.add(self.velocity)
  25.  
  26. class Mover:
  27. def __init__(self):
  28. self.location = PVector(random(width), random(height))
  29. self.velocity = PVector(0, 0)
  30. self.acceleration = PVector(0, 0)
  31. self.top_speed = 3
  32.  
  33. #mouse is a private variable (set using trailing double underscores)
  34. __mouse = PVector(0, 0)
  35. self.dir = PVector(0, 0)
  36.  
  37. def update(self):
  38.  
  39. __mouse = PVector(mouseX, mouseY)
  40. self.dir = PVector.sub(__mouse, self.location)
  41.  
  42. self.dir.normalize()
  43. self.dir.mult(0.5)
  44.  
  45. self.acceleration = self.dir
  46. self.acceleration.mult(0.15)
  47. self.acceleration.mult(random(2))
  48.  
  49. self.velocity.add(self.acceleration)
  50. self.velocity.limit(self.top_speed)
  51. self.location.add(self.velocity)
  52.  
  53.  
  54. def display(self):
  55. stroke(0)
  56. fill(255)
  57. ellipse(self.location.x, self.location.y, 16, 16)
  58.  
  59. def checkEdges(self):
  60. if self.location.x > width:
  61. self.location.x = 0
  62. elif self.location.x < 0:
  63. self.location. x = width
  64.  
  65. if self.location.y > height:
  66. self.location.y = 0
  67. elif self.location.y < 0:
  68. self.location.y = height
  69.  
  70. def setup():
  71. size (640, 360)
  72.  
  73. global mover
  74. mover = Mover()
  75.  
  76. global centipede
  77. centipede = Centipede()
  78.  
  79. centipede.__follow= PVector(mover.x,mover.y)
  80.  
  81. background(0)
  82.  
  83.  
  84. def draw():
  85. mover.update()
  86. mover.checkEdges()
  87. mover.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement