Saturday, April 24, 2010

Python in Python.

Every python dev sooner or later found out about other python implementations. Python (as language) have them plenty: Jython, IronPython, PyPy, to name a few. They are all very important and adds to spirit of python community. To emphasize it Guido van Rossum have proposed suspension of changes to python syntax, semantic, and build-ins. So non c implementations can catch up with recent changes (mainly emerging py3k).

Jython or IronPython are great at combing python in java or .Net but the most exciting is PyPy the idea to write python in python. 


PyPy have many advantages over CPython:
  • Speed
  • Memory usage
  • JIT compiler
  • Sandboxing
  • 'Stackless' as option
  • .Net and Java backends
  • other languages backends (Prolog, Smalltalk, JavaScript, Io, Scheme, Gamboy)
PyPy can be really faster up to 28%.
PyPy implementation of classes and objects is economic and comparing to cpython apps that eat hundreds of MBs should consume less up to 50%.
PyPy JIT compiler is responsible for speed. It allow to translate python into java, prolog, it also help in memory management.
Stackless can use micro-threads.
And there are ready to use Java and .Net back ends Prolog is almost complete and others are developed.

PyPy 1.2 is compilant with CPython 2.5. You can already use some great third-party packages like Django, Pylons, Twisted or Pyglet.

To sum up PyPy is really worth trying, promising Python implementation.

Monday, April 19, 2010

Weekly Python module: atexit

New idea about few posts. What about smalltalk about one Python module in a week?

Lets go!



For this week Python standard module 'atexit'. Whole purpose of this module is to provide functions that will be executed at normal interpreter exit.

It has only one function register, that takes function and make sure that every function that was registered will be called in last in, first out order. If you know 'atexit()' C routine you will be familiar with it.

Small example:

def goodbye(name, adjective):
    print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)

import atexit
atexit.register(goodbye, 'Donny', 'nice')

# or:
atexit.register(goodbye, adjective='nice', name='Donny')

register function can also be used as decorator:

import atexit

@atexit.register
def goodbye():
    print "You are now leaving the Python sector."

Thursday, April 15, 2010

PyGame: basics

In this tutorial we will focus on Python's PyGame library that allow to create all staff needed to create all sort of games. But we will not learn Python itself. You must know Python. Classes and modules are obligatory also modules to load files but no other extra knowledge.

PyGame
PyGame can handle time, video (both images and vids), music, fonts, different image formats, cursors, mouse, keyboard, Joysticks and much much more. And all of that is **very** simple.

SDL
PyGame is extension of SDL an C library that was created to allow easy migration form one operating system to another (e.g. Windows to Linux). Its means that PyGame is easy multi-platform multimedia library.

Game loop
Before writing first lines of code I will mention about how game loop is designed in PyGame. Game loop is the place where events, game logic and rendering onto screen is performed. It should look similar to:


while 1:
events()
loop()
render()

Where event() proceeds events like pressed keys, mouse motion etc. loop() compute changes in the game world like NPC's moves, player moves, AI, game score. And render() just print out on the screen graphic. That separation of tasks makes your game design easier and allow to easy changes in code when new ideas rise in you head.

First code
Its time for more code:


import pygame
from pygame.locals import *

class App:
  def on_execute(self):
    pass

if __name__ == "__main__" :
  theApp = App()
  theApp.on_execute()


As you can see I prefer classes than functions. That is because OOP is getting more and more fans in game programming due to it correlation with game design. Simply in game we have objects, theirs tasks etc. even when programing without classes.

CApp will be our main class. To run game we will only need call OnExecute() function. Yes this function will contain our Game Loop.

Creating simple window
Lets add some more code:


import pygame
from pygame.locals import *
class App:
  def __init__(self):
    self._running = True
    self._surf_display = None
    self.size = self.weight, self.height = 640, 400
  
  def on_init(self):
    pygame.init()
    self._display_surf =       pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
    self._running = True
  def on_event(self, event):
    if event.type == pygame.QUIT:
    self._running = False
  def on_loop(self):
    pass
  def on_render(self):
    pass
  def on_cleanup(self):
    pygame.quit()

  def on_execute(self):
    if self.on_init() == False:
    self._running = False
    while( self._running ):
      for event in pygame.event.get():
        self.on_event(event)
      self.on_loop()
      self.on_render()
      self.on_cleanup()

if __name__ == "__main__" :
  theApp = App()
  theApp.on_execute()


Test
You should see black window. If window don't wont to quit use 'xkill' under linux or task manager under windows.

on_init on_event on_loop on_render on_cleanup
on_init calls pygame.init() that initialize all PyGame modules. Then it create main display - 640x400 window and try to use hardware acceleration. At the end this routine sets _running to True.
on_event check if Quit event happened if so sets _running to False wich will break game loop.
on_loop and OnRender do nothing.
on_cleanup call pygame.quit() that quits all PyGame modules. Anything else will be cleaned up by Python.

on_execute
on_execute initialize pygame than enter main loop in witch check events and then compute and render everything till _running is "True" and only Quit event will set it to "False".
Before quitting it will cleanup.

If you will run this file as program it will create instance of App and call its on_execute(). (3 last lines)

Note: PyGame can create your game's window in multiple modes:
  • pygame.FULLSCREEN a fullscreen display
  • pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
  • pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
  • pygame.OPENGL an opengl renderable display
  • pygame.RESIZABLE display window should be sizeable
  • pygame.NOFRAME display window will have no border or controls