To animate some tiled image like this one:
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
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing