Tuesday 8 December 2015

Pygame: Draw simple animations

Move rectangle horizontally
#import pygame
import pygame

#initialize game engine
pygame.init()

rect_x=0
rect_y=50
rect_width=50
rect_height=50

window_width=500
window_height=500

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()

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

    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, [rect_x,rect_y,rect_width,rect_height])
    rect_x+=animation_increment
    pygame.display.flip()
    clock.tick(clock_tick_rate)

    if(rect_x==window_width-rect_width):
        rect_x=0


Move more than one rectangle horizontally
#import pygame
import pygame

#initialize game engine
pygame.init()

rect_x=0

rect1_y=30
rect2_y=130
rect3_y=230
rect4_y=330
rect5_y=430

rect_width=50
rect_height=50

window_width=500
window_height=500

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()

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

    screen.fill(BLACK)

    pygame.draw.rect(screen, WHITE, [rect_x,rect1_y,rect_width,rect_height])
    pygame.draw.rect(screen, WHITE, [rect_x,rect2_y,rect_width,rect_height])
    pygame.draw.rect(screen, WHITE, [rect_x,rect3_y,rect_width,rect_height])
    pygame.draw.rect(screen, WHITE, [rect_x,rect4_y,rect_width,rect_height])
    pygame.draw.rect(screen, WHITE, [rect_x,rect5_y,rect_width,rect_height])

    rect_x+=animation_increment

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

    if(rect_x==window_width-rect_width):
        rect_x=0


Move rectangles horizontally and vertically

#import pygame
import pygame

#initialize game engine
pygame.init()

rect_x=0
rect_y=0

rect1_y=30
rect2_y=130
rect3_y=230
rect4_y=330
rect5_y=430

rect1_x=30
rect2_x=130
rect3_x=230
rect4_x=330
rect5_x=430

rect_width=50
rect_height=50

window_width=500
window_height=500

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()

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

    screen.fill(BLACK)

    pygame.draw.rect(screen, RED, [rect_x,rect1_y,rect_width,rect_height])
    pygame.draw.rect(screen, GREEN, [rect_x,rect2_y,rect_width,rect_height])
    pygame.draw.rect(screen, BLUE, [rect_x,rect3_y,rect_width,rect_height])
    pygame.draw.rect(screen, WHITE, [rect_x,rect4_y,rect_width,rect_height])
    pygame.draw.rect(screen, RED, [rect_x,rect5_y,rect_width,rect_height])

    pygame.draw.rect(screen, GREEN, [rect1_x,rect_y,rect_width,rect_height])
    pygame.draw.rect(screen, BLUE, [rect2_x,rect_y,rect_width,rect_height])
    pygame.draw.rect(screen, WHITE, [rect3_x,rect_y,rect_width,rect_height])
    pygame.draw.rect(screen, RED, [rect4_x,rect_y,rect_width,rect_height])
    pygame.draw.rect(screen, GREEN, [rect5_x,rect_y,rect_width,rect_height])

    rect_x+=animation_increment
    rect_y+=animation_increment

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

    if(rect_x==window_width-rect_width):
        rect_x=0

    if(rect_y==window_height-rect_height):
        rect_y=0









Previous                                                 Next                                                 Home

No comments:

Post a Comment