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?

  • 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? – Burhan Khalid Sep 11 '17 at 05: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 – jmercouris Sep 11 '17 at 05: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. – Vishnu Nittoor Sep 11 '17 at 05: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. – Vishnu Nittoor Sep 11 '17 at 06:23
  • Perhaps by embedding the browser? – Arafangion Sep 11 '17 at 06:51
  • @Arafangion How do I do that, and what are it's implications? – Vishnu Nittoor Sep 11 '17 at 07:04
  • @VishnuNittoor: It's fairly complicated. Your best bet would probably be to study https://wiki.python.org/moin/WebBrowserProgramming (and then ask a more specific question) – Arafangion Sep 11 '17 at 23:59

1 Answers1

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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107