Saturday, 29 November 2008

And that's why I love python!

Well for the last few weeks I've been hacking away with C++ whenever I get some free time which although interesting has been tedious, after all having to remember when you're passing pointers, references or values gets to be a pain real quick.

One of the really nice things with something like C++ though is memory management and speed when you get it right, you're working with natively compiled code and not having to wait for a garbage collector to clear up your allocations for you which makes for nice neat code. Of course like I said this is when you get it right, the downside is getting it wrong means memory leaks, stack overflows, variables pointing to garbage and so on which is troublesome to fix.

Today I got a little bored of all this so I figured it was time to put my Python hat back on and do some coding which was a little more interesting. So I took up one of the puzzles over at Project Euler (22 to be exact) which involves reading in a file of over 5000 names, sorting them alphabetically and then adding the numerical value of each name multiplies by it's position to an overall count and deducing what the total value of the names is. So where "COLIN" is at position 938 and it's numerical value is 53 (C=3, O=15 etc...) it's overall value is 49741.

I dread to think how to do this in C++ but in python it's a breeze thanks to it's list comprehension and easy file methods. So reading in the file is simply:

names_file = open("names.txt", "r")
names ='"', '').split(',')

And there we have the file being opened, the contents being read, having any quotes removed and then split by commas to get a list and finally the file being closed. Then to do something like figure out the numerical value of COLIN we do something like this:

name_value = sum([alphas.index[c] for c in "COLIN"])

so alphas is defining a string (with an intentional first space so that A is now at index 1), the alphas.index[c] part basically returns the index in the alphas string of a character and the "for c in "COLIN"" part is looping through each letter in the name. Surrounded by the square brackets means we are producing a list, so each iteration adds a new value to the list and then the sum() adds them all together. It's just to easy.

If anyone is interested, the finished code (not published here as I shall leave it as an exercise for the reader to complete) takes about 92 milliseconds to run against all 5163 names.

Sunday, 16 November 2008

Best laid plans and all that

So I mentioned previously the books I was looking at and I can inform you that I ended up with none of them! Whilst I was looking around we ended up going on holiday and I spent all of that lovely book money on wasteful stuff like having fun, what was I thinking. So to keep me going I started looking at a site run by a guy called Alex called Learn C++ which is not only informative but well written and I reckon is a good place to head over to whether you're an experienced programmer or complete novice.

Anyway in the end I picked up a book from Amazon as a reference by someone called Bjarne Stroustrup who happened to create the C++ language so I figured he must know what he's talking about. The book is called The C++ Programming Language: Third Edition, it was published a few years ago now but not a lot has changed really since. I haven't had a proper look through yet but when I do I'll let you know what it's like, currently I'm still working through the Learn C++ site.

The Python stuff has unfortunately taken a bit of back seat, I still haven't had a chance to work through the latest game in Linux Format as things have been busy and it's taking enough effort to fit in the C++ around it all. But hopefully as we get towards christmas things will quieten down a little and I can get back into it again.

Sunday, 12 October 2008

Still ongoing...

Well having recently had a holiday and having made some time to do some odd jobs around the place my pace of development has dropped. I've had some time to look around at a few books for C++ and have downloaded a copy of "How to Think Like a Computer Scientist". I think for the time being I'll be looking through this until I can purchase a book of my choosing, for some reason I still like books in dead tree format although as soon as devices like the BeBook become more affordable I may consider getting one of them instead.

Currently I'm looking at getting a Sam's teach yourself in one hour a day book but I've never used one of their books before normally preferring instead to go for something from O'Reilly or Apress. It's quite amazing how we can grow accustomed to and even develop a preference for a particular publisher.

In the mean time I've just received my next copy of Linux Format and there is another PyGame tutorial in there so I'll probably dive into that at some point in the very near future.

Monday, 15 September 2008

Reference and pointers

Well unfortunately I've hit a small snag in the short term of this plan. I've posted previously elsewhere about having to maintain a C++ application where I work, so far I've been able to get away with the rather rusty skill-set I currently have but now I have to make some fairly substantial changes to the code (something I was told would never happen, I should have figured something was wrong at that point).

So I need to move away from the Python and Mono plan temporarily and go back in time a bit and get to grips with C++ again. But why not turn this into an opportunity, at the very least it's an extra line on my CV, more importantly though it's an extra weapon in my programming arsenal (I say arsenal, it's really more a pile of rocks and a sling) which will allow me to contribute to a wider range of applications. A lot of programs written for Linux are admittedly either C, Python or Java but there are other projects out there, the Syllable operating system for instance is written entirely in C++ so there is an opportunity to do some good there perhaps.

Needles to say that I will be keeping up some Python whilst I'm going through this otherwise I'll probably end up starting over again, but for a short while my attention is unfortunately going to be on C++. Stay tuned though as you never know where inspiration may strike.

Sunday, 31 August 2008

Now with text

Okay so I've not had a lot of time to make much progress this week but I did manage to spend a few minutes on my space invaders game. Looking back through previous issues of Linux Format - as suggested by someone on their forums - I came across a flash card tutorial which used PyGame as well. The bulk of the game was text based so there was a bit which explained about rendering and displaying text on the game window, porting this into my game was quite trivial and so now I have a life and score counter.

The thing to learn from this though is that it is always worth hanging onto those back issues, even though some of the stuff is now out of date there is still a wealth of knowledge buried in those pages.

Sunday, 24 August 2008

Game update

Been hacking around with the code from Linux Format and added in a few features. So we've now got a configurable alien setup where you can define the number of rows and the number of aliens in each row. I've set up a missile count so we start out at the number of rows plus 1, it also now removes a missile for each row you take out and checks to see if there are more missiles than aliens and removes them so there is always one less missile than there are aliens, until you get to one alien then obviously we keep that final missile in play.

The speeds and drops are now configurable and we've still got missile collision detection and a configurable number of lives. Still got a few more things I can do with it, and when I've done those I'll probably be able to think of a few more but this is a great project for cutting my teeth with Python.

Saturday, 23 August 2008

Sods Law

As is normally the case, I finally settle down into doing something and I end up getting ill! Fortunately though I was starting to feel a bit better this morning and was inspired by the latest copy of Linux Format which turned up through my letterbox earlier this week.

One of the things I tend to do is flip straight to the back to see what kind of tutorials they're running and found to my delight one on Python game programming using PyGame, so I set about following the tutorial to see what I could do.

I could have easily skipped half the work at the first stage as LXF very kindly included the art-work from the tutorial on the cover disk but I figured I wanted to create my own, firstly so that the game felt more personal but also because the aliens look like a cross-dressing pac-man throwing kitchen funnels at the poor hero (if you're reading this Mike, it's this charm which makes them scary). I'm sure mine aren't that much better but like I said it makes the game feel more personal.

The code for the game is quite straight forward for a few reasons, firstly there is only one row of aliens, secondly the only collision detection is between the missiles and the hero/enemy and finally there is no on-screen text. So if you get hit it's game over and when you've killed them all it's game over. The only snag I hit was in setting the transparency for the graphics, the ones provided by LXF were probably already in the correct format whereas mine are not. Fortunately the PyGame surface class has a function called convert which sorts this out, so changing the line of code from:

self.bitmap = image.load(filename)
self.bitmap.set_colorkey((0, 0, 0))


self.bitmap = image.load(filename).convert()
self.bitmap.set_colorkey((0, 0, 0))

I suddenly ended up with transparent graphics.

Since then I've gone on and added in missile collision detection so I can blast their missiles out of the sky and I've added sprite collision detection, so if the aliens get to the bottom of the screen and they hit me it's game over. I also added lives into the game so I get three chances to lose now.

I'm possibly going to stick with this game for a few days now and add things like information to the display, more aliens and I might try and see if I can get it to become progressively more difficult. This is why I like game tutorials, they really get the imagination going.

Wednesday, 13 August 2008

What's it all about

It's probably best to explain a little about myself before I try to explain what this blog is about.

I'm a developer living in Derbyshire, I've been using Linux for about 8 years now and I've been using it as my only OS at home for about 1 year. I spend 5 days a week churning out code at work using a Microsoft stack, so .Net and C# pre-dominantly, and I've been doing this along with using some other proprietary stuff for the last 9 years.

Now I'm one of these people who feels lucky doing the job I do because I love to code. I enjoy finding solutions to problems and writing the code to implement that solution, I also like helping other people out with their problems which is why I've done a few websites for people I know as well. I've been helping out in then free and open source community for a while now by helping out in forums and such but I've gotten to the point where I want to do more and this is where this blog comes in.

I have been on and off trying to pick up Python and migrate my existing skills over to Mono but I get distracted or start trying to do too much and end up never accomplishing anything so I'm setting myself a couple of goals:
  1. Teach myself Python
  2. Migrate my existing skill to Mono
  3. Do enough of goals 1 and 2 to start contributing towards an open-source project
The purpose of this blog is for me to post up my progress and how I manage or overcome various obstacles so that I can see how I'm doing. But more importantly it is to provide some kind of account so that anyone else who finds themselves in a similar situation to myself can learn from my experiences. This is not meant to be a tutorial blog of any kind although I may well post code snippets if I've done something which I think other people may benefit from (or I'm particularly happy with myself and want to share that).

To date I've managed to get to chapter 8 in the "Dive into Python" online book and I've written a Hello World app using Mono and MonoDevelop! So I've got some way to go, but then this is all about the journey.