quote wiki Encyclopedias :
Hunt the Wumpus yes Gregory Yob On 1973 A text-based Adventure games . In the game , Players walk through a series of connected caves , These caves are arranged as the apex of the dodecahedron , They hunted a man named Wumpus The monster of . In turn based games, players try to avoid deadly bottomless holes and “ Super bat ”, Will move the cave system around them ; The goal is to launch them through the cave “ Curved arrow ” One to kill Wumpus.
The original work is more complicated , Here we make the following simplification :
Though sparrows are small , But the five internal organs are complete ! First, let's take a look at the final effect , Duotu kills cats ~
Because of a miscalculation , Entering the monster's cave directly leads to failure : Because there is only one arrow , The wrong direction of archery leads to the survival and failure of monsters : Rely on intelligence , Succeed in defeating monsters , The outcome of victory ️
This is of course the simplest part , Use character string
Output print
A welcome message or game help message . Then receive the user's choice input
, And make judgments according to different conditions if
def menu():
print("=== Welcome to the world of umbas ===")
ch = input("1. Start \n2. sign out \n->")
if ch == "1":
begin()
if ch == "2":
print(" ok , See you next time ")
Of course, the map is the most important part , There are many ways to do it , Here we use the simplest and easiest to understand List nesting
( Two dimensional array ).
def create_map(x=5, y=5):
# Generate maps
yj = []
for _ in range(y):
xi = []
for _ in range(x):
xi.append(SPACE)
yj.append(xi)
wumpas_local, player_local = create_player_local(x, y)
yj[wumpas_local[0]][wumpas_local[1]] = WUMPUS
yj[player_local[0]][player_local[1]] = PLAYER
global p_local
p_local = [player_local[0], player_local[1]]
return yj
Players and monsters coordinate
namely Two dimensional array
The subscript , Generate using random numbers . Here is Be careful Slightly ! The random coordinates of players and monsters may coincide ! There are many ways to avoid the repetition of random numbers , Use here A recursive algorithm
Generate unique coordinates . First, randomly generate two coordinates , If the coordinates are the same, call the function that generates the coordinates again , Until two different coordinates are generated .
def random_local(x, y) -> tuple:
""" Randomly generate coordinates """
return random.randint(0, x - 1), random.randint(0, y - 1)
def create_player_local(x, y) -> tuple:
""" Generate unique coordinates for monsters and players wumpas_local Monster coordinates player_local Player coordinates """
wumpas_local = random_local(x, y)
player_local = random_local(x, y)
if wumpas_local == player_local:
wumpas_local, player_local = create_player_local(x, y)
if wumpas_local != player_local:
return (wumpas_local, player_local)
The map has been generated , The display is very simple ! Simply replace the string of the map according to the winning or losing status .
Player displacement , Umbas' breath and archery logic are the same . The essence is to deal with the coordinates of the four directions of the lattice ( Breath needs to deal with the surrounding eight grids ).
Ha , It only uses addition and subtraction within two ( Add one , Minus one ), Easy ? what ? Have been dizzy ? Get a pen and paper and draw ~.
def smell(x, y) -> bool:
""" Judge whether there are monsters nearby """
x1 = MAP_X - 1 if p_local[0] == 0 else p_local[0] - 1
x2 = 0 if p_local[0] == MAP_X - 1 else p_local[0] + 1
y1 = MAP_Y - 1 if p_local[1] == 0 else p_local[1] - 1
y2 = 0 if p_local[1] == MAP_Y - 1 else p_local[1] + 1
round = [(x1, y), (x2, y), (x, y1), (x, y2), (x1, y1), (x1, y2), (x2, y1), (x2, y2)]
for r in round:
if MAP[r[0]][r[1]] == WUMPUS:
return True
return False
Yes , The whole game is actually a state machine , All in all victory
, Failure
, In the game
Three states . Then we can use a while
sentence , Start a The event loop
. You can always play happily ! Happy or not ?
Although this program has no complex algorithm , But it's easy to use The order
, Judge
, loop
Sentence and yidudu A recursive algorithm
You can realize a little complicated Games .
As the saying goes : The greatest truths are the simplest A heavy sword has no edge It's a coincidence
This game also has a lot of expandable content , Here are a few thinking questions , Make the game more fun ~
Bottomless pit
and Super bat
Elements . Game points
, Game timing
And Game archive
function .See source code : github.com/spaceack/Hu… Or pay attention to the official account 【 The dance of programming 】 reply wumpus receive .