我正在尝试编写一个控制台刽子手游戏。如果我有一个问题,它会打印一个人被他的头上吊,然后如果我再次错过它会打印新图像以及前一个图像(参见下面的输出)。如何只显示一张文字图片?
这是我的代码:
import random
import urllib
animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()
#animals = ('dog','cat','sheep','mouse','frog','worm','bird','lion','dear')
while True:
print 'time to play hangman'
print 'hint: the word is an animail'
secret = random.choice(animals)
guesses = 'aeiou'
turns = 5
while turns > 0:
missed = 0
for letter in secret:
if letter in guesses:
print letter,
else:
print '_',
missed += 1
print
if missed == 0:
print 'You win!'
break
guess = raw_input('guess a letter: ')
guesses += guess
if guess not in secret:
turns -= 1
print 'Nope.'
print turns, 'more turns'
if turns < 5: print """
-------
| |
| O
|
|
|
|
|
---------
"""
if turns < 4: print """
-------
| |
| O
| \_|_/
|
|
|
|
---------
"""
if turns < 3: print """
-------
| |
| O
| \_|_/
| |
|
|
|
---------
"""
if turns < 2: print """
-------
| |
| O
| \_|_/
| |
| / \
|
|
---------
"""
if turns < 1: print """
-------
| |
| O
| \_|_/
| |
| / \
| d b
|
---------
"""
if turns == 0:
print 'The answer is', secret
break
user = raw_input("\n\tShall we play another game? [y|n] ")
again = "yes".startswith(user.strip().lower())
if not again:
raw_input ("\n\n\nPress enter to exit")
break
这是输出(注意它是如何打印多次的):
================================ RESTART ================================
>>>
>>> time to play hangman
hint: the word is an animail
_ _ a _ _ i _ _
guess a letter: p
Nope.
4 more turns
-------
| |
| O
|
|
|
|
|
---------
_ _ a _ _ i _ _
guess a letter: g
Nope.
3 more turns
-------
| |
| O
|
|
|
|
|
---------
-------
| |
| O
| \_|_/
|
|
|
|
---------
_ _ a _ _ i _ _
guess a letter: l
Nope.
2 more turns
-------
| |
| O
|
|
|
|
|
---------
-------
| |
| O
| \_|_/
|
|
|
|
---------
-------
| |
| O
| \_|_/
| |
|
|
|
---------
_ _ a _ _ i _ _
guess a letter: m
Nope.
1 more turns
-------
| |
| O
|
|
|
|
|
---------
-------
| |
| O
| \_|_/
|
|
|
|
---------
-------
| |
| O
| \_|_/
| |
|
|
|
---------
-------
| |
| O
| \_|_/
| |
| / |
|
---------
_ _ a _ _ i _ _
guess a letter: n
Nope.
0 more turns
-------
| |
| O
|
|
|
|
|
---------
-------
| |
| O
| \_|_/
|
|
|
|
---------
-------
| |
| O
| \_|_/
| |
|
|
|
---------
-------
| |
| O
| \_|_/
| |
| / |
|
---------
-------
| |
| O
| \_|_/
| |
| / | d b
|
---------
The answer is crayfish
Shall we play another game? [y|n]