animation code test

To animate some tiled image like this one:
tiles.png

pygame-ce module init:

# /// script # dependencies = [ # "pygame-ce", # ] # /// import asyncio import pygame async def main(): pygame.display.set_caption("simple animation") screen = pygame.display.set_mode((1024, 600)) anim_tiles = pygame.image.load("tiles.png") anim_frames = 2 anim_current = 0 # display same image, frame count anim_refresh = 6 # persistence frame counter anim_pass = 0 anim_pos = 1000

The animation loop which must yield regularly on each frame (with await asyncio.sleep) so host can process I/O and drawings:

while True: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: return # clear whole screen screen.fill( (0,0,255) ) # draw a frame from animation screen.blit(anim_tiles , (anim_pos, 300), pygame.Rect( anim_current * 128, 0, 128,128) ) anim_pass = anim_pass +1 if anim_pass>anim_refresh: anim_pass = 0 anim_current = anim_current+1 if anim_current>=anim_frames: anim_current = 0 anim_pos = anim_pos -1 if anim_pos<0: anim_pos = 1000 pygame.display.update() await asyncio.sleep(0.016)

run the program:

asyncio.run(main())

Bye

run url: https://pygame-web.github.io/showroom/pythongit.html?-i#src/test_hackmd.py https://hackmd.io/zZTCp8XETLiX0QA30bbPUQ

Select a repo