I created a collection of programs that use my Interphase module to design the interface panel. The programs, packaged together in Interphase Pack, includes Serpent Duel, Pod Descent, and Sliding Control Puzzle. These programs are written in Python programming language and Pygame multimedia module. The source of the programs are provided, and the code can be examined as an aid in understanding in utilizing the Interphase module specifically, or Python/Pygame programming in general.
The Interphase module was developed to add interface panel functionality to my programs, started initially as the interface for my Microbe program, and expanded with the aim to facilitate coding a simple GUI interface to any Pygame application. To those who may have need for interface panel functionality, I will briefly describe some of the workings of the Interphase module deriving the example from one of the applications in Interphase Pack, Serpent Duel.
The Interphase module was used to design an interface panel for Serpent Duel, which has controls for serpent movement and program configuration. I briefly describe some of the functionality of the Interphase module, more detailed information can be found in the Interphase module documentation.
To use the Interphase module, the program requires the following basic code:
import interphase
class Interface(interphase.Interface):
def __init__(self):
interphase.Interface.__init__(self, **parameters)
def add_controls(self):
self.add(**parameters)
def update(self):
interphase.Interface.update(self)
state = self.get_state()
The script does an import of the Interphase module, which imports interphase.py that should be in the same folder or on the system path. The interface functionality is embedded in the Interphase Interface Class Object (interphase.Interface), which inherits from pygame.sprite.Sprite class. The best means to use the Interphase module is to create a class object for the interface panel that is a subclass of interphase.Interface that has the overriding methods __init__(), add_controls(), and update().
Within __init__(), the superclass interphase.Interface.__init__() is called to initiate the interface panel, and takes many optional keyword parameters that describes the specifics of the panel, such as position and size. For example:
def __init__(self):
interphase.Interface.__init__(self,
position = (250,452),
color = (43,50,58),
size = (350,100),
image = 'panel.png',
button_image = ['button.png'],
control_image = ['control.png'] )
The add_controls() method is called upon interface initiation, and used to code the interface controls. Each control object is added with an add() method that has multiple keyword parameters that describe the specifics of the control. For example, the serpent directional controls are specified with:
def add_controls(self):
ctr = ('U',(175,25),'^'), ('D',(175,75),'v'),
('L',(150,50),'<'), ('R',(200,50),'>')
for ident, pos, ctrl in ctr:
self.add(
identity = ident,
control_type = 'control_toggle',
position = pos,
control_list = [ctrl],
label_display = False,
control_response = 0 )
In the interface subclass update() method, call the superclass interphase.Interface.update(). Interaction with the interface is done through numerous interface methods such as get_state(), get_control(), and get_value(). The get_state() method retrieves the InterfaceState object, which carries the state of the interface such as control pressed and control value. The attribute state.control specifies the identity of control currently pressed, and the following code shows testing using state object and linking to a program method that controls serpent movement:
def update(self):
interphase.Interface.update(self)
state = self.get_state()
if state.control:
if state.control in ('U','D','L','R'):
directions = {'U':(0,-1), 'D':(0,1), 'L':(-1,0), 'R':(1,0)}
serpent_control(directions[state.control])
An instance is created from the Interface Class and placed in a Pygame sprite group such as pygame.sprite.RenderUpdates, that provides methods update/draw/clear to update the panel.
Submitted by Jim on February 5, 2010 at 8:00 pm