def letter(x, s): w = s.length() // 5 for k in range(5): for i in range(w): if s.charAt(k * w + i) == 'x': showBrick(x + i, k) def showBrick(i, k): tm.setImage("sprites/brick.gif", i, k) for n in range(4): balls[n].addCollisionTile(Location(i, k)) gg = GameGrid(620, 180, 1, False) gg.setBgColor(X11Color("darkblue")) gg.setSimulationPeriod(50) gg.addActListener(MyActListener()) tm = gg.createTileMap(nbHorzTiles, nbVertTiles, tileSize, tileSize) tm.setPosition(Point(xMapStart, yMapStart)) for n in range(4): ball = Ball() gg.addActor(ball, gg.getRandomLocation(), gg.getRandomDirection()) ball.setCollisionCircle(Point(0, 0), 8) ball.addTileCollisionListener(MyTileCollisionListener()) if n == 2 or n == 3: ball.show(1) balls.append(ball) createText()
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()
def __init__(self): GameGrid.__init__(self, 10, 10, 60, X11Color("red"), "sprites/reef.gif") self.addActor(nemo, Location(2, 4)) self.addActListener(MyActListener()) self.show()
else: # at right border self.setHorzMirror(True) self.turn(90) self.move() self.turn(90) self.tryToEat() def tryToEat(self): actor = gg.getOneActorAt(self.getLocation(), Honey) if actor != None: actor.hide() # --------------------- class MyMouseListener -------------------------- class MyMouseListener(GGMouseListener): def mouseEvent(self, mouse): location = gg.toLocationInGrid(mouse.getX(), mouse.getY()) if gg.isEmpty(location): # Do not create an actor if cell is occupied honey = Honey() gg.addActor(honey, location); gg.addMouseListener(honey, GGMouse.lPress | GGMouse.lDrag | GGMouse.lRelease) return False # Don't consume the event, other listeners must be notified # --------------------- main --------------------------------- gg = GameGrid(10, 10, 60, X11Color("green"), False) gg.setTitle("Click to create a honey pot, press and drag to move it") gg.addActor(Bear(), Location(0, 0)) gg.addMouseListener(MyMouseListener(), GGMouse.lPress) gg.show() gg.doRun()
# Ex03a.py from ch.aplu.jgamegrid import GameGrid, Actor, Location, GGActListener from ch.aplu.util import X11Color # ----------------- class MyActListener -------------- class MyActListener(GGActListener): def act(self): nemo.move() if not nemo.isMoveValid(): nemo.turn(180) nemo.setHorzMirror(not nemo.isHorzMirror()) # ----------------- main ----------------------------- nemo = Actor("sprites/nemo.gif") gg = GameGrid(10, 10, 60, X11Color("red"), "sprites/reef.gif") gg.addActor(nemo, Location(2, 4)) gg.addActListener(MyActListener()) gg.show()
return True # --------------------- MyActorCollisionListener ------------ class MyActorCollisionListener(GGActorCollisionListener): def collide(self, actor1, actor2): gg.setMouseEnabled(False) # Inhibit further mouse events Monitor.wakeUp() # Resume processing return 0 # --------------------- main --------------------------------- gg = GameGrid(400, 300, 1, False) gg.setTitle("Move dart using mouse left button drag") gg.setSimulationPeriod(50) gg.setBgColor(X11Color("lightblue")) gg.playSound(GGSound.DUMMY) dart = Dart() dart.setCollisionSpot(Point(30, 0)) dart.addActorCollisionListener(MyActorCollisionListener()) gg.addActor(dart, Location(50, 50)) gg.addMouseListener(dart, GGMouse.lDrag) balloon = Actor("sprites/balloon.gif", 2) gg.addActor(balloon, Location(300, 200)) balloon.setCollisionImage(0) # Select IMAGE type detection dart.addCollisionActor(balloon) gg.show() gg.doRun()
def askColor(title, defaultColor): if isinstance(defaultColor, str): return JColorChooser.showDialog(None, title, X11Color.toColor(defaultColor)) else: return JColorChooser.showDialog(None, title, defaultColor)
def getRandomX11Color(): return X11Color.getRandomColorStr()
if self.isHorzMirror(): self.setHorzMirror(False) self.turn(270) self.move() self.turn(270) else: self.setHorzMirror(True) self.turn(90) self.move() self.turn(90) self.tryToEat() def tryToEat(self): actor = gg.getOneActorAt(self.getLocation(), Leaf) if actor != None: actor.hide() # --------------------- class Leaf --------------------------- class Leaf(Actor): def __init__(self): Actor.__init__(self, "sprites/leaf.gif") # ----------------- main ----------------------------- gg = GameGrid(10, 10, 60, X11Color("red")) gg.addActor(Bear(), Location(0, 0)) for i in range(10): gg.addActor(Leaf(), gg.getRandomEmptyLocation()) gg.show()
class Rock(Actor): def __init__(self): Actor.__init__(self, "sprites/rock.gif") # --------------------- class Hazelnut ----------------------- class Hazelnut(Actor): def __init__(self): Actor.__init__(self, "sprites/hazelnut.gif") # --------------------- class MyButtonListener ---------------- class MyButtonListener(GGButtonListener): def buttonClicked(self, button): gg.dispose() # ----------------- main ----------------------------- exitButton = GGButton("sprites/ggExitButtonA.gif") gg = GameGrid(10, 10, 50, X11Color("green"), None, False, True) # undecorated gg.setBgColor(X11Color("darkGray")) gg.addActor(exitButton, Location(9, 9)) exitButton.addButtonListener(MyButtonListener()) for i in range(10): gg.addActor(Rock(), gg.getRandomEmptyLocation()) for i in range(20): gg.addActor(Hazelnut(), gg.getRandomEmptyLocation()) gg.addActor(Hamster(), gg.getRandomEmptyLocation()) gg.show() gg.doRun()
break def canMove(self): if self.isMoveValid() and gg.getOneActorAt(self.getNextMoveLocation(), Rock) == None: return True # Inside grid and no rock return False # --------------------- class Rock --------------------------- class Rock(Actor): def __init__(self): Actor.__init__(self, "sprites/rock.gif") # --------------------- class Hazelnut ----------------------- class Hazelnut(Actor): def __init__(self): Actor.__init__(self, "sprites/hazelnut.gif") # ----------------- main ----------------------------- gg = GameGrid(10, 10, 50, X11Color("green")) gg.setBgColor(X11Color("darkGray")) for i in range(10): gg.addActor(Rock(), gg.getRandomEmptyLocation()) for i in range(20): gg.addActor(Hazelnut(), gg.getRandomEmptyLocation()) gg.addActor(Hamster(), gg.getRandomEmptyLocation()) gg.show()