Ejemplo n.º 1
0
class Bulb(Actor):
    def __init__(self, type):
        Actor.__init__(self, "sprites/bulb" + str(type) + ".gif", 2)


# --------------------- class Switch --------------------------
class Switch(Actor, GGMouseListener):
    def __init__(self, bulb):
        Actor.__init__(self, "sprites/switch.gif", 2)
        self.bulb = bulb

    def mouseEvent(self, mouse):
        location = gg.toLocationInGrid(mouse.getX(), mouse.getY())
        if location.equals(self.getLocation()):
            self.showNextSprite()
        self.bulb.show(self.getIdVisible())
        gg.refresh()
        return False


# --------------------- main ---------------------------------
gg = GameGrid(7, 4, 40, False)
gg.setBgColor(X11Color("lightgray"))
for i in range(3):
    bulb = Bulb(i)
    gg.addActor(bulb, Location(2 * i + 1, 1))
    switch = Switch(bulb)
    gg.addActor(switch, Location(2 * i + 1, 3))
    gg.addMouseListener(switch, GGMouse.lPress)
gg.show()
Ejemplo n.º 2
0
# --------------------- class CapturedActor ------------------
class CapturedActor(Actor):
   def __init__(self, isRotatable, imagePath, nbSprites):
      Actor.__init__(self, isRotatable, imagePath, nbSprites)
   
   def move(self, dist):
      pt = gg.toPoint(self.getNextMoveLocation())
      dir = self.getDirection()
      if pt.x < 0 or pt.x > gg.getPgWidth():
         self.setDirection(180 - dir)
         self.setHorzMirror(not self.isHorzMirror())
      if pt.y < 0 or pt.y > gg.getPgHeight():
         self.setDirection(360 - dir)
      Actor.move(self, dist)

# --------------------- class MyActListener ------------------
class MyActListener(GGActListener):
   def act(self):
      head.move(10)

# --------------------- main ----------------------------------
head = CapturedActor(False, "sprites/head_0.gif", 1)
gg = GameGrid(800, 600, 1)
gg.addActListener(MyActListener())
gg.setSimulationPeriod(40)
gg.setBgColor(X11Color("lightgray"))
gg.addActor(head, Location(400, 200), 66)
gg.show();