Hardware Reference
In-Depth Information
pygame.init() # initialise graphics interface
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
pygame.display.set_caption(“Test Window 1”)
screen = pygame.display.set_mode([788,250],0,32)
def main():
while True :
checkForEvent()
def terminate(): # close down the program
print (“Closing down please wait”)
pygame.quit()
sys.exit()
def checkForEvent(): # see if we need to quit or
# look at the mouse
#print “checking for quit”
event = pygame.event.poll()
if event.type == pygame.QUIT :
terminate()
elif event.type == pygame.KEYDOWN
and event.key == pygame.K_ESCAPE :
terminate()
if __name__ == '__main__':
main()
When you run this, you should get just a black window in the middle of the screen. It won't
do much, but it is a real window. You can drag it around the screen, and clicking the mini-
mise icon at the top-right corner will fold up the window and put it on the task bar at the
bottom of the screen. Clicking the close cross will quit the program as will pressing the Esc
key. When the program quits, you will get a message printed out in blue in the console win-
dow along with several lines of red debug information telling you where the program quit.
If you look at the anatomy of the program, you will see things are quite simple. The first few
lines tell pygame to create a window, of a certain size, with a certain title and put it in the
middle of the screen. The main part of the program is an infinite loop that constantly checks
to see if an event has occurred.
In programming terms, an event is something happening, which is normally how user inter-
action gets input to the program. You are looking for a close event or a key up event on the
Esc key. A close event is either the user clicking the close cross on the window or the operating
system telling the program to quit because it is going to shut down. If your program sees any
of those events, it calls a terminate function that prints out a message. Then it quits pyg-
ame to release any memory it grabbed, and it exits to the operating system.
Search WWH ::




Custom Search