Developing the basics : programming myself, post six
42 and the meaning of life.
So a couple of posts ago, I shared with you a small, albeit incredibly awesome, program I made “what is the meaning of life” which asked you to guess the meaning of life. It wanted the number 42. Anyone who is familiar with Hitchhiker’s guide to the galaxy will know that this is the answer the computer Deep Thought gave as the answer to life, the universe, and everything. Douglas Adams was known to be a bit of a computer geek, so it would be nice to theorize that he chose 42 with a hidden agenda. As in the ASCII language, 42 is the designation for asterisk, which is the wildcard character in regular expressions, so it is whatever you want it to be, thus making the meaning of life 42 or “whatever you want it to be.”
Unfortunately, it isn’t this clever:
“The answer to this is very simple; It was a joke. It had to be a number, an ordinary, smallish number, and I chose that one. Binary representations, base 13, Tibetan monks are all complete nonsense. I sat on my desk, stared into the garden and thought 42 would do. I typed it out. End of story.” – Douglas Adams – from an interview by the Independent.
The effects of programming: you double check everything
It did though seem feasible like it was information which could be good to use. But like in this series, I hope that I can dig out false information, false promises and genuinely provide you with what I find works for learning programming and what you can maybe try yourself.
The epic world of computer science is gradually getting more and more accessible now as I discover new things that are possible to do with code. What has been really interesting are the things which other people have already put together for developers, to make achievable software I wouldn’t have believed was possible after only a couple of months.
More about libraries
Like the kind of new world that the Pygame library has opened up. This library has taken out a lot of the pain of programming parts in Python, it is well documented and has some great example programs to look into. With libraries like this, you have an incredible shortcut into the more difficult concepts as the legwork has already in a sense been done, you can just start using the functions to make your own programs really awesome.
Making games is probably one of the most fun things you are going to be able to do with programming, as you can not only make them but then play them after.
The Pygame library comes with a lot of example programs which you can look into and modify as you wish. Sometimes this can be a good way of just seeing how it all comes together.
Aliens example. In the Lib\site-packages\pygame\examples\ Part of the downloaded Pygame library, you can find a few example games. Simply copy the .py file of the game over to your project and make a data file to store all the sprites(character graphics) and sounds etc.
Once this is done you can run the code sample, and it should let you play the game.
(Yes I know I have a score of 0 – it’s because trying to make a screenshot when defending the earth from an alien invasion is actually really hard.)
What is the point of this? Well, it is threefold:
First, it lets you look into the code and so you can now hopefully understand a bit about what the various parts of the code are doing.
So, first of all, we should make this game easier:
class Alien(pygame.sprite.Sprite): speed = 5 #let's make this 5 as the original 22 was ridiculous animcycle = 12
Second, it lets you modify the code and see what the effect is, so you can really understand the interaction of the code and how it has come together.
Third, it shows you what exactly you can do, for instance, this game pops up in a new window or GUI (graphical user interface) as it is called in the business. If they can make this happen, then you too can do this, and with the source code at hand, all it takes is a little bit of diving into it.
Making games from scratch is pretty difficult, so if you can find some code which generally does what you too want to achieve, then you can take it adapt it and improve it. Take some code remove it and modify it and see the result it has. Usually, this will be some kind of syntax error… but it can get you familiar with which parts rely on what and how they interact, to build your understanding on.
So to make a window for instance:
screen = pygame.display.set_mode(SCREENRECT.size) #The SCREENRECT was defined with a value at the beginning of the source code.
The full code test so you can check it works as you think it should:
import pygame SCREENRECT = Rect(0, 0, 640, 480) screen = pygame.display.set_mode(SCREENRECT.size) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: raise SystemExit
This actually sounds much easier than it is though. As even though the code is commented, extracting just small parts which work for your own application can be a real pain in the bum. Diving into this is a like taking the blue pill that Morpheus is offering up, only instead of Morpheus it is actually just a drunk guy, and instead of a pill to unlock the world beyond the Matrix, it is actually just some random abuse. You have been warned!
But it can give you some cool ideas and examples of how to work the code into your own projects. Though sometimes it is not so easy to work out how to make the parts which you want to use work. This is a good example though of why you should get into the habit of making your code clear. As you never know who might need to look at it (sometimes yourself). So comment your code, name your variables and functions nicely, and make an impact.
“The Ultimate Answer to Life, The Universe and Everything is…42!” – Deep Thought