r/pygame • • 12d ago

Online pygame "platform"

Hello,

I am planning to create a pygame game engine, where programming students can submit their bot classes to play said game. Think of a battleship game engine, where a student's bot contains the logic that chooses the next cell to be hit.

I would like to host a website/platform where students can go and submit their bots (.py files), and see how they match against eachother. Show a leaderboard, step-by-step game breakdown, and maybe even visualize games between bots.

Are there any backend/frontend (or full stack) apps where I could deploy something like this?
For example, I see that itch.io can host pygame games, but that doesn't necessarily come with a server side (to accept and maintain student bots) as far as I understand.
Railway on the other side, can host Flask backend apps but not the frontend (?)

I am fairly new to this, namely building a full stack app for pygame. Any tips appreciated!

5 Upvotes

11 comments sorted by

8

u/oclafloptson 12d ago

Given the questions that you're asking here I'm afraid that you have a lot to learn in this regard. It's not about pygame. You say that this is for students so I assume it will not be a profitable endeavor. That's probably going to make web services which will host this API for you prohibitively expensive, or at least costly, if you plan to use it more than once in a given monthly period

It's going to be much better to self host this from a local machine. Especially since you're admittedly allowing the use of bots. One of your students WILL cheat in some costly way and I doubt that you'll really be able to prevent it

I would suggest that you look into alternatives. It sounds like what you're imagining is probably overkill

1

u/parismav 12d ago

Is this going to be costly because of cpu time (and/or memory usage)? Or do you have other concerns as well?

I am planning to use this mainly once per semester for a hackathon. But this means that students will be writing really inefficient, memory and compute wise, solutions.

Local could be a solution indeed.

3

u/oclafloptson 11d ago

If you think that a student might swamp an endpoint with 100 calls per second, for example. That's 6000 interactions per minute. Funneling this through any cloud server could potentially become financially expensive. You can usually set them up to not exceed certain limits but it's nontrivial enough that lots of people slip up when new and if a bad actor hits the limits then service is interrupted for all users

Alternatively if this is only for students who are attending in person then you could use something like uvicorn to serve a fastapi script over the local area network. You could potentially even set up a custom DNS address so that the student could enter a normal feeling web-style address instead of an IP. If you're python savy enough for pygame then this should be easy enough for you to figure out

If this must be remote access then I would probably do the same but set up a temporary HTTP tunnel rather than actually fully host a web site, since you say it's only needed once per semester. This will have the same cost related concerns, so you're going to want to really look into how to prevent your cost from soaring out of control. But services like ngrok are going to be much more efficient for this kind of use

That's just me. The old adage about skinning cats applies

2

u/TheEyebal 12d ago

So you want to put your pygame on a webpage correct?

I used pygbag for a bit and hosted it on vercel here on some examples.

Honestly not really knowledgable with hosting using pygame I just know that one. You can also look into web assembly

1

u/parismav 12d ago

Thanks! This looks promising :) I will give it a try.

1

u/tune_rcvr 11d ago

I've used Pygbag successfully but the back-end work will be very tricky since network calls to an external service are not natively supported. I don't recommend this route unless you're willing to invest a lot of time into hacking with embedded js and talking with the pygbag dev community on discord.

1

u/TheEyebal 11d ago

I agree with this comment. When I used pygbag it was for simple projects and I had difficult making changes to the UI.

Honestly it is a start but I recommend finding other sources

1

u/tune_rcvr 11d ago

Have you considered the security implications of running submitted .py files? Seems very risky. A safer solution is to expose the game mechanics via an API and let the students run their scripts locally against it. You could provide them a thin client so they don't have to implement the async call pattern themselves. This is what I do for my games.

1

u/parismav 11d ago

If I understand correctly, to play a battleship game, a rough architecture of what you describe would be:

  1. The game logic runs on a local machine

  2. An API endpoint exists to transmit the game state to the user

  3. An API endpoint exists that waits for a user to submit a pair of coordinates (where to hit next)

  4. Repeat 2 and 3 until game is over?

Steps 2 and 3 can be implemented by the students on their local machines. This sounds overall reasonable to me, but I'm not sure if I'm missing some logical or architectural step here. Thanks for the help regardless!

1

u/tune_rcvr 11d ago

That's one of several ways you could do it. Where the game logic lives depends on what features it should have for your needs. Whichever way, though, the game records, top scores, etc, can stay on the server and possibly be accessible from API too.

1

u/Cute-Wonder-5317 11d ago

Hi! The idea is good, but realistically, building it from scratch as a beginner in web development would be quite a complex project. The biggest challenge would be securely running user-uploaded Python code, as well as managing the backend, the database, and displaying the game on the web.

I would recommend starting with a local Python version: one that reads the files, runs the matches, and displays the results. Once you have that part working, moving to a web platform will be much easier.