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))
def messageReceived(sender, message): print "Got message: " + message global isMyMove, enemyScore, myScore, loc x = ord(message[0]) - 48 # We get ASCII code of number y = ord(message[1]) - 48 if x == 9: # Got command if y == GAME_START: isMyMove = False gg.setTitle("Wait! Enemy shoots first.") if y == SHIP_HIT: gg.addActor(Actor("sprites/checkgreen.gif"), loc) myScore += 1 if myScore == nbShips: gameOver(True) if y == SHIP_MISSED: gg.addActor(Actor("sprites/checkred.gif"), loc) else: # Got coordinates loc = Location(x, y) actor = gg.getOneActorAt(loc, Ship) if actor != None: actor.removeSelf() gg.addActor(Actor("sprites/explosion.gif"), loc) tcpNode.sendMessage("9" + str(SHIP_HIT)) enemyScore += 1 if enemyScore == nbShips: gameOver(False) return else: tcpNode.sendMessage("9" + str(SHIP_MISSED)) gg.setTitle("Shoot now! Score: " + str(myScore) + " (" + str(enemyScore) + ")") isMyMove = True
def initSortBtn(): global sortBtn toolBar = ToolBar(cg) sortBtn = ToolBarItem("sprites/sortBtn.gif", 2) toolBar.addItem(sortBtn) toolBar.show(Location(233, 212)) toolBar.addToolBarListener(MySortBtnListener())
def gameOver(isWinner): global isMyMove isMyMove = False gg.removeAllActors() gg.addActor(Actor("sprites/gameover.gif"), Location(3, 3)) if isWinner: gg.setTitle("You win!") else: gg.setTitle("You lost!")
def initToolBar(): global cutBtn global numbers numbers = ToolBarStack("sprites/number30.gif", 10) separator = ToolBarSeparator(2, 30, X11Color("black")) cutBtn = ToolBarItem("sprites/cutBtn.gif", 3) toolBar = ToolBar(cg) toolBar.addItem(numbers, separator, cutBtn) toolBar.show(Location(7, 160)) toolBar.addToolBarListener(MyToolBarListener())
def initToolBar(): global upBtn, downBtn, numbers upBtn = ToolBarItem("sprites/up30.gif", 2) numbers = ToolBarStack("sprites/number30.gif", 10) downBtn = ToolBarItem("sprites/down30.gif", 2) toolBar = ToolBar(cg) toolBar.addItem(upBtn, numbers, downBtn) toolBar.show(Location(160, 185)) numbers.show(3) # Default start number toolBar.addToolBarListener(MyToolBarListener())
def mouseTouched(self, actor, mouse, spot): global hotspot event = mouse.getEvent() if event == GGMouse.lPress: hotspot = spot if event == GGMouse.lDrag: actor.setLocation( Location(mouse.getX() - hotspot.x, mouse.getY() - hotspot.y)) if actor.getLocation().getDistanceTo(handLocation) < 50: insertInHand(actor) if event == GGMouse.lRelease: insertInHand(actor)
def act(self): global fish global isSpaceKeyDown if gg.isKeyPressed(KeyEvent.VK_SPACE) and not isSpaceKeyDown: fish = Clownfish() gg.addActor(fish, Location(0, 0)) fish.setSlowDown(4) isSpaceKeyDown = gg.isKeyPressed(KeyEvent.VK_SPACE) 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)
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()
def __init__(self): GameGrid.__init__(self, 10, 10, 60, GameGrid.RED, "sprites/reef.gif") self.addActor(nemo, Location(2, 4)) self.show()
# Ex02.py from ch.aplu.jgamegrid import Location from ch.aplu.jcardgame import Deck, CardGame, RowLayout, StackLayout, CardListener from ch.aplu.jcardgame.Hand import SortType # ------------------ class MyCardListener ---------------------------------- class MyCardListener(CardListener): def leftDoubleClicked(self, card): card.transfer(stock, True) # ------------------------ main -------------------------------------------- Suit = enum("SPADES", "HEARTS", "DIAMONDS", "CLUBS") Rank = enum("ACE", "KING", "QUEEN", "JACK", "TEN", "NINE", "EIGHT", "SEVEN", "SIX") deck = Deck(Suit.values(), Rank.values(), "cover"); cg = CardGame(600, 600) hands = deck.dealingOut(1, 9) stock = hands[1] stock.setView(cg, StackLayout(Location(300, 150))) stock.draw() hands[0].setView(cg, RowLayout(Location(300, 400), 500)) hands[0].sort(SortType.SUITPRIORITY, False) hands[0].draw() hands[0].addCardListener(MyCardListener()) hands[0].setTouchEnabled(True) cg.setDoubleClickDelay(300)
def mouseEvent(self, mouse): Monitor.wakeUp() return True # ------------------------ main ------------------------------------------ Suit = enum("SPADES", "HEARTS", "DIAMONDS", "CLUBS") Rank = enum("ACE", "KING", "QUEEN", "JACK", "TEN", "NINE", "EIGHT", "SEVEN", "SIX") deck = Deck(Suit.values(), Rank.values(), "cover") cg = CardGame(900, 615, 30) cg.addMouseListener(MyMouseListener(), GGMouse.lPress) while True: hand = deck.dealingOut(1, 25)[0] rowLayout = RowLayout(Location(450, 80), 890) hand.setView(cg, rowLayout) hand.sort(SortType.RANKPRIORITY, True) hand.draw() cg.setStatusText("Click to get pairs, trips and quads") Monitor.putSleep() if cg.isDisposed(): break pairs = hand.extractPairs() for i in range(len(pairs)): pairs[i].setView(cg, RowLayout(Location(70 + 150 * i, 230), 120)) pairs[i].draw() trips = hand.extractTrips() for i in range(len(trips)): trips[i].setView(cg, RowLayout(Location(70 + 150 * i, 380), 120))
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()
toolBar = ToolBar(cg) toolBar.addItem(upBtn, numbers, downBtn) toolBar.show(Location(160, 185)) numbers.show(3) # Default start number toolBar.addToolBarListener(MyToolBarListener()) def sortAndDrawHands(): upperHand.sort(SortType.RANKPRIORITY, True) lowerHand.sort(SortType.RANKPRIORITY, True) # ------------------------ main -------------------------------------------- Suit = enum("SPADES", "HEARTS", "DIAMONDS", "CLUBS") Rank = enum("ACE", "KING", "QUEEN", "JACK", "TEN", "NINE", "EIGHT", "SEVEN", "SIX") deck = Deck(Suit.values(), Rank.values(), "cover") upBtn = None downBtn = None numbers = None cg = CardGame(460, 400) hands = deck.dealingOut(2, 5, True) upperHand = hands[0] upperHand.setView(cg, RowLayout(Location(230, 100), 400)) lowerHand = hands[1] lowerHand.setView(cg, RowLayout(Location(230, 300), 400)) sortAndDrawHands() initToolBar()
def initToolBar(): toolBar = ToolBar(cg) toolBar.addItem(textItem, separator0, spades, hearts, diamonds, clubs, separator1, okBtn) toolBar.show(Location(10, 10)) toolBar.addToolBarListener(MyToolBarListener())
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 showBrick(i, k): tm.setImage("sprites/brick.gif", i, k) for n in range(4): balls[n].addCollisionTile(Location(i, k))
def insertInHand(actor): actor.removeSelf() card = actor.getCard() hand.insert(card, False) hand.sort(SortType.RANKPRIORITY, True) takeFromTalon() def addActor(cardActor): cg.addActor(cardActor, Location(talonLocation)) cardActor.addMouseTouchListener( MyMouseTouchListener(), GGMouse.lPress | GGMouse.lDrag | GGMouse.lRelease, True) # ------------------------ main -------------------------------------------- handLocation = Location(250, 100) talonLocation = Location(250, 300) hotSpot = Point(0, 0) cg = CardGame(500, 400, 30) hands = deck.dealingOut(1, 2) # only two cards in hand hand = hands[0] hand.setView(cg, RowLayout(handLocation, 400)) talon = hands[1] talon.setView(cg, StackLayout(talonLocation)) hand.draw() talon.draw() takeFromTalon() cg.setStatusText("Drag cards from card talon to the hand")
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
hands[i].sort(SortType.RANKPRIORITY, False) else: hands[i].setVerso(True) hands[i].draw() talonCover.removeSelf() hands[0].setTouchEnabled(True) # ------------------------ main -------------------------------------------- Suit = enum("SPADES", "HEARTS", "DIAMONDS", "CLUBS") Rank = enum("ACE", "KING", "QUEEN", "JACK", "TEN", "NINE", "EIGHT", "SEVEN", "SIX") deck = Deck(Suit.values(), Rank.values(), "cover") hands = None talon = None nbPlayers = 4 handLocations = [ Location(300, 525), Location(75, 300), Location(300, 75), Location(525, 300) ] talonLocation = Location(300, 300) packetSize = 3 cg = CardGame(600, 600, 30) dealingOut() cg.setStatusText("Dealing Out...done")
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() while True: Monitor.putSleep() if gg.isDisposed(): break dart.hide() # Hide dart balloon.show(1) # Show exlode image
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()
deck.getRank(id) card = Card(deck, deck.getSuit(i), deck.getRank(id)) break return card # ------------------------ main -------------------------------------------- Suit = enum("SPADES", "HEARTS", "DIAMONDS", "CLUBS") Rank = enum("ACE", "KING", "QUEEN", "JACK", "TEN", "NINE", "EIGHT", "SEVEN", "SIX", "FIVE", "FOUR", "THREE", "TWO") nbRanks = len(Rank.values()) deck = Deck(Suit.values(), Rank.values(), "cover") hand = Hand(deck) sortBtn = None textItem = ToolBarText("Select Card:", 30) separator0 = ToolBarSeparator(2, 30, X11Color("black")) spades = ToolBarStack("sprites/spades.gif", nbRanks) hearts = ToolBarStack("sprites/hearts.gif", nbRanks) diamonds = ToolBarStack("sprites/diamonds.gif", nbRanks) clubs = ToolBarStack("sprites/clubs.gif", nbRanks) separator1 = ToolBarSeparator(2, 30, X11Color("black")) okBtn = ToolBarItem("sprites/ok30.gif", 2) cg = CardGame(300, 250, 30) cg.setStatusText("Select a card by clicking on the card stacks and press OK.") hand.setView(cg, RowLayout(Location(150, 125), 290)) hand.draw() initToolBar()
# 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()
# ------------------------ main -------------------------------------------- isDebug = False Suit = enum("KREUZ", "HERZ", "KARO", "PIK") if isDebug: Rank = enum("ASS", "KOENIG", "DAME", "BAUER", "ZEHN") else: Rank = enum("ASS", "KOENIG", "DAME", "BAUER", "ZEHN", "NEUN", "ACHT", "SIEBEN", "SECHS") deck = Deck(Suit.values(), Rank.values(), "cover") # ------------ Locations ------------------ handLocations = [ Location(300, 525), Location(75, 300), Location(300, 75), Location(525, 300) ] talonLocation = Location(250, 300) pileLocation = Location(350, 300) textLocation = Location(300, 400) trumpActorLocation = Location(350, 200) toolBarLocation = Location(470, 567) btnLocation = Location(100, 580) hideLocation = Location(-500, -500) toolBarTextLocation = Location(502, 550) # ------------------------------------------ version = "3.1"
def __init__(self): Actor.__init__(self, True, "sprites/dart.gif") # Rotatable self.oldLocation = Location()
# --------------------- 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();
def addActor(cardActor): cg.addActor(cardActor, Location(talonLocation)) cardActor.addMouseTouchListener( MyMouseTouchListener(), GGMouse.lPress | GGMouse.lDrag | GGMouse.lRelease, True)
class MyMouseListener(GGMouseListener): def mouseEvent(self, mouse): Monitor.wakeUp() return True # ------------------------ main -------------------------------------------- Suit = enum("SPADES", "HEARTS", "DIAMONDS", "CLUBS") Rank = enum("ACE", "KING", "QUEEN", "JACK", "TEN", "NINE", "EIGHT", "SEVEN", "SIX") deck = Deck(Suit.values(), Rank.values(), "cover") cg = CardGame(900, 615, 30) cg.addMouseListener(MyMouseListener(), GGMouse.lPress) while True: hand = deck.dealingOut(1, 25)[0] hand.setView(cg, RowLayout(Location(450, 80), 890)) hand.sort(SortType.RANKPRIORITY, True) sequence3 = hand.extractSequences(Suit.HEARTS, 3) for i in range(len(sequence3)): sequence3[i].setView(cg, RowLayout(Location(70 + 150 * i, 230), 120)) sequence3[i].draw() sequence4 = hand.extractSequences(Suit.HEARTS, 4) for i in range(len(sequence4)): sequence4[i].setView(cg, RowLayout(Location(70 + 150 * i, 380), 120)) sequence4[i].draw() sequence5 = hand.extractSequences(Suit.HEARTS, 5) for i in range(len(sequence5)): sequence5[i].setView(cg, RowLayout(Location(100 + 200 * i, 530), 150))
for i in range(nbPlayers): eval.insert(bids[i].getFirst(), False) nbWinner = eval.getMaxPosition(SortType.RANKPRIORITY) cg.setStatusText("Round winner: " + str(nbWinner)) transferToStock(nbWinner) return nbWinner # ------------------------ main -------------------------------------------- Suit = enum("SPADES", "HEARTS", "DIAMONDS", "CLUBS") Rank = enum("ACE", "KING", "QUEEN", "JACK", "TEN") deck = Deck(Suit.values(), Rank.values(), "cover") nbPlayers = 4 handLocations = [ Location(300, 525), Location(75, 300), Location(300, 75), Location(525, 300), Location(300, 300) ] bidLocations = [ Location(300, 350), Location(250, 300), Location(300, 250), Location(350, 300) ] stockLocations = [ Location(400, 500), Location(100, 400), Location(200, 100),