vineri, 27 februarie 2009

How I used time on pygame.

This is simple example. The program show some random colors.

import time
import random
import pygame

pygame.init()

def main():
    for i in range(60):
        screen = pygame.display.set_mode((800,600))
        pygame.display.set_caption("Some random colors!")

        bg = pygame.Surface(screen.get_size())
        bg = bg.convert()
        bg.fill((random.randint(1,255),random.randint(1,255),random.randint(1,255)))
        clock = pygame.time.Clock()
        Go = True
        while Go:
            clock.tick(2)
            screen.blit(bg,(0,0))
            pygame.display.flip()
            time.sleep(6)
            Go = False

if __name__ == "__main__":
    main()
As you see , try to create one loop:

        clock = pygame.time.Clock()
        Go = True
        while Go:
            clock.tick(2)
            ...  
            some code line 
            ...
            time.sleep(6)
            Go = False