Please, I Have A Question before I get started

Terry Hancock hancock at
Tue Mar 14 03:34:43 CET 2006

On Mon, 13 Mar 2006 02:19:39 GMT
Skipper <mike at> wrote:
> Basically the program will blank the screen and call up
> (for example) 6 pictures.  A flashing border with travel
> from picture to picture. When the computer senses a mouse
> click it will clear the screen and present a second set of
> choices ... one level deeper than the first ... based on
> the chosen picture.

So the key difference from, say, a web page in a browser,
is that you want the choice to be determined by timing,
not by where the mouse is pointed?

This is actually quite non-standard, so there won't be
a lot of support for it in GUI-building tools.

However, it is the sort of thing that a game toolkit is
well-organized for. IMHO, PyGame is probably very close
to the fastest way to achieve this.  But you will have
to learn how to create a data model for your menus,

The main module can be dead simple, though. You just
need to paste in a little PyGame boilerplate at the
top (examples are included in the PyGame distribution),
then you'll have a loop that handles displaying a menu,
rotating through the choices, and listening for keyboard
input.  Something like this (I'm writing from memory --
you'll have to check the pygame documentation (which is
excellent, BTW) to find details):

import pygame
from pygame.locals import *

screen = pygame.display.set_mode((800,600))
	# There are smarter ways to do this, but for
	# a one-off project, just use your known screen size

# I'm assuming a tree structure of some
# kind with some obvious methods attached
# to each node

current_menu = root_menu
while 1:
            for choice in current_menu.choices:
                t0= pygame.time.get_ticks()
                t = t0
                while t<t0+5000.0:
                    t = pygame.time.get_ticks()
                    for event in pygame.event.get():
                        if event.type == KEYDOWN:
                            if event.key == K_q:
                                current_menu = choice.act()
                                raise StopIteration
        except StopIteration:

At least I think that's about right. The idea is, you 
dispatch all of your actions from the menu objects,
which you display at the top level of the loop. Then,
you loop through the possible choices, and listen for
a key stroke, if there is one, then you dispatch the
appropriate reaction, possibly returning something
other than the current menu (in which case, the
current menu changes).

You can also do this using timer events, but I think
that might confuse you for a project this simple. The
get_ticks approach ought to work.

And of course, you need some way to get out of the
program -- this code listens for the "Q" key to be
pressed to exit the program.

The menu objects must look something like this:

class Menu(object):
    def __init__(self, ...):
       # stuff defining the look of the menu, what
       # the choices are and pictures, etc.

       # one of the attrib is a list of callable
       # objects representing each action
       choices = []

    def display(self, ...):
       # code to paint the screen with this menu,
       # using PyGame code (load images, blit to
       # screen, etc)

    def highlight(self, choice):
       # this code makes the highlight effect for
       # a choice

You'll also need a choice object:

class Choice(object):
     def __init__(self, parent, target=None, ...):
       # parent is the menu this choice is in:
       self.parent = parent

       # target is the menu this action takes you to = target

     def __call__(self):
       # this will take whatever the required action is
       # For actions which don't change the menu (maybe
       # they play a sound, for example), just return the
       # parent, otherwise return the target so we can
       # go there:
       if not target:
           return self.parent

This is far from working code, but maybe it'll
give you an idea of how you might approach the problem.


Terry Hancock (hancock at
Anansi Spaceworks

More information about the Python-list mailing list