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() gg.show()
# --------------------- 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();
class Ghost(Actor): def __init__(self): Actor.__init__(self, "sprites/ghost.gif", 4) def act(self): if self.getNbCycles() % 5 == 0: self.showNextSprite() pt = gg.toPoint(self.getNextMoveLocation()) dir = self.getDirection() if pt.x < 0 or pt.x > gg.getPgWidth(): self.setDirection(180 - dir) if pt.y < 0 or pt.y > gg.getPgHeight(): self.setDirection(360 - dir) self.move() # --------------------- main --------------------------------- gg = GameGrid(800, 600, 1, None, True) gg.setSimulationPeriod(20) gg.setBgColor(X11Color("darkgray")) for i in range(7): ghost = Ghost() startLocation = Location(310 + 30 * i, 100) startDirection = 66 + 66 * i gg.addActor(ghost, startLocation, startDirection) gg.show()