How to synchronize sprite image in pygame

I am looking to create characters in a game using pygame and python version 2.7.6. I have a simple python script that allows me to create a window and print the image on the screen. This works successfully, however it is just a static image.

import pygame


class Game(object):
    def main(self, screen):

        #load first sprite to image for blit"
        image = pygame.image.load('sprites/sprite1.png')
        image2 = pygame.image.load('sprites/sprite2.png')
        image3 = pygame.image.load('sprites/sprite3.png')
        while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                return

        screen.fill((200, 200, 200))
        screen.blit(image, (320, 240))
        pygame.display.flip()

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    Game().main(screen)

I want to create a more dynamic picture by changing the first sprite with the second and so on, at least three of the sprite may be larger ( sprite1.png, sprite2.png, sprite3.png). How can I cycle images after a specified time interval, for example: 1 second 1/2 second ...

+4
source share
3 answers

@Puciek , ,

import time

for img in [image, image1, image2]:
    screen.fill((200, 200, 200))
    screen.blit(img, (320, 240))
    time.sleep(1)    # sleep 1 second before continuing
0

, (, ). , - , , . , , , . , python, SFML , python.

, , , . , , , , - , - :

def shake_it():
  while True:
    time.sleep(3)
    #do rotation here

, , . , - , , NPC . , ( , ).

, - . , , . , , , event_handler , , .

+3

-, . - . suburface(), blitting, "" , . : , , . 8 , FPS: - 12 ( 1/12 ), 1/3 fps = 4 fps ( 3/12 ) 1/12 fps = 1 fps (12/12 = 1 ). script, , , script. script:

enter image description here

Source:

import pygame, os
from pygame.locals import *

def Cycle(n, N) :           # n - current state, N - max number of states
    value = n+1 
    case1 = (value >= N) 
    case2 = (value < N)
    result = case1 * 0 + case2 * value
    return result

w = 600         # window size
h = 400
cdir = os.path.curdir       # current directory in terminal
states = 8      # number of sprite states

pygame.init()
DISP = pygame.display.set_mode((w, h))
BG = (36, 0, 36)        # background color
DISP.fill(BG)
band_1 = pygame.image.load(os.path.join(cdir, "band1.png")).convert()
K = 4           # global scale factor
band_1 = pygame.transform.scale(band_1, (K * band_1.get_width(), K * band_1.get_height()))
band_1.set_colorkey((0,0,0))
sw = K*8            # sprite width
sh = K*8            # sprite height
r0 = (100, 100, sw, sh)     # sprite 0 rectangle
r1 = (150, 100, sw, sh)     # sprite 1 rectangle
r2 = (200, 100, sw, sh)     # sprite 2 rectangle
s0 = 0          # initial state of sprite 0
s1 = 0          # initial state of sprite 1
s2 = 0          # initial state of sprite 2
t0 = 1          # main ticker
t1 = 1          # ticker 1
t2 = 1          # ticker 2
Loop_1 = True
FPS = 12
clock = pygame.time.Clock( ) 
while Loop_1 :
    clock.tick(FPS)
    DISP.fill(BG, r0)
    sprite_0 = band_1.subsurface((s0*sw, 0, sw, sh))
    DISP.blit(sprite_0, r0)
    s0 = Cycle(s0, states)
    if t1 == 3 :
        DISP.fill(BG, r1)
        sprite_1 = band_1.subsurface((s1*sw, 0, sw, sh))
        DISP.blit(sprite_1, r1)
        s1 = Cycle(s1, states)
        t1 = 0
    if t2 == 12 :
        DISP.fill(BG, r2)
        sprite_2 = band_1.subsurface((s2*sw, 0, sw, sh))
        DISP.blit(sprite_2, r2)
        s2 = Cycle(s2, states)
        t2 = 0
    for event in pygame.event.get( ):
        if event.type == QUIT : Loop_1 = False
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE : Loop_1 = False
#   t0 = t0 + 1 
    t1 = t1 + 1 
    t2 = t2 + 1 
    pygame.display.update()

pygame.quit( )
-3
source

All Articles