Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

That's not helpful at all.


I do not know if I am knowledgeable enough about asynchronous code yet to even understand the documentation for pygbag, its worth a try. Can you link to the documentation? When I try to search for Pygbag documentation, I get a bunch of videos made by people not working on Pygbag talking about Pygbag. I want the actual documentation.

How are you searching? 

Write "pygbag" in google. The first link is the official page:
https://pypi.org/project/pygbag/

The second link, the wiki:
https://pygame-web.github.io/wiki/pygbag/

In both you have minimal example:

import asyncio
import pygame
pygame.init()
pygame.display.set_mode((320, 240))
clock = pygame.time.Clock()
async def main():
    count = 60
    while True:
        print(f"{count}: Hello from Pygame")
        pygame.display.update()
        await asyncio.sleep(0)  # You must include this statement in your main loop. Keep the argument at 0.
        if not count:
            pygame.quit()
            return
        
        count -= 1
        clock.tick(60)
asyncio.run(main())

You don't need to know anything else about async, this example already gives you all the use you need to make of async, which is only 3 lines.

Define the main function with

async def main():

call the main function with:

asyncio.run(main())

After updating the pygame canvas, you put:

await asyncio.sleep(0)

Stop, you don't need to know anything else about async for your game.

Why is there nothing on the same line as return? What is being returned? It just skips a line. Also, the  if not count part confuses me. count is initially set up at 60, that is not a Boolean. What keeps count from decrementing forever?

(4 edits)

When you use a "return" without writing any parameters, you are saying that you want to use it just like a brake, that is, its only purpose is to stop the execution of the code in that point (and exit from the function). This returns the value None in case you were wondering.

If you apply a boolean operator like IF or NOT to an INT, the rule is 0 == False and every other value is True
In other words, the Not count line will be True when count==0 and Not count will be False for every value of count other than 0

Example:

In [16]: not 1
Out[16]: False
In [17]: not 0
Out[17]: True

So the statements.

if not count:
            pygame.quit()
            return

When count==0 the "pygame.quit()" and the "return" statement is executed which ends the execution of the main function and finalize pygame.

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