raspberry pi zero based drum machine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sBall.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import FSM
  2. import utils as u
  3. import menus as m
  4. import random
  5. #====================================
  6. State = type("State", (object,), {})
  7. #====================================
  8. class State(object):
  9. def __init__(self, FSM):
  10. self.FSM = FSM
  11. def Enter(self):
  12. pass
  13. def Execute(self):
  14. pass
  15. def Exit(self):
  16. pass
  17. class Ball(State):
  18. def __init__(self,FSM):
  19. super(Ball, self).__init__(FSM)
  20. def Enter(self):
  21. self.ball_size = 10
  22. self.ballx = self.ball_size
  23. self.bally = self.ball_size
  24. self.ball_speed = 8
  25. self.ball_x_dir = 1
  26. self.ball_y_dir = 1
  27. self.ball2x = self.ball_size + 180
  28. self.ball2y = self.ball_size + 70
  29. self.ball2_x_dir = -1
  30. self.ball2_y_dir = -1
  31. self.xmult = 1.0
  32. o = self.FSM.owner
  33. o.header_text = "Balls"
  34. o.pub.register("beat", self)
  35. if o.keyState[16] > 0 or o.keyState[17] > 0:
  36. pass
  37. else:
  38. u.draw_header(o)
  39. o.update_display(0)
  40. super(Ball, self).Enter()
  41. def Execute(self):
  42. o = self.FSM.owner
  43. m.menu1_actions(self, o)
  44. if o.keyState[16] == 1:
  45. m.draw_menu1(o)
  46. o.update_display(0)
  47. if o.keyState[16] > 0 or o.keyState[17] > 0:
  48. pass
  49. else:
  50. if self.ballx < (0 + self.ball_size):
  51. self.ball_x_dir = 1
  52. if self.ballx > (o.width - self.ball_size):
  53. self.ball_x_dir = -1
  54. if self.bally < (0 + self.ball_size):
  55. self.ball_y_dir = 1
  56. if self.bally > (o.height - self.ball_size):
  57. self.ball_y_dir = -1
  58. self.ballx += self.ball_speed * self.ball_x_dir
  59. self.bally += self.ball_speed * self.ball_y_dir
  60. if self.ball2x < (0 + self.ball_size):
  61. self.ball2_x_dir = 1
  62. self.xmult = random.uniform(.6, 1.2)
  63. if self.ball2x > (o.width - self.ball_size):
  64. self.ball2_x_dir = -1
  65. self.xmult = random.uniform(.6, 1.2)
  66. if self.ball2y < (0 + self.ball_size):
  67. self.ball2_y_dir = 1
  68. if self.ball2y > (o.height - self.ball_size):
  69. self.ball2_y_dir = -1
  70. self.ball2x += (self.ball_speed * self.ball2_x_dir) * self.xmult
  71. self.ball2y += (self.ball_speed * self.ball2_y_dir)
  72. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  73. o.draw.ellipse((self.ballx - self.ball_size, self.bally - self.ball_size, self.ballx + self.ball_size, self.bally + self.ball_size), fill = o.light_grey, outline =o.light_grey)
  74. o.draw.ellipse((self.ball2x - self.ball_size, self.ball2y - self.ball_size, self.ball2x + self.ball_size, self.ball2y + self.ball_size), fill = o.light_grey, outline =o.light_grey)
  75. o.update_display(0)
  76. def ReceiveMessage(self, message):
  77. o = self.FSM.owner
  78. u.play_seq(o, message)
  79. def Exit(self):
  80. self.FSM.owner.pub.unregister("beat", self)