[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Help needed for pygame beginner



Hello Matthias,

On Sun, 6 Nov 2005 22:13:25 +0100
"Matthias F. Brandstetter" <haimat@xxxxxxx> wrote:

> Now I tried to write my own code, but it seems I am doing something
> wrong  (please see attached sample code). Result: I get the white
> window as  expected, but no black rect drawn onto this surface.
> 
> Any ideas what I am doing wrong here?
> Greetings and TIA, Matthias

Yes, I'd say, it was wrong indentation. If've fixed it and attached a
corrected version (it runs on my box properly, so I'd say it is ok now).

Have fun.

greets,
Marek
#!/usr/bin/env python
# -*- coding: latin-1 -*- 
"""
draw a barcode-like moving banner onto the screen
"""

#Import Modules
import pygame
import pygame.locals as pyl


def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""

    #Initialize Everything
    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption('barcodes')
    pygame.mouse.set_visible(0)

    #Create The Backgound
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((255, 255, 255))

    #Display The Background
    screen.blit(background, (0, 0))
    pygame.display.flip()

    #Prepare Game Objects
    clock = pygame.time.Clock()
    
    # constants defined outside the mainloop
    color = 0, 0, 0
    rect = 5, 5, 5, 5

    #Main Loop
    while True:
        clock.tick(60)

        #Handle Input Events
        for event in pygame.event.get():
            if event.type == pyl.QUIT:
                return

        #draw some barcodes onto the background
        pygame.draw.rect(background, color, rect)

        #Draw Everything
        screen.blit(background, (0, 0))
        pygame.display.flip()

    #Game Over

#this calls the 'main' function when this script is executed
if __name__ == '__main__': 
    main()