Aegidius
 Plüss   Aplulogo
 
in examples
Use the asterisk (*) character for wildcard searches
Searches on more than one word will be treated "as a phrase"
 
serverstatus
 www.aplu.ch
 www.java-online.eu
     Print Text 
© 2018, V10.0
 
  Jython
 
 

 

Designing and constructing an own computer game is a very motivating task for most people. Inspired by the vast number of high quality products the game developer is challenged by the complexity of the code for the most trivial game. Without a sophisticated graphics game library, most the job to program even a simple game is just too hard to accomplish in the class room. The JGameGrid package was developed to facilitate game programming dramatically. It's mainly used in the field of computer education. The class GameGrid provides a game window where all game actions take place. Visual game actors move in this window and interact according to the game design. They are normally instances of a class derived from Actor and "added" to the GameGrid window. There is an internal animation machine that calls the actor's act() method repeatedly. Normally this is the place where we put the code that animates the actor. We can also register a callback event notification for each act-cycle. (For more details, consult the tutorial on the JGameGrid homepage.)

Game development with JGameGrid is heavily based on object-oriented programming (OOP), because game actors must be instances of the Actor class. The design of the game window is defined by parameters in the GameGrid constructor. If the gamegrid module is imported and after calling makeGameGrid(), all methods of the class GameGrid are available as global functions.

In the first example the makeGameGrid function defines a square game grid with 10x10 cells, each 60 pixels wide, a background image reef.gif (that must reside in the sprites subdirectory of TigerJython's "home" directory (where tigerjython.jar resides) and red grid lines. The class Fish is derived from Actor and the sprite image nemo.gif (that must also reside sprites subdirectory) is passed to the constructor. (Many sprite images are included in the sprites subdirectory of TigerJyhton's JAR archive and are loaded from there.)

The behavior of a Fish instance is determined by the act() method. In this simple example the fish just moves forth and back. Provided that you have a basic idea of OOP and you know how to implement classes in Python the code is straightforward.

# GameGridEx1.py
from gamegrid import *                         

# ---------------- class Fish ----------------
class Fish(Actor):
   def __init__(self):
      Actor.__init__(self, "sprites/nemo.gif")
   
   def act(self):
      self.move()
      if not self.isMoveValid():
         self.turn(180)
 
# ---------------- main ----------------------
makeGameGrid(10, 10, 60, makeColor("red"), 
    "sprites/reef.gif")
nemo = Fish()
addActor(nemo, Location(2, 4))
show()
GGEx1

Select GameGridEx1.py (Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.

Discussion: The backslash is used as line continuation symbol. You must use the navigation buttons to run the animation. Add the line

self.setHorzMirror(not self.isHorzMirror())

to make nemo always swim headfirst.

It may be preferable to create a application class derived from GameGrid in order to have an easy access to all (non-private) methods and be able to overwrite some of its callback methods like act() that is called in every simulation cycle. The following example with the same functionalities as before shows how to proceed.

#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()
GGEx2
Select GameGridEx2.py (Ctrl+C to copy, Ctrl+V to paste)

Discussion: Instead of using a global variable, nemo could be declared as instance variable of the class MyGame by adding

self.nemo = Actor("nemo.gif")

to the MyGame constructor. In this case every reference to nemo in the class MyGame must be prefixed by self. which makes the code somewhat overburden.

The next example is our showpiece of OOP. Because every instance of the class Fish is a fully featured individual who "knows" how to move, it is fun to create as many fishes as you want by clicking with the mouse in a cell. The class Fish is the same as in GameGridEx1, only a mouse event handler mousePressed() is added where the fishes are created and added to the game grid.

# GameGridEx3.py
from gamegrid import *                         

# ---------------- class Fish ----------------
class Fish(Actor):
   def __init__(self):
      Actor.__init__(self, "sprites/nemo.gif");
    
   def act(self):
      self.move()
      if not self.isMoveValid():
         self.turn(180)
         self.setHorzMirror(not self.isHorzMirror())
         
# ---------------- main ----------------------
def onMousePressed(mouse):
   loc = toLocationInGrid(mouse.getX(), mouse.getY())
   addActorNoRefresh(Fish(), loc)
makeGameGrid(10, 10, 60, makeColor("red"), 
   "sprites/reef.gif", False, 
    mousePressed = onMousePressed)
show()
doRun()
GGEx3
Select GameGridEx3.py (Ctrl+C to copy, Ctrl+V to paste)
Execute the program locally using WebStart.

Discussion: In Jython it is very simple to register an event handler (callback function) by using a named parameter in the class constructor (or here in makeGameGrdi()). The additional parameter value False removes the navigation area and we start the simulation cycling programmatically by calling doRun().

It is rather straightforward to port the many examples from the JGameGrid Java tutorial to Jython. Use this link to download the Jython versions.

If you are interested in card games, consult the CardGame site. Use this link to download the Jython versions of the CardGame tutorial.

MauMau

Execute the program locally using WebStart.