Pages

Se afișează postările cu eticheta binary. Afișați toate postările
Se afișează postările cu eticheta binary. Afișați toate postările

luni, 2 august 2010

PyGame : Binary bitmate with pygame.

Is a very simple example.
I use this code :

import pygame
from pygame.locals import *
import random
import Numeric
from math import *
import random
WIDTH = 640     #width of screen
HEIGHT = 480    #height of screen
def main():
    pygame.display.init()
    
    screen = pygame.display.set_mode((WIDTH,HEIGHT),DOUBLEBUF,32)
    pixels = pygame.surfarray.pixels3d(screen)
    
    width = len(pixels)-1
    height = len(pixels[0])-1
    
    for y in xrange(height):
        for x in xrange(width):
   a=random.choice([0,1])
   if a==1 : b=(255,255,255)
   else : b=(0,0,0)
   pixels[x,y] = (b)
    pygame.display.update()
    done = False
    while not done:
        for e in pygame.event.get():
            if e.type == KEYDOWN:
                done = True
if __name__ == "__main__":
    main()
The most important is this line : 
pixels = pygame.surfarray.pixels3d(screen)
I use the random function to select "0" or "1" and set the colors.
Next, I fill the screen by update the screen with new values of pixels : pygame.display.update()
See below the result :