Search This Blog

Saturday, May 3, 2014

Python - Cat animation using Pygame

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((400,300))
FPS = 30
fpsClock = pygame.time.Clock()

pygame.display.set_caption('Animation')

BLACK = (0,0,0)
WHITE = (255,255,255)
RED   = (255,0,0)
BLUE  = ( 0, 0, 255)

catImg = pygame.image.load('cat.jpg')
catx = 10
caty = 10
direction = 'right'
                           

while True:
    screen.fill(WHITE)
    if direction == 'right':
        catx += 5
        if catx == 360 :
            direction = 'down'
    elif direction == 'down':
        caty += 5
        if caty == 260:
            direction ='left'
    elif direction == 'left':
        catx -= 5
        if catx == 10:
            direction = 'up'
    elif direction == 'up':
        caty -= 5
        if caty == 10:
             direction = 'right'
    screen.blit(catImg, (catx,caty))
    
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    fpsClock.tick(FPS)



No comments:

Post a Comment