Ejemplo n.º 1
0
# --------------------- 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();


Ejemplo n.º 2
0
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()
Ejemplo n.º 3
0
         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()

Ejemplo n.º 4
0
        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()
gg.doRun()
Ejemplo n.º 5
0
# 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()
Ejemplo n.º 6
0
        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
Ejemplo n.º 7
0
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()
Ejemplo n.º 8
0
            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()
Ejemplo n.º 9
0

# --------------------- 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()
Ejemplo n.º 10
0
      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()
Ejemplo n.º 11
0
# --------------------- 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()
Ejemplo n.º 12
0
            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()


Ejemplo n.º 13
0
]


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()
Ejemplo n.º 14
0
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()
Ejemplo n.º 15
0
                    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()