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 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)
def getTrumpActor(suit): if suit == Suit.CLUBS: return Actor("sprites/bigclub.gif") if suit == Suit.HEARTS: return Actor("sprites/bigheart.gif") if suit == Suit.DIAMONDS: return Actor("sprites/bigdiamond.gif") if suit == Suit.SPADES: return Actor("sprites/bigspade.gif") return None
def getTrumpActor(suit): if suit == Suit.KREUZ: return Actor("sprites/bigclub.gif") if suit == Suit.HERZ: return Actor("sprites/bigheart.gif") if suit == Suit.KARO: return Actor("sprites/bigdiamond.gif") if suit == Suit.PIK: return Actor("sprites/bigspade.gif") return None
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 checkOver(nbPlayer): if hands[nbPlayer].isEmpty(): cg.addActor(Actor("sprites/gameover.gif"), textLocation) cg.setStatusText("Game over. Winner is player: " + str(nbPlayer)) for i in range(nbPlayers): hands[i].setVerso(False) # Show player hands cg.refresh() cg.setMouseEnabled(False) cg.doPause() return True return False
def checkTalon(): # Returns false, if check fails because there are no cards available if talon.isEmpty(): # Talon empty, need to shuffle if pile.getNumberOfCards() < 2: cg.setStatusText("Fatal error: No cards available for talon") doPause() cg.setMouseEnabled(False) return False # Show info text actor = Actor("sprites/reshuffle.gif") cg.addActor(actor, textLocation) # Move animated card cover from playing pile to talon cardCover = CardCover(cg, pileLocation, deck, 1, 0, False) cardCover.slideToTarget(talonLocation, 2, False, True) # On bottom # Save card on pile top and remove it topCard = pile.getLast() pile.remove(topCard, False) # Shuffle cards pile.shuffle(False) # Hide all pictures pile.setVerso(True) # Insert into talon for card in pile.getCardList(): talon.insert(card, False) # Cleanup playing pile pile.removeAll(False) # Insert saved card pile.insert(topCard, False) # Redraw piles pile.draw() talon.draw() # Remove info text cg.delay(1000) actor.removeSelf() return True
nbPlayers = 4 if isDebug: nbStartCards = 3 else: nbStartCards = 5 handWidth = 300 trumpActor = None trump = None hands = None talon = None thinkingTime = 1200 # ms pile = Hand(deck) isPartnerMoves = False okBtn = GGButton("sprites/done30.gif", True) toolBarTextActor = Actor("sprites/text_item.png") spadeItem = ToolBarItem("sprites/spades_item.png", 2) heartItem = ToolBarItem("sprites/hearts_item.png", 2) diamondItem = ToolBarItem("sprites/diamonds_item.png", 2) clubItem = ToolBarItem("sprites/clubs_item.png", 2) Item = ToolBarItem("sprites/clubs_item.png", 2) cg = CardGame(600, 600, 30) cg.setTitle("Mau-Mau (V" + version + ") constructed with JGameGrid (www.aplu.ch)") cg.setStatusText("Initializing...") initHands() cg.addActor(okBtn, hideLocation) okBtn.addButtonListener(MyButtonListener()) toolBarTextActor.hide() cg.addActor(toolBarTextActor, toolBarTextLocation)
def __init__(self): Actor.__init__(self, "sprites/hamster.gif")
def __init__(self, bulb): Actor.__init__(self, "sprites/switch.gif", 2) self.bulb = bulb
def __init__(self): Actor.__init__(self, "sprites/bear.gif")
def __init__(self): nbSprites = 18 Actor.__init__(self, "sprites/earth.gif", nbSprites) self.setSlowDown(6)
def __init__(self): Actor.__init__(self, "sprites/rocket.gif")
def __init__(self): Actor.__init__(self, "sprites/nemo.gif")
def __init__(self): Actor.__init__(self, "sprites/boat.gif")
def __init__(self, sprite, clazz): Actor.__init__(self, sprite) self.clazz = clazz
def __init__(self): Actor.__init__(self, "sprites/ghost.gif", 4)
def __init__(self): Actor.__init__(self, "sprites/leaf.gif")
#GameGridEx2.py from ch.aplu.jgamegrid import GameGrid,\ Actor, Location # ---------------- class MyGame ------------------- class MyGame(GameGrid): def __init__(self): GameGrid.__init__(self, 10, 10, 60, GameGrid.RED, "sprites/reef.gif") self.addActor(nemo, Location(2, 4)) self.show() def act(self): nemo.move() if not nemo.isMoveValid(): nemo.turn(180) nemo.setHorzMirror(not nemo.isHorzMirror()) # ---------------- main -------------------------- nemo = Actor("sprites/nemo.gif") MyGame()
# --------------------- 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 gg.playSound(GGSound.PING) # Play ping dart.setLocation(dart.getLocationStart()) # Init dart
def __init__(self): Actor.__init__(self, "sprites/honey.gif") self.isDragging = False
def __init__(self): Actor.__init__(self, True, "sprites/dart.gif") # Rotatable self.oldLocation = Location()
def __init__(self, isRotatable, imagePath, nbSprites): Actor.__init__(self, isRotatable, imagePath, nbSprites)
def __init__(self): Actor.__init__(self, "sprites/angelfish.gif")
def __init__(self, type): Actor.__init__(self, "sprites/bulb" + str(type) + ".gif", 2)
def __init__(self): Actor.__init__(self, "sprites/hazelnut.gif")