Pages

marți, 12 octombrie 2010

PyGame : First interface - part 2

One thing necessary to create an interface and a game is using a "sprite system".
To illustrate this, I'll show you a sequence of source code:

import pygame
def must_quit():
    event = pygame.event.poll()
    return event.type == pygame.QUIT
screen = pygame.display.set_mode((640, 480))
SpriteImage = pygame.image.load('image.jpg')
while not must_quit():
    screen.blit(SpriteImage, (0, 0))
    pygame.display.flip()

It sounds simple but is not.
Why? Because when you work with multiple images when source code is more complicated.
For this we need a system and use the "classes".
Let's see:

class SpriteImage:
    def __init__(self, image_filename):
        self.image = pygame.image.load(image_filename)
    def paint(self):
        screen.blit(self.image, (0, 0))
screen = pygame.display.set_mode((640, 480))
sprite = SpriteImage('image.jpg')

This is a more simple way to use it.
Try learning more about pygame.
Good luck.