Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags
(1 edit)

That makes sense. Thanks for the help.


Edit: I just tried it. This is not what I am looking for. I tried to modify it to suit my needs more, but had no luck. I wasn't specific in what I was asking for. That's on me. Anyway, do you have an example with a game window, where you click an x and that closes it? This just closes on its own. Also this leaves out a lot. Where do I put my global variables? Where do I call functions that do things like fill the game window, draw things, and play sounds? I would assume that is starting on the line after the code says while True:

I know how to do it without asyncio, but with asyncio, no idea. Should I show a screenshot?

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())