Making my first game (another Pong) in Python.

Thanks to the power of Python and the open-source library Pyxel I have managed to make my very first game, a Pong game. Pyxel is a library that helps to make retro-like games. The path was no bed of roses but the game is 100% playable, and you can play in your browser.

Ricardo Montero Rubert
2023-06-17

I started 2023 with a rather long list of things I wanted to achieve, and one of those things was to code a Tetris clone. Let me tell you right here, right now, that I have not succeeded at making a Tetris clone.

Why? Because the path of making your first game is a rather steep process, and if you are learning git and object-oriented programming whilst your background is RCS and structured programming learnt in a Fortran90 university course many years ago it reaches absolutely nonsense levels.

A screenshot of the game, with the debug mode activated.

Figure 1: A screenshot of the game, with the debug mode activated.

So I had to compromise, and as I still wanted to make a game, I settled on the most simple game ever done. The pong. Or so I thought. I have been able to make a fairly stable prototype, that has most of the features that I wanted to have. Check aside or at the end of the post to play.

Major hitting walls

Collisions

The first problem I encountered was collisions. We have been suffering from them since they were invented by Sir Isaac Newton back in 1687. I was expecting that in those 336 years, someone would have come up with something to deal with them, but that was not the case and I was delighted to build my own collision methods. Thank you, Newton.

So I rolled up my sleeves and also decided that my collision engine should distinguish on which side the collision has happened. Why? Because I wanted to make my life harder and be able to hit with the platform side for a speed bonus.

Three different approaches later and I have got a collision system that works okay at least. The side detection is a little bit flickery at times and if the ball moves too fast it skips the collision and crosses the platform, just like in the real world!1

The Menu

I wanted to have an elementary menu that allowed me to choose between what type of games, a few options, and display a scoring ranking which I have not been able to implement at all.

The menu, nobody would think that some letters in a black screen could take that much time

Figure 2: The menu, nobody would think that some letters in a black screen could take that much time

My initial approach was to hard-code everything into the game as a dictionary of dictionaries which eventually would lead to a function that was executed. That went chaotic quickly and the menu ended up being its own class which returns a number. The game now gets the number and with a switch-case alike structure does whatever is needed instead.

The menu is not quite polished, its main caveat is that it can’t show the selected options. For example, which difficulty is currently set in the game). This is a clear action to be solved in the future, especially if I want to reuse the menu code for any future game.

The Computer

Every human needs a computer to play against, so I had to code one. My first attempt was straightforward, to track the ball’s height position and match it with the computer’s platform. It works for low speeds but the computer is doomed as soon as the vertical speed goes over the speed is allowed to move the platform.

A harder opponent was required and that is how the moderate IA was born. It can predict the location where the ball will hit, and move the platform there. It has some gibberish introduced into their calculations which reduces as the ball is closer. It aims to hit the ball with the centre of the platform but the gibberish will likely avoid it. I think this level is too intelligent for its own good and if I ever work on it I would like to make it more dumb.

Finally, the hard mode tweaks the moderate IA for even finer improvement. First, the computer is not being away with fairies when the ball moves towards the player, as it now centres its platform in the middle of the screen, plus it will hit the ball precisely to send it the furthest possible to the human’s platform.

Final Conclusion

I had a lot of fun while doing this game, and although in the current version (0.13) there are quite a few things to improve like the menu not being able to show selected options, the moderate AI being an impressive adversary or the lack of ranking and scores.

The game has performed its objective, to make me familiar with the system so that I can eventually make that Tetris game I always wanted to do.

It might also entertain you for a while, you can play on your browser here, or download Exe the Windows executable here. As usual the Virus Total report can be seen here.


  1. Although in the real world, it typically makes a hole as a sub-product of the collision↩︎