好吧,所以我正在为我的CSCI课程做最后的项目,我决定选择一个刽子手计划,因为它是一个相当简单的游戏来实现。除了我遇到的唯一问题就是我的一些打印语句,我所做的一切都完全正常。代码如下:
import random
import os
HANGMANPICS = ['''
+---+
| |
|
|
|
|
|
=========== ''','''
+---+
| |
O |
|
|
|
|
=========== ''','''
+---+
| |
O |
| |
|
|
|
=========== ''','''
+---+
| |
O |
/| |
|
|
|
=========== ''','''
+---+
| |
O |
/|\ |
|
|
|
=========== ''','''
+---+
| |
O |
/|\ |
/ |
|
|
=========== ''','''
+---+
| |
O |
/|\ |
/ \ |
|
|
=========== ''']
ANIMALS = '''cat dog fish whale otter spider snake bird dolphin tiger mouse
rabbit bear lion'''
CITIES = '''indianapolis chicago orlando miami denver columbus memphis oakland
seatlle phoenix dallas detroit baltimore cincinatti'''
COUNTRIES = '''india china america japan egypt greece mexico italy canada
australia france brazil germany korea'''
def Countries():
print 'Guess the name of this country'
return COUNTRIES
def Cities():
print 'Guess the name of this city'
return CITIES
def Animals():
print 'Guess the name of this animal'
return ANIMALS
def Random():
print 'Guess the name of this randomly chosen country, city, or animal'
return COUNTRIES + CITIES + ANIMALS
def Welcome():
print ('Select a category:')
print ('1: Countries')
print ('2: Cities')
print ('3: Animals')
print ('4: Random')
choice = {"1": Countries, "2": Cities, "3": Animals, "4": Random}
choose = raw_input()
return choice.get(choose, Random)().split()
def getRandomword(wordlist):
wordindex = random.randint(0, len(wordlist)-1)
return wordlist[wordindex]
def Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD):
os.system('cls')
print(HANGMANPICS[len(MISSEDLETTERS)])
print
print 'Missed letters:',
for letter in MISSEDLETTERS:
print letter,
print
blanks = '_' * len(SECRETWORD)
for i in range(len(SECRETWORD)):
if SECRETWORD[i] in CORRECTLETTERS:
blanks = blanks[:i] + SECRETWORD[i] + blanks[i+1:]
for letter in blanks:
print letter
print
def getGuess(alreadyguessed):
while True:
print 'Guess a letter'
guess = raw_input()
guess = guess.lower()
if len(guess) != 1:
print 'Please enter single letters'
elif guess in alreadyguessed:
print 'This letter has already been guessed, please guess again'
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print 'You did not guess a letter, please guess a letter'
else:
return guess
def playAgain():
print ('Do you wanna play again? (Yes or No)')
return raw_input().lower().startswith('y')
print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)
done = False
while True:
Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)
guess = getGuess(MISSEDLETTERS + CORRECTLETTERS)
if guess in SECRETWORD:
CORRECTLETTERS = CORRECTLETTERS + guess
found = True
for i in range(len(SECRETWORD)):
if SECRETWORD[i] not in CORRECTLETTERS:
found = False
break
if found:
print('You won the game!')
print('The correct word was ---->' + SECRETWORD.upper())
done = True
else:
MISSEDLETTERS = MISSEDLETTERS + guess
if len(MISSEDLETTERS) == len(HANGMANPICS)-1:
Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)
print('You did not guess the word correctly. The word was : ' + SECRETWORD)
done = True
if done:
if playAgain():
os.system('cls')
words = Welcome()
MISSEDLETTERS = ''
CORRECTLETTERS = ''
done = False
SECRETWORD = getRandomword(words)
else:
break
我希望在选择完成后,在国家,城市,动物和随机中的打印报告在实际的游戏屏幕上打印,但是我没有运气,如果我拿出来它就有效来自两个地方的os.system(' cls')但它们有助于使程序看起来更好,这也是一个加分。任何有助于使这些印刷语句发挥作用的建议都会很棒!谢谢!
答案 0 :(得分:1)
问题是你在调用Cities(),Countries()等中的print语句后调用os.system('cls')
。让我们来看看你的代码结构:
print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)
done = False
while True:
Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD)
当Welcome()
存在时,它会调用相应的函数,例如Cities()
def Cities():
print 'Guess the name of this city'
return CITIES
这个print语句被调用和打印,但几乎立即执行将流入你的while True
循环,调用Display()
立即调用os.system('cls')
,删除Cities()
内的打印1}}。这有意义吗?
我认为最好的方法是将他们的选择存储在一个变量中,并在清除屏幕后让Display()
打印正确的信息。但是,这将涉及您的代码的轻微重组。这样做的一种强硬方式是根据Welcome的当前返回值推导出他们的选择。这是我对你的代码的攻击,我认为你做了你想做的事情:
def Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD, CHOICE):
os.system('cls')
CHOICE()
print(HANGMANPICS[len(MISSEDLETTERS)])
print
print 'Missed letters:',
for letter in MISSEDLETTERS:
print letter,
print
blanks = '_' * len(SECRETWORD)
for i in range(len(SECRETWORD)):
if SECRETWORD[i] in CORRECTLETTERS:
blanks = blanks[:i] + SECRETWORD[i] + blanks[i+1:]
for letter in blanks:
print letter
print
def getGuess(alreadyguessed):
while True:
print 'Guess a letter'
guess = raw_input()
guess = guess.lower()
if len(guess) != 1:
print 'Please enter single letters'
elif guess in alreadyguessed:
print 'This letter has already been guessed, please guess again'
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print 'You did not guess a letter, please guess a letter'
else:
return guess
def playAgain():
print ('Do you wanna play again? (Yes or No)')
return raw_input().lower().startswith('y')
print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
if words == ANIMALS:
CHOICE = Animals
elif words == CITIES:
CHOICE = Cities
elif words == COUNTRIES:
CHOICE = Countries
else:
CHOICE = Random
MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)
done = False
while True:
Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD,CHOICE)
guess = getGuess(MISSEDLETTERS + CORRECTLETTERS)
答案 1 :(得分:0)
尝试在print语句后调用sys.stdout.flush()。
答案 2 :(得分:0)
两种可能性:
1)像libcurses一样的光标放置。您可以将光标放在(文本)屏幕上的任意位置并打印出文本字符。我不知道更多的东西 - 你必须要查找并希望python绑定到libcurses。 (" urwid"在这里可能有用)
2)如果您只需要更改打印的最后一行,则可以使用骗子方式。在输出中使用\ r(回车),光标将转到行的开头并覆盖那里的内容。