Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

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