PyGame:pygame.time.Clock.tick()的异步版本

问题描述 投票:0回答:1

pygame 中没有异步 API

Clock.tick()
,我如何实现这样的东西? (这对于像
pygbag
这样需要异步主循环的事情很有用。)

python pygame pygame-ce
1个回答
0
投票

Pygame 并非设计用于与 asyncio 一起使用,因此请在 单独线程中运行 Pygame 代码,并使用 asyncio 与之交互。

import asyncio
import pygame
import threading
class PygameThread(threading.Thread):
    def __init__(self)
        super().__init__()
        self.clock = pygame.time.Clock()
    def run(self):
        pygame.init()
        screen = pygame.display.set_mode((800, 600))
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
            # Implement your game logic here, ain't doing your homework 😜.
            pygame.display.flip()
            self.clock.tick(60)
pygame_thread = PygameThread()
pygame_thread.start()
# Use asyncio here 🤯.
async def main():
    while True:
        print("doing async 😵.")
        await asyncio.sleep(1)
asyncio.run(main())
© www.soinside.com 2019 - 2024. All rights reserved.