[OLPC-Games] Embedding pygame in gtk.

C. Scott Ananian cscott at laptop.org
Mon Dec 24 17:50:07 EST 2007


I've been playing around with pygame in Pippy.  At the bottom please
find a minimal example that runs a pygame in full-screen mode from
Pippy, but when 'Keep as Activity Bundle' is used creates a proper
activity toolbar & etc.  Unfortunately, the embedding currently
doesn't pass mouse or keyboard events to the pygame.  I'm sure this
problem has been solved before, perhaps even in the OLPCGames package
which I just saw posted to this list.  Comments/advice welcome.
  --scott
--------------
# bounce: move some text around the screen
import sys
sys.path[0:0] = [ 'library' ]
import pippy, pygame
from pygame.locals import *
import random

def bounce():
    # always need to init first thing
    pygame.init()
    # XO screen is 1200x900
    size = width, height = 1200, 900
    # we'll use 36 pixel high text
    fsize = 36
    # create the window and keep track of the surface
    # for drawing into
    screen = pygame.display.set_mode(size)
    msg = "Hello!"
    font = pygame.font.Font(None, fsize)
    text = font.render(msg, True, (10,10,10))
    textRect = text.get_rect()

    while pippy.pygame.next_frame():
        for event in pygame.event.get():
            if event.type in [ QUIT, KEYDOWN ]:
                sys.exit()
        screen.fill((250,250,250))
        textRect.top = random.randint(0,height)
        textRect.left = random.randint(0,width)
        # draw the text
        screen.blit(text, textRect)
        # update the display
        pygame.display.flip()

if __name__=='__main__': bounce()

def fork_pygame(window_id):
    import os
    os.environ['SDL_VIDEODRIVER']='x11'
    os.environ['SDL_WINDOWID'] = str(window_id)
    if os.fork()==0:
        bounce()

from activity import ViewSourceActivity
from sugar.activity.activity import ActivityToolbox
import gtk
class PyGameActivity(ViewSourceActivity):
    def __init__(self, handle):
        super(PyGameActivity,self).__init__(handle)
        toolbox = ActivityToolbox(self)
        self.set_toolbox(toolbox)
        toolbox.show()
        socket = gtk.Socket()
        self.set_canvas(socket)
        socket.show()
        fork_pygame(socket.get_id())

def pippy_activity_class():
    return 'pippy_app.PyGameActivity'
--------------------------
-- 
                         ( http://cscott.net/ )


More information about the Games mailing list