True is the opposite of false. Up is the opposite of down. What is the opposite of dog?
If you replied “cat”, what exactly makes cats the opposite of dogs the same way up is the opposite of down?
I use this example to point out a common problem that software developers make with variable and parameter names. When there are two possible values for some variable or parameter, it is very common that it should be defined as a boolean type.
Jeff Atwood wrote a post on his Coding Horror blog entitled “Please Don’t Learn to Code” in which he rails against the idea that “everyone should learn programming”.
And I couldn’t agree more.
People, not everyone needs to learn programming. Only some gifted individuals (of which we professional software developers are included) need to learn programming. For the rest of you, unless you are srsly committed it will just be a meaningless chore that may damage your tiny brains.
Coding is just like surgery: if an amateur decides to code their own Angry Birds clone as a fun little project, people will literally die. Those are the stakes, folks. That’s why it should be left to those who are explicitly pursing it as a professional career.
TL; DR link
You have my assurance that I find Bloomberg’s encouragement of people to learn a technical skill personally offensive. It filled me with a rage that was only subdued after discouraging a small child from learning to play the harmonica. (What’s the kid going to do with that skill anyway? There are better ways he could spend his valuable time.)
This post goes into the details of how you can add a “save game” feature to your games. Python’s built-in shelve module makes this very easy to do, but there are some pitfalls and tips that you might want to learn from this post before trying to code it up yourself.
To give an example of adding a “save game” feature to a game program, I’ll be taking the Flippy program (an Othello clone) from Chapter 10 of “Making Games with Python & Pygame” (and Reversi from Chapter 15 of “Invent Your Own Computer Games with Python”.)
If you want to skip ahead and see the Flippy version with the “save game” feature added, you can download the source code and image files used by the game. You need Pygame installed to run Flippy (but not Reversi).
- This tutorial is short.
- To figure out bugs in your code, you might put in
print() calls to display the value of variables.
- Don’t do this. Use the Python
logging is better than printing because:
- It’s easy to put a timestamp in each message, which is very handy.
- You can have different levels of urgency for messages, and filter out less urgent messages.
- When you want to later find/remove log messages, you won’t get them confused for real
- If you just print to a log file, it’s easy to leave the log function calls in and just ignore them when you don’t need them. (You don’t have to constantly pull out print() calls.)
Using print is for coders with too much time on their hands. Use logging instead. Also, learn to use the Python debugger to debug bugs and Pylint to prevent bugs and make your code readable.
To print log messages to the screen, copy and paste this code:
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')
To write log messages to a file, you can copy and paste this code (the only difference is in bold):
logging.basicConfig(filename='log_filename.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('This is a log message.')
Later runs of the program will append to the end of the log file, rather than overwrite the file.
To log messages to a file AND printed to the screen, copy and paste the following:
I made a twitter bot that checks every hour for someone who has asked the question, “Why do homeless people have dogs?” and automatically replies, “Because a dog will love you even though you are homeless.” It’s running right now at @YHobosHaveDogs.
Figuring out how to code this took a couple evenings and a little hair pulling, so I decided to document the process in this blog article to make it easier for the next programmer. This will be making a Twitter bot in Python using the python-twitter module (which runs on Python 2, not Python 3), and then running the bot from my Dreamhost server (but most likely any web host will work just fine. Or if you have a computer that is always online, you can run the bot from that). First we will run the bot from our machine to test it out, and then load it onto the Dreamhost web host. (I’m running a Windows box, but the steps should work on any OS.)
- Download the python-twitter module (I tried some of the other modules but didn’t like them as much.)
- Unzip this file and from the command line in the unzipped directory, run “python setup.py install” to install the twitter module.
Long before World of Warcraft, people played text-based MMORPGs called MUDs (Multi-User Dungeon). These were basically multiplayer text adventure games where people could wander through a virtual world fighting monsters and exploring. They had several RPG elements to them.
CircleMUD was a popular piece of server software for running a MUD, and it came with a sizeable virtual world (which the admin could modify/append to customize their fantasy world.) It would be pretty handy to use parts of this data if you were creating your own virtual world for a text adventure game, but the format of CircleMUD’s data files is kind of obtuse and not amenable to manipulation.
So I wrote a few scripts to convert these files into a single XML file which is 4MB when unzipped. You can parse this file and modify it to suit your needs. It contains 1979 rooms across 30 different areas (called zones in the file), with 46 shops and 569 different “mobs” (mobile objects, which are the monsters and NPCs). There are 678 different types of objects, including 116 weapons and 154 types of armor.
The scripts and original CircleMUD data (along with descriptions of the data formats) are included in the zip:
Download CircleMUD XML Data (1.3 MB zipped)