For this version, we shall use the following scenario: The player assumes the role of King Arthur. He or she is given a menu of weapons to use against the rabbit such as sword, dagger, Holy Hand Grenade of Antioch, and so forth. Each weapon will have a different level of effectiveness and reap a different reward (the ever popular “points”). For example, a dagger might only rarely defeat the rabbit; perhaps one in ten tries, while a sword defeats the rabbit two in ten tries and the Holy Hand Grenade works nine times out of ten. The dagger would have a much higher reward though, maybe 1000 points versus the sword’s 400 points and the Holy Hand Grenade’s meager 50 points. Without this point variation there will be little motivation to ever use anything other than the most effective weapon. The player continues to choose weapons and battles the rabbit (and the rabbit’s offspring should the rabbit be killed) until the player loses a certain number of times (normally depicted as “lives” but this could be thought of as simple “loses”). For example, the player might start out with five lives and they will continue to play until they are defeated (killed) by the rabbit five times. At this point, they will have accumulated a certain number of “points”, the more the better (not that the player could use them for anything, like say, a sandwich or a Monty Python DVD).
So, a basic pseudo code might look something like this:
1. Import the random module!
2. Initialize game variables such as lives = 5 and score = 0.
3. Give the player directions.
4. Start the main battle loop which continues for as long as the player has lives left.
5. Show the player the weapon menu.
6. Ask the player for their choice of weapon.
7. Compute whether or not the weapon worked this time (requires random number).
8. If the weapon worked, tell the player and increment score by the appropriate number of points.
9. If the weapon failed, tell the player and subtract a life.
10. Tell the player their current score and lives.
11. End of loop from step four.
12. Give player final parting message.
Think of this as a general starting point, the core of the program: Get this much working first. Once the core is up and running, consider expanding some of these ideas to make the game more interesting and entertaining to play. For example, the winner/loser messages can be randomized, a new life could be earned after accumulating a certain number of points, a weapon used too frequently could become “broken” and unavailable, etc. We shall look at a few of these momentarily but let’s concentrate on the core first.
The first three steps should be very familiar by now. Take care to put in some effort on step three “Give the player directions”. One element that makes computer games entertaining is good, detailed, (and in this case) humorous text. This would be an ideal place to use a triple quoted string. For example, you might consider using the “ASCII graphics” technique explained in the beginning of the text to create a giant image of a rabbit.
Step four is tailor-made for a
while loop. The game will continue for as long as the player has lives remaining:
while lives > 0:
Steps five and six should also be familiar. The weapons menu involves one or more
input() statement where the player enters their numeric choice (an integer) from the weapons menu. The more weapons on the menu, the more permutations of the game that exist, and the more entertaining it can be. Once finished, it is suggested that the menu contain at least 10 different weapons. Don’t restrict yourself to conventional weapons, either. Along with swords and arrows the choices might also include a shrubbery or a cow launched from a catapult.
Step seven is the most interesting part. Each weapon needs to have two things associated with it: the odds of it working and the points awarded if it does indeed work. One possible way of handling this is to create a large
if/elif structure to handle each weapon. First, a random number would be generated, let’s say an integer between 0 and 100. Then for each weapon, that number would be compared to the odds for that weapon to determine if it was a winner. An appropriate message would result along with adjustment of the scores and lives. For example, suppose there are ten weapons on the menu. Weapon one is a rock. It has a low chance of winning, maybe 10% of the time (i.e., 10 times out of 100), but it has a high payout, maybe 500 points. The second weapon is a lance. Its odds are better at 15% but it only pays out 200 points.
r = random.randrange(100) if weapon == 1: # rock if r < 10: # 10% chance to win print( “You killed the rabbit!” ) score = score + 500 else: print( “You die an embarrassing death: rabbit food!” ) lives = lives – 1 elif weapon == 2: # lance if r < 15: # 15% winning odds print( “The rabbit has been vanquished!” ) score = score + 200 else: print( “Fool! You end your days as an appetizer!” ) lives = lives – 1 elif weapon == 3: # next weapon on menu…
This would continue for all of the weapons on the menu, each with its own odds, award, and messages. It is particularly useful if some of the losing messages are notably snarky in attitude. This makes it more enjoyable for someone who is just watching someone else play the game. Once this is completed, a status message is needed (step ten) which is merely a print out of the current score and lives remaining. Other items might be added as well, such as the number of attempts or a win/loss percentage (other variables would need to be created and then modified and updated within the loop).
Finally, a parting message (step twelve) is added at the very end, once the loop is complete.