Answers for "pygame animation"

C
1

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()
Posted by: Guest on November-06-2020
0

pygame animation

import pygame ,sys
from pygame.constants import QUIT
from time import *
pygame.init()
screen = pygame.display.set_mode((200,200))
clock = pygame.time.Clock()
face = pygame.image.load("asset/smile.png")
#face change
face_smile = pygame.image.load("asset/smile.png")
face_haha = pygame.image.load("asset/haha.png")
face_say_a = pygame.image.load("asset/say_a.png")
face_say_c = pygame.image.load("asset/say_c.png")
face_say_d = pygame.image.load("asset/say_d.png")
face_say_b = pygame.image.load("asset/say_b.png")
face_list = [face_smile,face_haha,face_say_a,face_say_c,face_say_d,face_say_b]
face_index = 0
face = face_list[face_index]
#animation
face_animation = pygame.USEREVENT + 1

#face animation

#########################code#####################################################
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    
    pygame.display.update()
    screen.blit(face,(0,0))
    clock.tick(60)
Posted by: Guest on November-12-2021

Code answers related to "C"

Browse Popular Code Answers by Language