Posted by Devin de Gruyl on Aug 19th, 2008
Yes, I’m old. Old enough to remember the MCP when it was just a chess program! He started small and he’ll end – sorry about that, wrong movie.
Actually, I have been around the block a few times. Maybe a few too many. I’m old enough to remember the likes of COMPUTE! Magazine, one of the most popular computer magazines of the early home-computer revolution, and one of its signature features – type-in programs, usually games, that were free for the cost of entering about 4-5 pages worth of BASIC listing.
The programs themselves were of little consequence (save for SpeedScript, a surprisingly powerful little word processor for its day that actually gave even the humble Commodore VIC-20 and Atari 400 computers a WordPerfect-like environment for document creation), being either simple arcade-style games or programs that performed a very specific function (such as loan amortization or Little League statkeeping). But most of them were in BASIC, and often came accompanied with articles that demonstrated specific programming techniques you could learn from the program, and thus carry over into your own projects later on should you so desire. (You’d never see something like that today, and not just because the home-computer market has moved away from the hobbyist and wannabe-coder user base!)
In the spirit of COMPUTE!, Ahoy!, Run, and other programmer-friendly mags from back in the day, I’ll offer up this little nugget for your consumption. Consider it a practical crash course in programming in Python, a powerful yet surprisingly human-readable language that has in just a few years carved out a strong fan base; it can do everything from basic scripting functions to being the backbone of full-fledged applications.
What I have here is a little Python program that simulates the bonus round of the classic 1972-86 TV game show The Joker’s Wild, which is a good example of a simple gambling game that makes a fine teaching tool for budding programmers. In this game, three slot machine-style wheels are spun, each wheel containing various amounts of money ($25, $50, $75, $100, $150, or $200). However, one of the three wheels, chosen at random, also contains a Devil. After each spin, the total amount of money showing in the three windows is added to the player’s score. The player then has the option of stopping and keeping the money won to that point, or risking it all by taking another spin. If the Devil pops up at any time, the game ends and the player wins nothing. On the other hand, building up a total of $1,000 or more would win the game (and, on TV, a bonus prize package in addition to the money).
While my little program won’t actually fatten your bank account, it might be able to expand your knowledge of program code and, by extension, whet your appetite for more. Even if you’ve never written a single line of BASIC before, I’ll do my best to make this understandable and readable. (In my experience, it’s often better to begin learning a new programming language with simple-but-practical applications that actually do something, rather than the standard “Hello World!” deal.) In the interest of keeping article size to a manageable length, today I’ll simply provide you with the listing; tomorrow, I’ll break the listing down section by section and show you what’s going on behind the scenes.
Okay. First of all, you’re gonna want to install Python on your computer. Luckily, it’s freely available at www.python.org for just about any computer platform you care to name… everything from Windows to Mac to *nix to OS/2 to even the Amiga! Follow the installation instructions for your platform of choice, then come back to me once everything looks good to go. Go ahead, I’ll wait.
[plays and loses about twenty hands of Spider Solitaire while waiting]
…Ah, you’re back. And all set to type some code, I see. Well, I hate to disappoint!
Here’s the program listing. You’ll have to type this listing in by hand, I fear, since I can only get WordPress to make the code nice and readable if I display it as a screenshot. Many apologies. (Important: Do not use a word processor like MS Word, OpenOffice.org Writer, abiword, or even WordPad to enter this, as full-blown word processing programs often insert their own formatting codes that can foul up programming code. Use a dedicated ASCII text editor like Notepad, GEdit, or whatever plain-text editing options your OS of choice offers; every OS has at least one. If you must use a word processor, be very sure to save the document as a plain ASCII text (.txt) file!) Don’t worry about understanding all of this just yet; in my next article, I’ll take you through each step and show you what all this gibberish is doing.
When you’re finished typing, save the file. You can call it anything you like, but it’s preferred you give it a .py extension. Now, to run it, open up a command-line window; in most forms of Windows, this is done by going to Start -> Programs -> Accessories -> Command Prompt, or by opening a Terminal window in OSX and various *nix systems.
On my system, I called the file devilgame.py, so the command to run the script for me would be:
python /path/to/devilgame.py
(If you get a “Command not found” or similar error and are positive you installed Python correctly, try changing to the directory Python got installed to with cd, then reissue the command.)
If all goes well, you should see something like the following appear in your Command Prompt/Terminal window:
$100
$75
$75
Total Score: $ 250
You need $ 750 more to win.
Press N to stop and keep what you’ve won, or Y to risk it:
Or, if you’re not so lucky, you’ll get something like this:
$200
DEVIL
Sorry, you lost everything! Try again…
Well, there you have it – a basic, text-only game that demonstrates simple programming techniques. In my next article, which I’ll post tomorrow, I’ll take you through the listing step by step and show you what is going on at every step of the way… and hopefully giving you an appreciation for coders that you may have lacked before. See you then…
Posted in code, lessons
| email this article
If you liked that, try...
- Simple Python Game 2: Electric Boogaloo
- Fortran Adventure Source Code Found
- HOWTO: Fix a “Flashing” NES
- Jim Butterfield, 1936-2007
- $^&@! this Code!
No Comments »
No comments yet.


Podcast RSS


