Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Pygame: Uploading Game Options

A topic by puzzlefrog created Jul 27, 2024 Views: 280 Replies: 10
Viewing posts 1 to 3

I have been working on a game for a game jam. It has to be a game that runs in the browser on Itch.io. I am using Pygame to make it. Once it is made, what options do I have for uploading it?


 I want to know as many as possible. That way, in the event that one of them doesn't work, I will have a backup plan. So far I know of only two ways to export games made in Pygame. The first way I heard about was PyInstaller, but that doesn't work for me most of the time and when it does Windows Defender just flags it as a virus. Also that creates an executable which the game jam runners do not want. The second is Pygbag which can convert the games I make on my computer into web assembly browser games. One problem with this is that if I have to mess around with the asyncio module which I find to be a pain to deal with as I only recently found out that asynchronous code is a thing. I haven't really needed it much up to this point. As a result, I am not good at working with the asyncio module yet. 


One of my teammates recently introduced me to Github. I have a copy of my code stored there. Maybe there is something I can do with that. If not, there are probably a lot of other ways to get my game uploaded.

There are several python to java compilers on the python wiki, but none of them work with pygame

https://wiki.python.org/moin/WebBrowserProgramming

For pygame, the only alternative is pygbag.

You don't really need to think too much about async, in the pygbag documentation itself they give you a minimal example of how to use it.

Simply put your game's main loop inside the main function that you call with async and there's not much more to complicate things.

Github has nothing to do with compiling your code, so there's nothing you can do there, unless the JAM asks you to share the source code.

Pyinstaller or Nuitka are useful for creating binary executables, but neither of those options are useful for the web.

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

Try Nuitka, it's supposed to have Pygame support now. But it makes native executables, not for the web.

Does it have the same bug that Pyinstaller does where things get flagged as viruses even when they aren't viruses?

Moderator(+1)

That's not a bug in PyInstaller, it's malware scanners being dumb. You'll have the same problem with Nuitka if you use the --one-file mode. But Nuitka also has a --standalone mode, where it just places the executable and DLLs in a folder. That shouldn't cause problems.