Skip to content

Instantly share code, notes, and snippets.

@linhns
Created April 8, 2025 08:48
Show Gist options
  • Save linhns/c65d50615b41a838dcc98cf107ece3a2 to your computer and use it in GitHub Desktop.
Save linhns/c65d50615b41a838dcc98cf107ece3a2 to your computer and use it in GitHub Desktop.
How to make an async game loop in Pygame
import asyncio
import pygame
class Game:
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode((640, 480))
self.clock = pygame.time.Clock()
self.running = True
def process_input(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
break
def render(self):
self.window.fill((200, 200, 200))
pygame.draw.rect(self.window, (0, 0, 255), (120, 120, 400, 240))
pygame.display.update()
async def run(self):
while self.running:
self.process_input()
self.render()
self.clock.tick(60)
await asyncio.sleep(0)
pygame.quit()
async def main():
game = Game()
await game.run()
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment