Thursday, 10 December 2015

Pygame: Display matrix movie screen

Following program display matrix.


# import pygame
import pygame
import random
import string

# initialize game engine
pygame.init()

# Window dimensions
window_width=1000
window_height=700

animation_increment=10
clock_tick_rate=20

# Open a window
size = (window_width, window_height)
screen = pygame.display.set_mode(size)

# Set title to the window
pygame.display.set_caption("Hello World")

dead=False

# Initialize values for color (RGB format)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
BLACK=(0,0,0)

clock = pygame.time.Clock()

class Number(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        self.image = pygame.Surface([15, 15])
        #self.image.fill(WHITE)

        font = pygame.font.SysFont('Calibri', 15, True, True)
        data=random.choice(string.ascii_letters + string.digits)

        if(data=='a' or data=='e' or data=='i' or data=='o' or data=='u' or
           data=='A' or data=='E' or data=='I' or data=='O' or data=='U'):
            text=font.render(data, True, [random.randint(0,255),random.randint(0,255),random.randint(0,255)])
        else:
            text=font.render(data, True, [58, 240, 48])
        self.image.blit(text, [0, 0])

        self.rect = self.image.get_rect()

numbers_list=pygame.sprite.Group()

for i in range(int(window_width/20)):
    myNumber=Number()
    numbers_list.add(myNumber)

    myNumber.rect.x=i*20
    myNumber.rect.y=10

screen.fill(BLACK)

print(len(numbers_list))
while(dead==False):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            dead = True

    #screen.fill(BLACK)

    numbers_list.draw(screen)

    for myNumber in numbers_list:
        myNumber.rect.y+=15

        if(myNumber.rect.y==window_height):
            numbers_list.remove(myNumber)

    for i in range(int(window_width/20)):
        myNumber=Number()
        numbers_list.add(myNumber)

        myNumber.rect.x=i*20
        myNumber.rect.y=10

    pygame.display.flip()
    clock.tick(clock_tick_rate)

Output





Previous                                                 Next                                                 Home

No comments:

Post a Comment