Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

The above code is not an example of a game, it is just the code so you understand how to implement pygbag in your own code. The short answer is that the main loop of your game must be contained within a main function called asynchronously.

If you want it to close with the X, you must implement that code, if you already have a game programmed, it is done in the same way, since asyncio does not change anything about how you implement the rest of the code of your game.

I think the problem comes because you do not have enough experience in programming yet, especially with the scope of variables and functions.

import asyncio
import pygame
pygame.init()
screen = pygame.display.set_mode((320, 240))
clock = pygame.time.Clock()
async def main():
    count = 60
    mainloop = True
    while mainloop:
        #---------------------------------------------------------------------------------------             
        #---Events         
        #---------------------------------------------------------------------------------------             
        for event in pygame.event.get(): # User did something             
            if event.type == pygame.QUIT: # If user clicked close                 
                mainloop = False # Flag that we are done so we exit this loop
        
        #---------------------------------------------------------------------------------------
        #--- Game logic
        #---------------------------------------------------------------------------------------
        screen.fill(( 51, 61, 86))
        pygame.display.update()
        await asyncio.sleep(0)  # You must include this statement in your main loop. Keep the argument at 0.
        clock.tick(60)
asyncio.run(main())