Tired of JavaScript – use browser Python

My experience developing the Snake game in Brython

image

“Wait what?” – I think most readers will react to the headline in that way.

In the sense of “just use Python in a browser”?

Everyone knows that only JavaScript works in browsers.

Well, the above is a screenshot with the source code of my personal site. Take a look, maybe you will see something new for yourself.

Yes, this is Python!

Now, let’s talk about how and how well this works, and also discuss a number of other JavaScript alternatives.

Introducing Brython

Brython Is a JavaScript3 implementation of Python3 that allows you to write Python code for the web.

Essentially, it is a JavaScript library that converts your Python code into equivalent JS and executes it in runtime.

Since writing browser code in Python sounds cool, I decided to give it a try.

Development of “Snake” on Brython

image

Here link to my sitewhere you can try Snake versions in JavaScript and Brython. But withGitHub source code link.

In order to try out Brython, I decided to write the classic Snake.

Since I’m not a Canvas HTML expert or game developer, I decided to use this JavaScript implementation as a starting point. Once I already created my “Snake” based on Canvas, but this implementation is more neat and compact.

And also author wrote it in less than 5 minutes. We must pay tribute to Chris Deleon, it is very impressive.

So, I added to Chris’ implementation scoring and saving the best result, as well as slightly improved the interface (added a pause button and a button with instructions). Then I ported the game to Brython.

I also modified his code so that it works in mode strict, since Chris’s implementation uses things like implicit global variables, which, in my opinion, do not reflect how most of the code in JS looks like (I do not criticize the author – he programmed for a while). I wanted to get a good comparison of Brython and JS code.

JavaScript turned out to be that way, and I will not post this code here, so our goal is to focus on Brython.

Although most of the Brython code was a “literal translation” with JS, some parts (for example, scoring functionality) were written directly in Brython and then implemented in JS to look at the differences.

The final result is as follows:





    
    
    Brython snake  



    

Snake built with Python!


Score: 0


High Score: 0

So, based on this snippet, let’s look at the basic concepts of Brython

Brython.js connection

No installation is required to use Brython. Just import the script inside head :

   

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *