Thursday, December 27, 2012

PyGame Intro

Before we get into plotting the GPS information we need to have a way to do it. Python doesn't come with any inbuilt graphics but there are a number of additional libraries that provide the functionality.

After a bit of research it seemed PyGame is one of the more popular ones to use. After a quick read on their website and tried out a few of the tutorials it looks pretty easy to do what I need it to.

As the name suggests it aimed at games programming, so should easily be able to hand a little line drawing.

Have a look at the tutorials and the cheat sheet is really useful.

In it's simplest form to draw with PyGame you

1. Import the PyGame library
2. Initialise it.
3. Declare a surface. i.e. Something to draw on.
4. Draw on your surface
5. Tell PyGame to show what you have drawn.

You also need event handler which basically loops and processes events like key presses, mouse movements, instructions to quit.

So you just setup your event handle to wait for a quit event then close everything down gracefully.

A simple program to do this would be


import pygame, sys
from pygame.locals import *

# set up pygame
pygame.init()

# set up the window
windowSurface = pygame.display.set_mode((640, 480), 0, 32)

# set up the colors
BLUE = (0, 0, 255)

# draw a blue line onto the surface
pygame.draw.line(windowSurface, BLUE, (60, 60), (120, 60), 4)

# draw the window onto the screen
pygame.display.update()

# run the game loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()


Next post we'll put some of this into practice. We'll use some GPS Lat and Lon coordinates in a file and plot them on the screen.

No comments:

Post a Comment