Category Archives: Python

Programming in Python (usually Python 2.6/2.7)

I wrote a Hello World program.

Evolution - it works. Given enough time.

Genetic algorithms have got to be one of the crazier ideas in computer science – take an initial population and cross-breed/mutate to create the next generation. It doesn’t always find the optimal solution, but it sure tries hard and is fun to watch!

Python 3 code here.


Too lazy to click links

Quite a lot of files to download and not enough patience in me

Quite a lot of files to download and not enough patience in me

It’s the end of the university semester, and that means downloading-every-available-resource time. For archiving reasons.

Unfortunately, there are a lot of files to be downloaded. For instance, one subject might have  12 weeks of content, each week having a tutorial sheet, a tutorial solution, a practice class sheet and a practice class solution. That’s 48 links already, and that doesn’t even include past exams and other resources yet!

So I started to think, maybe I could use Python and Requests to nab the files I need. First I’d specify a URL, an output folder and the types of files to be downloaded (pdf, tex, etc). Then all I would have to do is run the program and voila! Everything downloaded in a second!

Well, that only worked for my first subject.

Unfortunately, other subjects needed a bit more work to get the files – you had to first log in with your university account. I saw this coming, so I was surprised that my first attempt even worked (maybe the lecturer just couldn’t be bothered).

I didn’t end up figuring my way around this. Requests has a way of submitting forms by using, data), but unfortunately my university used some sort of authentication I couldn’t work out. Other subjects I didn’t even attempt, since they were on a completely different system which also required logging into.

One thing I did learn from this, however, is that you can download files with Requests quite easily. Fetch the file with r = requests.get(url), and the binary data can be found in r.content. Then all you have to do is write to a file using open(filename, “wb”).

What this means is that when I wrote my xkcd comic downloader, I didn’t actually have to use the Python Imaging Library. I could have just written to a file directly using the “wb” mode – and in fact this would also resolve my problem of downloaded GIFs being static

An overview of past projects

High school quality graphics.

Over time you learn new things, and find new ways to apply what you know. Or maybe there’s something you don’t yet know but want to learn, so you decide to look it up and play around with it.

That, in a nutshell, is how my post-primary school life went. I’d (infrequently) embark on new experiments, generally digital ones, and make something small over the course of a few days. Rarely did I ever create a large project which gave off a sense of completion, but nonetheless even the small projects were nice learning experiences.

Here I thought I’d share… quite a lot of these projects. Maybe you’ll find my journey amusing, and consider this a nice read. Or maybe I can somehow motivate you to try things you’ve never done before. Either way, enjoy.

(This is quite a long post, so feel free to skim the page and read the parts that look interesting. My heartfelt thanks goes to you if you end up reading it all though.)

Continue reading

The Puyo Puyo Puro-gram

Why anybody would put a match-n game like this under the “Puzzle” category confuses me

About a month or so ago, Sega came out with “Puyo Puyo Quest”, a smartphone “Puzzle RPG” from the Puyo Puyo franchise. It has become my latest public transport timekiller.

On the bottom half of the screen, you clear away up to 5 puyos with a swipe (linked horizontally, vertically or diagonally). Gravity influences the remaining puyos, and any blocks of four or more puyos of the same colour (linked horizontally, vertically but not diagonally) are removed, and add to your characters’ attack. Gravity continues to take effect and more puyos fall to fill in the gaps. Repeat.

After playing this for a while, I’ve gotten to the stage where the bosses of each quest have an abnormally high HP (I wonder whether they’re just trying to make you fail in order to pay for “magic stones” that let you continue). Since it’s getting hard to pass normally, I thought I’d try to write a program for this problem.

Some input and output corresponding to the screenshot above

Some input and output corresponding to the screenshot above

Basically the program reads in the grid from a text file. It then goes through all possibilities of removing puyos, and simulates the outcome. The program reports back the move which gives the highest “score”, as well as the move with the largest combo.

You may notice that the input file on the left has a few extra lines before the grid. This is because clearing a certain colour only adds attack to characters of that colour. Since I had no purple or blue characters, linking four or more blue/purple puyos together would do nothing. The extra lines of input are meant to represent the attacking power of my party corresponding to each colour, and the “score” of each move is then calculated based on this, along with other factors such as how many puyos of each colour were removed.

There were also a few game-related quirks to be taken into consideration. The heart piece, for example, disappears when an adjacent group of puyos disappear (the heart heals characters). In addition, the row of small coloured circles above the board indicate the next colour to fall in that column. These weren’t particularly hard to deal with, but it did take some thinking.

Currently my program is not very efficient – you’re allowed to remove up to 5 puyos at once, but my program is only fast enough to calculate removing 3 in a reasonable time. On the bright side, that’s good enough for most practical purposes. Although, despite saying that, having to type out the game board into the text file each time is pretty painful.

The particle simulator

Pretty colours!

Pretty colours!

One day a friend of mine made a sort of battle royale – you had things bouncing around and when two collided they would “battle” each other. One person, determined partly by the amount of EXP they had, would turn out victorious and continue bouncing around merrily until only one remained. The “game” was somewhat fun to watch – and the fact that the names of people he knew were included made it really interesting. After all, who wouldn’t want to see their own character win?

So I asked him what he used to make all this stuff and I learnt from him that he uses Pygame for his graphical Pythoning purposes. And today I decided to try it out myself.

This cheat sheet from the internet sure came in handy – it allowed me to do what I wanted without having to go through pages of documentation. So Pygame was easy to set up. I was off to a great start.

What did I wish to make? Something equally fun and mesmerising to gaze at, of course. I decided on a sort of particle simulator, where circles would whiz around, affected by only gravity (implemented straight from the formula). It worked beautifully for the first few seconds. But then two particles collided with each other, and flew off at a funny angle. It turns out that, if the distance between two objects is small, the force due to gravity becomes extremely large. And since my particles were allowed to overlap one another, they could theoretically even have a distance of zero!

So that got fixed with an absorbing mechanism, just like the Katamari games. So now, just like the battle royale, one giant blob would remain victorious at the end, having swallowed up all the others. But of course, that would be pretty sad and boring to watch so I let you add more particles by clicking anywhere in the window.

Naaa~ na na na na-na na na na katamari damacy!

Naaa~ na na na na-na na na na katamari damacy!

The particles would sometimes go offscreen too, so I put in an auto-zooming camera option. That tended to make the particles really small (to compensate for the zooming) though.

So how long did that all take? 3 hours! Woo!