def __init__(self): GameGrid.__init__(self, 10, 10, 60, GameGrid.RED, "sprites/reef.gif") self.addActor(nemo, Location(2, 4)) self.show()
if self.getX() < 0 or self.getX() > 599: self.turn(180) self.setHorzMirror(not self.isHorzMirror()) def keyPressed(self, evt): keyCode = evt.getKeyCode() if keyCode == KeyEvent.VK_UP and self.getY() > 0: self.setY(self.getY() - 1) if keyCode == KeyEvent.VK_DOWN and self.getY() < 599: self.setY(self.getY() + 1) # ----------------- class MyKeyListener -------------- class MyKeyListener(GGKeyListener): def keyPressed(self, evt): if evt.getKeyCode() == KeyEvent.VK_SPACE: fish = Angelfish() gg.addActor(fish, Location(300, 300)) gg.addKeyListener(fish) return False # Don't consume # --------------------- main --------------------------------- gg = GameGrid(600, 600, 1, None, "sprites/reef.gif", False) gg.setSimulationPeriod(50) gg.setTitle("Space bar for new object, cursor to move up/down") gg.addKeyListener(MyKeyListener()) gg.show() gg.doRun()
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()
# 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()
self.setDirection(math.degrees(phi)) self.oldLocation.x = location.x self.oldLocation.y = location.y 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
# --------------------- 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 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()
# --------------------- class Rocket --------------------- class Rocket(Actor): def __init__(self): Actor.__init__(self, "sprites/rocket.gif") def act(self): self.move() if self.getX() < -100 or self.getX() > 700: self.turn(180) if self.isHorzMirror(): self.setHorzMirror(False) gg.setPaintOrder(Rocket) # Rocket in foreground else: self.setHorzMirror(True) gg.setPaintOrder(Earth) # Earth in foreground # --------------------- main --------------------- gg = GameGrid(600, 200, 1, None, False) gg.setSimulationPeriod(50) earth = Earth() gg.addActor(earth, Location(300, 100)) # Rocket added last->default paint order: in front of earth rocket = Rocket() gg.addActor(rocket, Location(200, 100)) gg.show() gg.doRun()
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 Player ------------------------- class Player(CapturedActor): def __init__(self): CapturedActor.__init__(self, False, "sprites/head.gif", 3) def act(self): if self.getNbCycles() % 5 == 0: self.showNextSprite() CapturedActor.move(self, 10) # --------------------- main ---------------------------------- gg = GameGrid(800, 600, 1, False) gg.setSimulationPeriod(40) gg.setBgColor(X11Color("lightgray")) for i in range(3): player = Player() startLocation = Location(200 + 100 * i, 125) startDirection = 15 * i gg.addActor(player, startLocation, startDirection) gg.show() gg.doRun()
tcpNode.disconnect() gg.dispose() def deployShips(): for i in range(nbShips): gg.addActor(Ship(), gg.getRandomEmptyLocation()) # ---------- main() ---------------------- sessionPrefix = "ama&19td" nickname = "captain" tcpNode = TcpNode(messageReceived=messageReceived, statusReceived=statusReceived) isMyMove = False nbShips = 7 loc = None myScore = 0 enemyScore = 0 GAME_START = 0 SHIP_MISSED = 1 SHIP_HIT = 2 gg = GameGrid(6, 6, 50, X11Color("red"), False, notifyExit=notifyExit) gg.setTitle("TcpBattleShip") deployShips() gg.addMouseListener(MyMouseListener(), GGMouse.lClick) gg.show() connect()
if self.getX() == 0 or self.getX() == 9: self.turn(180) self.setHorzMirror(not self.isHorzMirror()) # ---------------- class MyActListener ---------------- class MyActListener(GGActListener): def act(self): global fish if gg.isKeyPressed(KeyEvent.VK_SPACE): fish = Clownfish() gg.addActor(fish, Location(0, 0)) if fish == None: # no fish yet return if gg.isKeyPressed(KeyEvent.VK_UP) and fish.getY() > 0: fish.setY(fish.getY() - 1) if gg.isKeyPressed(KeyEvent.VK_DOWN) and fish.getY() < 9: fish.setY(fish.getY() + 1) if gg.isKeyPressed(KeyEvent.VK_SPACE): fish = Clownfish() gg.addActor(fish, Location(0, 0)) # --------------------- main --------------------------------- gg = GameGrid(10, 10, 60, X11Color("red"), False) gg.setTitle("Space bar for new object, cursor to move up/down") gg.addActListener(MyActListener()) fish = None gg.show() gg.doRun()
# --------------------- class Dart -------------------------- class Dart(Actor, GGMouseListener): def __init__(self): Actor.__init__(self, True, "sprites/dart.gif") # Rotatable self.oldLocation = Location() def mouseEvent(self, mouse): location = gg.toLocationInGrid(mouse.getX(), mouse.getY()) self.setLocation(location) dx = location.x - self.oldLocation.x dy = location.y - self.oldLocation.y if (dx * dx + dy * dy < 25): return True phi = math.atan2(dy, dx) self.setDirection(math.degrees(phi)) self.oldLocation.x = location.x self.oldLocation.y = location.y return True # --------------------- main --------------------------------- gg = GameGrid(400, 300, 1, False) gg.setTitle("Move dart using mouse left button drag") gg.setSimulationPeriod(50) gg.setBgColor(X11Color("lightblue")) dart = Dart() gg.addActor(dart, Location(50, 50)) gg.addMouseListener(dart, GGMouse.lDrag) gg.show() gg.doRun()
self.move() self.turn(90) self.tryToEat() def tryToEat(self): actor = gg.getOneActorAt(self.getLocation(), self.clazz) if actor != None: actor.hide() # --------------------- class Dog --------------------- class Dog(Animal): def _init__(self, clazz): Animal._init(self, "sprites/dog.gif", clazz) # --------------------- class Bear --------------------- class Bear(Animal): def _init__(self, clazz): Animal._init(self, "sprites/bear.gif", clazz) # --------------------- main --------------------- gg = GameGrid(10, 10, 60, X11Color("red"), False) bear = Bear("sprites/bear.gif", Dog) dog = Dog("sprites/dog.gif", Bear) gg.addActor(bear, Location(0, 5)) gg.addActor(dog, Location(9, 5), 180) # gg.setActOrder(Bear) gg.show() gg.doRun()
] def initStars(): for i in range(nbStars): size = random.randint(10, 41) star = Star(size, colors[i]) stars.append(star) def drawParadies(): global dy bg.clear(Color(175, 175, 255)) for i in range(nbStars): stars[i].turn(rotInc[i]) stars[i].showAt(mx[i], my[i] + dy) dy += 2 nbStars = 20 stars = [] dy = 0 gg = GameGrid(600, 600, 1, False) gg.setSimulationPeriod(50) gg.addActListener(MyActListener()) bg = gg.getBg() initStars() gg.addActor(Angel(), Location(300, 300), -22) gg.show() gg.doRun()
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()
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()
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)
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 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()
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()