General > Feedback

new way to develop with JPCT - Jython

(1/1)

MichaelJPCT:
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.

--- Code: ---@c:\jythonpath\jython.exe -Djava.library.path=jar_path -i mygame.py
@pause

--- End code ---

minimal example of a JPCT program, save as mygame.py

--- Code: ---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

fb=FrameBuffer(640,480,FrameBuffer.SAMPLINGMODE_NORMAL)
running=1
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

mainLoop()

--- End code ---
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.

EgonOlsen:
Do you want to add this the wiki? Maybe in the 'code snippets and other tips' section? If so, please let me know and I'll create an account for you.

MichaelJPCT:
ok. i have never done wiki before. :D

EgonOlsen:
You should have received your login data via email.

Navigation

[0] Message Index

Go to full version