In one of my previous posts on Python, I wrote about how I like to program games when learning a new language. In my initial post, I have written “tic-tac-toe” with some AI. In my second post, I went for a very ambitious AI on the famous “Connect 4” game. In this third post, I will release a “minesweeper” game for iOS written using the “Pythonista” App. The picture below shows the game on my iPad mini:
A rainy day
Last week I was on holiday, and since it rained almost the entire week (I stayed in the Netherlands) I had plenty of time to sharpen my Python skills. The house we were staying in had a good Wi-Fi Internet connection and although I did not bring my laptop, I did bring my iPad.
Normally I use the iPad for reading websites on the couch while listening to some streaming music. I also like to play small games or type drafts for blog posts (like this one) on it. For the latter, I use the (free) WordPress iOS App. But now I had to do some Python Minesweeper coding.
I expected having to connect to one of my Linux boxes using a terminal application and then coding remotely using a text mode editor, like Vim. But when I explored the possibilities I ran into the Pythonista App. It is a full featured run-time and IDE for Python on iOS. It allows to both program (type) and execute the code on the iPad.
Although it is a paid app, I liked the idea of coding Python on the iPad so much that I decided to buy it. Pythonista comes with some sample applications that will get you up to speed in no time. Pythonista also has a fairly good help system so that you can get context sensitive help on Python functions.
After many hours of typing, Googling, and thinking, I came to the point that the code was ready. I ran into a few nice Python best practices: generators, enums, namedtuple and genuinly had a good time. I especially liked the “yield” (generator) construct a lot and as was happy to find yield is included in PHP (since version 5.5).
Windows XP graphics
Minesweeper X is a nice minesweeper clone written by Curtis Bright. On the site you find a set of skins for his game. Instead of inventing my own skin system, I decided to implement his. To load the skin I converted the file from BMP to PNG using Gimp. After that I converted the file to a base64 string to be able to paste it (with minimal length) in the code. For the game, I chose the familiar “Windows XP” skin, shown below:
Pythonista is not free
There also is a very innovative app called “ScriptKit” (11 eur/12 usd), that allows for touch-enabled “drag-and-drop” programming in a Lua-like language.
Download the source code
Downloading the source code is as simple as following the link below and copy-pasting the code into your Pythonista app:
If you want to dive deeper into Pythonista, you may want to check out the following links:
- Pythonista video on YouTube
- Pythonista review by Ole Begemann
- Pythonista 101: The Scripting Community
- Code on iPad
- On iPad coding apps
Python games on GitHub
With this last post I have finished the three games I normally write when learning a new language:
I have published all three games on my GitHub account and discussed each of them in a blog. I hope it inspired you to pick up Python as well.