More fun with pyglet

12 views
Skip to first unread message

Søren Kyale

unread,
Apr 23, 2013, 4:43:26 PM4/23/13
to uic-mcs...@googlegroups.com
I added keyboard and mouse interaction. Also a function for rattling sprites.

tracking window movement and feeding it to a function to move the sprites.

   def update(self, dt):
      # print self.get_location()[0]
      position0, positionA = self.winPosition, numpy.array((self.get_location()[0], -(self.get_location()[1])))
      deltaShift = positionA - position0
      self.winSpeed = deltaShift
      self.winPosition = positionA
      self.yesWindowShift = numpy.linalg.norm(deltaShift) > 0
      if self.yesWindowShift:
         for aThing in self.things: aThing.shift(deltaShift)
      for index, aThing in enumerate(self.things):
         aThing.move(self.things[index+1:],yesWindowShift=self.yesWindowShift, winSpeed=self.winSpeed)
      self.winSpeed, self.yesWindowShift = numpy.zeros(2), False


mouse interaction:
   def on_mouse_press(self, x,y,button, mods):
      mousePoint = numpy.array((x,y))
      if button == 1:
         for aThing in self.things: aThing.punt(mousePoint, amplification = 3 * (mods & 3) if mods & 3 else 1)
      if button == 4:
         for i, aThing in enumerate(self.things):
            if aThing.pointIntersect(mousePoint):
               if mods & 3 == 3: aThing.acceleration = numpy.zeros(2)
               if mods & 3 == 1: aThing.acceleration = numpy.random.random(2)-.5
               if mods & 3 == 2: self.things.pop(i)
               aThing.kick(numpy.array((0,0)))
      print "we tried to punt a thing at", mousePoint, " with button ", button, " and mods: ", mods

keyboard interaction:
   def on_key_press(self, symbol, mods):
      print symbol, " was pressed with ", mods
      if symbol == 65307: exit()
      if symbol == 32: self.addThing()
      if symbol == 115:
         for aThing in self.things: aThing.kick(numpy.zeros(2))
      if symbol == 99:
         self.yesCrazy = not self.yesCrazy
         for aThing in self.things:
            aThing.acceleration = (numpy.random.random(2)-.5)*.67 if self.yesCrazy else numpy.zeros(2)
      if symbol == 107: self.things = []
      if symbol == 65361: self.set_size(self.get_size()[0]-100, self.get_size()[1])
      if symbol == 65363: self.set_size(self.get_size()[0]+100, self.get_size()[1])
      if symbol == 65362: self.set_size(self.get_size()[0], self.get_size()[1]-100)
      if symbol == 65364: self.set_size(self.get_size()[0], self.get_size()[1]+100)
ball.py
lab9balls.py
Reply all
Reply to author
Forward
0 new messages