Monday, January 4, 2010
Game Programming in Python
Image Source: http://farm2.static.flickr.com
Python is a modern language that makes programming reasonably easy. At the same time, Python is a serious language that is used by companies and organizations such as Google, NASA, Industrial Light and Magic, and many others.
There is a fantastic free eBook by Al Sweigart that teaches how to use Python in a fun way. "Invent with Python" is not a book for making professional video games (which require teams of artists and programmers), but rather a book to teach computer programming to non-programmers by making simple fun games.
Al Sweigart is a legend for writing this book and sharing his world with us.
To get started with Python, all we need to do is go to his web page here:
http://inventwithpython.com/
It has links for downloading Python installer for multiple operating systems. It also has Al's free ebook "Invent Your Own Computer Games With Python" as a 4.27MB download in PDF format.
The book shows us fun games and then explains how they are put together in the Python Language. The companion website also has full sets of program code that we can download for each finished game.
(Note that the Appendix sections of the Book are on the website version of the book, but not in the PDF version of the book).
Here is a list of the chapters that are in the book:
In the first chapter Al tells us how to install Python 3.1.
So we downloaded and installed it.
The book said that we needed to install the older version 2 of Python to be able to make .exe files. So on the main web page we also selected to download and installed Python 2.6.4.
(Note that we did not experience any subsequaent technical problems having both versions installed at once).
To make a program in Python, we start up the Python "IDLE".
IDLE stands for "Interactive DeveLopment Environment". The development environment is software that lets us write Python programs. We use IDLE to type in our programs, and then press the F5 key to run them.
The window that appears when we first run Python IDLE is called the "interactive shell". We actually then do File > New to open a fresh window to program in.
A "shell" is a program that lets us type instructions into the computer. The Python shell lets us type Python instructions, and the shell sends these instructions to software called the Python interpreter to perform. We can type Python instructions into the shell and, because the shell is interactive, the computer will read our instructions and execute them when we press F5.
This is shown in the following screen capture:
There are several other fun games we can make using Al's book, such as a simple version of the classic "Hangman" game.
Making an .EXE Version of our Game
Appendix C in the online web version of the book covers in detail about how we can make an .EXE version of our game that we can run off our USB, or zip up and email to our friends.
Eg. Step by Step instructions can be found here:
http://inventwithpython.com/appendixc.html
We followed the steps, but had some minor difficulties along the way.
Downloading and installing py2exe was ok.
However, when we run it we must ensure we are using the Python 2.6.4 IDLE, and we are not in Python 3.1 which will not find py2exe.
When we ran the Appendix 3 confirmation test we received:
"DeprecationWarning: the sets module is deprecated"
A friend of mine who does Python told me this about "Deprecation":
There are MAJOR changes happening in the python language between 2.X and 3.X and those marked as deprecated in 2.X won't work in 3.X.
So the warning is an alert to programmers who plan to upgrade their code to the new version of python not to use the old 2.X methods, variables, and so on.
Next we made the setup.py script, which will build an .EXE for the "Hangman.py" Hangman Game.
Running the Windows command line proved problematic.
We had to change directory cd so that we were in c:\Python26
Then we had to copy our setup.py and Hangman.py files into c:\Python26
Then we had to alter the command to not contain the .py suffix in it:
Eg. type the command line in Windows XP command editor as:
python.exe setup py2exe
and not as python.exe setup.py py2exe
We then received a not found error on module "Hangman.py".
So we went back and edited our setup.py to contain "Hangman" instead of "Hangman.py".
This got the module compiling, but a failure with syntax error in Line 70 of our Hangman program. We suspect this is due to a Python 3.1 versus Python 2.6 language difference. We did not pursue this any further.
However the online book does contain an Appendix which discusses differences between Python 2.6 and Python 3.1 .
We decided to go for something simpler, and try to compile our simple "Hello" program into an .exe .
This worked fine and put out lots of text messages, and then created a "dist" folder as c:\Python26\dist
This dist folder contains our .exe and associated files. This folder is what we need to distribute to friends, or put onto our USB.
However, when we ran the "Hello.exe" it worked ok, but closed the runtime window so fast that we were unable to see the program result on the screen. The original Hello.py program possibly needs us to enhance it with a time delay coded into the end of it to keep the results on the screen for a few seconds.
Additional Python Resources:
http://www.pygame.org/
http://www.pyglet.org/
http://docs.python.org/3.1/tutorial/
Special thanks to Donna Benjamin from Creative Contingencies: http://cc.com.au who helped out with material for this post.
Python programming is ok, but personally I much prefer Flash Actionscript, because we can put our final products so easily onto web pages to share with the world immediately.
To sum up:
There is nothing to be scared of playing around with Python,
just as long as you work out who's the boss!
Image Source: http://gobalnerdy.com
DON'T FORGET TO DO OUR ONE CLICK SURVEY AT THE TOP RIGHT HAND CORNER OF THIS WEB PAGE. JUST CLICK ONE OPTION IN, AND THEN CLICK THE "VOTE" BUTTON.
Enjoy,
Big Passy Wasabi
Labels:
Python
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment