import asyncio
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((1280, 768))
pygame.mouse.set_visible(False)
async def main():
#def main():
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
running = False
pygame.draw.rect(screen, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), pygame.Rect((random.randint(0, 1279), random.randint(0, 767)), (random.randint(0, 1279), random.randint(0, 767))))
pygame.display.flip()
await asyncio.sleep(0)
pygame.quit()
#main()
asyncio.run(main())
Note : j'ai laissé en commenté les appels “non asynchrones” qu'on aurait tendance à mettre pour un exécutable local. Mais en réalité le mode asynchrone fonctionnera aussi pour un programme interprété dans une fenêtre d'exécution.