1

I have written a python3 implementation of connect4. It features an AI that will play against you. It currently runs using the module pygame, which allows you to draw certain objects on a window.

I'd like to have this game work on a HTML canvas with JavaScript for drawing. Something akin to p5.js.

I'd rather not write the whole thing in JavaScript, since the python file runs perfectly (and I don't want to run all the code client-side for security reasons). Python also offers better functionality for this.

Is there an easy way to do this?

7
  • You mean easier than writing code for it? Not sure what you are asking here. Do you want to know if you can run pygame code in the browser? Commented Sep 11, 2017 at 5:01
  • If your goal is to turn it into a game servable via the browser, you will need to rewrite it, you can't just draw to the canvas from python, also you'll need a web server if you imagined it being multi player Commented Sep 11, 2017 at 5:04
  • @jmercouris No, it's to be played against an AI. I'm fine rewriting it, but there are certain things that python does way better than JavaScript. The game is finished, I just need a way to pass the data to a HTML file and display it on the browser. Commented Sep 11, 2017 at 5:41
  • @BurhanKhalid No, I just want to display data from the python file in the browser. I don't think I need a full-blown web server for this, just a way to transfer data between a HTML or JavaScript file and a python file. Commented Sep 11, 2017 at 6:23
  • Perhaps by embedding the browser? Commented Sep 11, 2017 at 6:51

1 Answer 1

1

Stack Overflow really isn't designed for general "how do I do this" type questions. It's for more specific "I tried X, expected Y, but got Z instead" type question. But I'll try to help in a general sense.

Python can't run in a web browser. Web browsers can only run HTML and JavaScript. So if you want your program to work in a web browser, you have to use HTML and JavaScript.

You might be able to run your game on a server and then use something like a REST API to trigger the logic from the client, but honestly that's probably overkil. You're probably better off just rewriting your logic in JavaScript.

There are JavaScript implementation that take Python code and convert it into JavaScript commands. Googling "run python in web browser" returns a ton of results. In my experience, those are going to be pretty hit and miss though, and again, your best bet is to rewrite your game in JavaScript.

See also:

Please try to ask more specific questions in the fiture. If you're asking stuff like "How do I do that, and what are it's implications?" then that's a question for Google, not for Stack Overflow. Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

Got it! Thanks. I will work on asking better questions in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.