Develop JPCT programs using Python language (Jython)

Revision as of 15:38, 1 January 2016 by MichaelJPCT (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

i am more familiar with Python than with Java and after writing some java code i still think Python code is much faster to write, although Python runs much slower than Java. and for a beginner, the oppotunity to type in functions or change states when the program is running, then see the effects immediately (this is so called "interactive mode", as oppose to modify code -> compile -> test run) is a great help.

Jython is Python which runs on Java Virtual Machine, and it is possibly compatible with any Java libraries, so a JPCT game could be written mostly in Python code, although some parts should be written in Java or even C language to keep a high running performance.

now let's see how a JPCT program can be written with Jython.

to start up the program, create a shortcut such as start.bat.

-i means interactive mode, pause to let yourself read any error messages.

   @c:\jythonpath\jython.exe -Djava.library.path=jar_path -i

minimal example of a JPCT program, save as

   import sys,time; sys.path+=['jar_path','jar_path/jpct.jar','jar_path/lwjgl.jar','jar_path/lwjgl_util.jar']
   from com.threed.jpct import *; from com.threed.jpct.util import *; from org.lwjgl.opengl import Display
   timeToSleep=0.01 # 0.01 second
   def sleepSomeTime(): time.sleep(timeToSleep) # or calculate a value to make framerate constant
   def GameLogic(): return
   def pollKeyboard(): return
   def mainLoop():
       global running
       while running:
           GameLogic(); fb.clear(); fb.update(); sleepSomeTime(); fb.display(); pollKeyboard()
           # if some key event: running=0
           if Display.isCloseRequested(): Display.destroy(); break

if "some key event" happens and interactive mode is used, mainLoop stops and you can type in commands, such as change timeToSleep to 0.02, then call mainLoop again after changing running back to 1. this is an example of the benefit of interactive mode.

to import your own java class, put class files in jar_path or append another path to sys.path, then import YourJavaClass

if you know both Python language and Java, you may find this approach has some advantages.