How to integrate pygame and Flask?

Viewed 1450

I am working on a simple pygame....game.
It's a multiplayer tank battle game. One player can play from WASD control and the other from Arrow keys.
Lately I was thinking of improving the way I take user input.

I thought that I can probably make a Flask app that renders pages with two joysticks on it. Each player can open the webpage from their phone's browser. I will take their inputs from the joysticks and use them for the player movement.

Unfortunately, I am unable to figure out how I can run both of them at the same time. When I run the app with flask run it shows some text that looks like it is from flask but doesn't start the server.On the other hand, if I run the app with python3 pratice.py it shows no text whatsoever from flask. Only runs pygame.Output

Here is a minimum reproducible example:

from flask import Flask
import pygame, sys
pygame.init()

screen = pygame.display.set_mode((500,500))
while True:
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'
    
if __name__ == '__main__':
    app.run()

I searched and was unable to find any examples where people tried to integrate pygame with flask.

Is flask a good choice for this particular task? If yes, where am I going wrong?

0 Answers
Related