Jump to content
  • Advertisement
  • entries
  • comments
  • views

Hunt the Wumpus

Sign in to follow this  
Trapper Zoid


It looks like I've broken the 4000 post mark sometime in the last few days; a clear indicator I've been hanging around here longer than is good for me [smile].

Yesterday I finished up my first Python game; I made a post on it, but I'll duplicate the code here. It's not particularly spectacular; it's a Python version of the classic "Hunt the Wumpus"; a classic of microcomputers in the 70s. Maybe next I'll go for a remake of Hamurabi.

"""Hunt the Wumpus - a simple game in Python

My first Python program.

import sys
import random

class Game:
"Hunt the Wumpus Game Class"

# The cave map
output = sys.stdout.write;

# start for the player
player = "A"

# the standard environment (will be randomised)
wumpus = "T"
pits = ["I","O"] # should be wiped by random positions
bats = ["G","M"] # should also be wiped by random positions
shots = 5

def __init__(self):

def run(self):
"Runs the game of Hunt the Wumpus"
self.output("Do you need instructions (y/N)? ")
if self.readInput() == "Y":
loop = True
while (loop):
self.output("Do you want to play another game (Y/n)? ")
if self.readInput() == "N":
loop = False
self.output("Fare thee well, mighty hunter!\n")

def mainLoop(self):
"The main loop that runs the hunt"
isGameActive = True
# loop until we win, lose or quit
while (isGameActive):
self.output("Do you want to (M)ove or (S)hoot? ")
action = self.readInput() # get the input letter
if action == "M": # MOVE
self.output("Which chamber to move to (")
# print the options of neighbours
neighbours = self.CAVES[self.player]
for chamber in neighbours[:-1]:
self.output("%s, " % chamber)
self.output("%s)? " % neighbours[-1])
chamber = self.readInput()
if chamber in neighbours:
self.player = chamber
self.output("Tread carefully...\n")
self.output("That's not a valid move...\n")
elif action == "S": # SHOOT
self.output("Which chamber to shoot into (")
# print the options of neighbours
neighbours = self.CAVES[self.player]
for chamber in neighbours[:-1]:
self.output("%s, " % chamber)
self.output("%s)? " % neighbours[-1])
chamber = self.readInput()
if chamber in neighbours:
self.output("You fire your trusty blunderbuss! BLAM!!\n")
self.shots = self.shots - 1
# was it a hit?
if chamber == self.wumpus:
self.output("THUWMP!! The wumpus has been slain!\nWell done, mighty hunter!\n")
isGameActive = False
if self.shots > 0:
self.output("The echo of the shot cannons through the caverns\n")
# should move the wumpus now
self.output("You hear the wumpus flumphing about. It might have moved...\n")
wumpus_choices = list(self.CAVES[self.wumpus])
self.wumpus = random.choice(wumpus_choices)
if self.shots > 1:
self.output("You've got %d shots left\n" % self.shots)
self.output("Last shot. Make it count!\n")
self.output("AARGHH! You've caused a cave in!!\nRocks fall, everybody dies!!\n")
isGameActive = False
self.output("You can't shoot there, silly...\n")
elif action == "Q": # QUIT
self.output("Do you want to end this game? (y/N) ")
if self.readInput == "Y":
isGameActive = False
# check to see if this room is safe
moveLoop = isGameActive
while moveLoop:
moveLoop = False
if self.player == self.wumpus:
self.output("AARGHH!! You've bumped into the wumpus!\n")
self.output("It seems the hunter is now the hunted... or rather, lunch.\n")
isGameActive = False
elif self.player in self.pits:
self.output("AARGHH!! You've fallen into a bottomless pit!\n")
self.output("When you fall into a bottomless pit, you die of starvation...\n")
isGameActive = False
elif self.player in self.bats:
self.output("OH NO!! You've run into some giant bats!\n")
self.player = random.choice(self.CAVES.keys())
self.output("They've carried you to Chamber %s. Hope it's not too dangerous...\n" % self.player)
moveLoop = True

def describeChamber(self):
"Describes the players chamber"
neighbours = self.CAVES[self.player]
self.output("You are currently in Chamber %s.\n" % self.player)
self.output("Passages lead to Chambers ")
for cavern in neighbours[:-1]:
self.output("%s, " % cavern)
self.output("and %s.\n" % neighbours[-1])
# need to check for breezes, stenches and bat chatter
chatter = False
breeze = False
stench = False
if self.wumpus in neighbours:
stench = True
for cavern in self.pits:
if cavern in neighbours:
breeze = True
for cavern in self.bats:
if cavern in neighbours:
chatter = True
if stench:
self.output("You smell a wumpus!\n")
if chatter:
self.output("You hear the chatter of bats.\n")
if breeze:
self.output("You feel a nearby breeze.\n")

def printWelcome(self):
"Prints the welcome message to the screen"
self.output(" HUNT THE WUMPUS ".center(50, "-"))

def printInstructions(self):
"Prints out the instructions for Hunt the Wumpus"
self.output("""There's a wumpus loose in the underground caverns!
As the great wumpus hunter, it is your job to hunt it down with your trusty blunderbuss!

On your turn, you can move or shoot into one of three caverns.
You have five shots with which to hunt the wumpus.
You can sense when a wumpus is nearby by its tell-tale stench.
Look out for pits; you'll feel a breeze when nearby.
Also watch for the bats; they delight in randomly dropping hunters into caverns.

Good luck, mighty hunter!

def readInput(self):
"Reads in a line of imput, and returns the first character (all that's needed for the game)"
i = sys.stdin.readline()
return i[0].upper()

def setEnvironment(self):
"""Sets the environment: the player to pos. A, and randomise everything else
Random position of the wumpus
Random bats (for the number of the caves)
Random pits (for the number of pits)

cave_list = self.CAVES.keys()
self.player = "A"
# remove the player position from the list
self.wumpus = random.choice(cave_list)
num_random_pos = self.NUM_PITS + self.NUM_BATS
random_list = random.sample(cave_list, num_random_pos) # need to make sure nothing is in the same pos to begin with
self.pits = random_list[:self.NUM_PITS]
self.bats = random_list[-self.NUM_BATS:]
self.shots = 5

if __name__ == "__main__":
wumpusGame = Game();
wumpusGame.run() # run the main game function

I'm not completely happy with this current game, simple though it may be. I think my use of a class in there was inelegant; mostly because I was playing around with classes to get a feel of them. I haven't yet figured out the best way to think like a Python programmer.

Eventually I'll move onto something a bit more complicated; either I'll move to Python and graphics or I'll try to get a text control library like curses and/or wcurses up and running to make a rogue-like game.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!