Have you ever been surprised by someone who bought you something completely unexpected? Something which they would not normally get for you as it's uncharacteristic of them?
Well during my recent birthday I got a note through the door whilst I was out from the Royal Mail saying that they had tried to deliver a package but of course no-one was home. So later that day I headed down to the delivery office and picked it up and one top of the items in the box was an Amazon note and a message from my Dad wishing me a happy birthday. Now in the box was one book which really took me by surprise, he'd gone and bought me "Beginning Game Development with Python and PyGame"! To say I was bowled over would be an understatement.
My dad was the person who initially got me into programming, he insisted when I got my first computer that I learn to use it properly and it not be used only for gaming. Whilst I'm sure he meant that I should learn how to write documents and edit graphics with it instead of hacking around with code he encouraged me to continue anyway, but never actually as far as buying me a programming book so this is a first. I know he bought it from my Amazon Wish List but there were other books on there along with music and DVD's so the fact he bought this one is incredible. I'm fairly sure he's not following this blog either as usually just turning a computer on is enough to get him fed up with using them so the idea he spends time browsing the web and reading a blog of mine dedicated to programming is slightly more than unlikely.
But anyway I thought I'd mention as the book is likely to drive some further posts to here and I'll write up a review on the book when I get somewhere useful with it.
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Sunday, 1 February 2009
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 = names_file.read().replace('"', '').split(',')
names_file.close()
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:
alphas = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
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.
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 = names_file.read().replace('"', '').split(',')
names_file.close()
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:
alphas = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
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, 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.
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.

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))
to
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.
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))
to
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.
Subscribe to:
Posts (Atom)