Search
 
SCRIPT & CODE EXAMPLE
 

C

Animated sprite from few images pygame

import pygame
import sys

def load_image(name):
    image = pygame.image.load(name)
    return image

class TestSprite(pygame.sprite.Sprite):
    def __init__(self):
        super(TestSprite, self).__init__()
        self.images = []
        self.images.append(load_image('image1.png'))
        self.images.append(load_image('image2.png'))
        # assuming both images are 64x64 pixels

        self.index = 0
        self.image = self.images[self.index]
        self.rect = pygame.Rect(5, 5, 64, 64)

    def update(self):
        '''This method iterates through the elements inside self.images and 
        displays the next one each tick. For a slower animation, you may want to 
        consider using a timer of some sort so it updates slower.'''
        self.index += 1
        if self.index >= len(self.images):
            self.index = 0
        self.image = self.images[self.index]

def main():
    pygame.init()
    screen = pygame.display.set_mode((250, 250))

    my_sprite = TestSprite()
    my_group = pygame.sprite.Group(my_sprite)

    while True:
        event = pygame.event.poll()
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)

        # Calling the 'my_group.update' function calls the 'update' function of all 
        # its member sprites. Calling the 'my_group.draw' function uses the 'image'
        # and 'rect' attributes of its member sprites to draw the sprite.
        my_group.update()
        my_group.draw(screen)
        pygame.display.flip()

if __name__ == '__main__':
    main()
Comment

PREVIOUS NEXT
Code Example
C :: wireless app debug android 
C :: pygame draw transparent rectangle 
C :: Donut-shaped C code 
C :: c random list 
C :: Which of the following are Cetaceans? 
C :: c program for threaded binary tree 
C :: same project on different monitor in intellij mac 
C :: how to prevent user from entering char when needing int in c 
C :: get chunks of a mp4 in ffmpeg 
C :: how to convert string to integer in c 
C :: print 2d array in c 
C :: postgres random select 
C :: how to read space separated words in c 
C :: 0/1 knapsack problem in c 
C :: concatenate char * c 
C :: c assign pointer to struct 
C :: to find greatest of 4 numbers in c 
C :: How to change an array in a function in c 
C :: Area of a Circle in C Programming 
C :: print short in c 
C :: make a function makefile 
C :: get float in c 
C :: bitwise and in c 
C :: simple bootstrap form example 
C :: pointer to function c 
C :: convert string to int c 
C :: function component with props 
C :: unpack and repack deb package 
C :: bcopy 
C :: enum case statement in c 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =