Author Topic: new way to develop with JPCT - Jython  (Read 1100 times)

Offline MichaelJPCT

  • long
  • ***
  • Posts: 136
    • View Profile
new way to develop with JPCT - Jython
« on: December 30, 2015, 03:13:01 pm »
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: [Select]
@c:\jythonpath\jython.exe -Djava.library.path=jar_path -i mygame.py
@pause

minimal example of a JPCT program, save as mygame.py
Code: [Select]
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()
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.
« Last Edit: December 30, 2015, 03:21:39 pm by MichaelJPCT »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: new way to develop with JPCT - Jython
« Reply #1 on: December 30, 2015, 06:01:05 pm »
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.

Offline MichaelJPCT

  • long
  • ***
  • Posts: 136
    • View Profile
Re: new way to develop with JPCT - Jython
« Reply #2 on: December 30, 2015, 07:38:33 pm »
ok. i have never done wiki before. :D

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: new way to develop with JPCT - Jython
« Reply #3 on: January 01, 2016, 02:41:57 pm »
You should have received your login data via email.