Hang子手游戏在Python中 - 我的休息似乎只在其他时间玩游戏
正如标题所示,游戏运行正常,但似乎我有一个我无法弄清楚的错误。Hang子手游戏在Python中 - 我的休息似乎只在其他时间玩游戏
第一场比赛结束后,当它询问你是否想再次参加比赛时,它会继续进行下一场比赛,你可以参加比赛,最后在比赛结束后,它不问你是否想要重新玩,但自动开始一个新的游戏,用刚刚在前一个游戏中出现的单词。
我找不出它是我的play_again函数还是我的check_win函数或什么。
感谢您的帮助
import random
def main():
hangman_word = generate_random()
while True:
play_game(hangman_word)
if play_again() == 2:
return False
break
else:
hangman_word = generate_random()
play_game(hangman_word)
############################################
# The game!
############################################
def play_game(random_word):
clear()
guesses = ""
tries = 9
while True:
winning_number = 0
hangmanInterface(tries)
print("WORD:", end="")
for char in random_word:
if char in guesses:
print(" {} ".format(char), end="")
else:
print(" _ ", end="")
winning_number += 1
if win_check(winning_number, tries) == True:
break
print_x(1)
print(" Tried:", end="")
for j in range(len(guesses)):
print("[{}]".format(guesses[j]), end="")
spacing_fix(2)
guess = get_guess()
guesses += guess
print_x(2)
if guess not in random_word:
tries -= 1
return
##########################################
##########################################
# generates a random word
##########################################
def generate_random():
#opens the dictionary and initializes a random word
with open("dict.txt") as fp:
dictwords = []
for line in fp:
dictwords.append(line.rstrip("\n"))
#makes sure the word is in lower case
rand = random.choice(dictwords)
randlower = rand.lower()
return randlower
##########################################
##########################################
# Checks if the game should restart
##########################################
def play_again():
print("Play again?")
print("1. yes!")
print("2. no :(")
ans = get_ans()
return ans
##########################################
def get_ans():
ans1 = input()
ans = int(ans1)
if ans == 1 or ans == 2:
return ans
else:
print("Please type 1 or 2")
get_ans()
##########################################
##########################################
# checks for the win
##########################################
def win_check(a, b):
if a == 0:
print_x(2)
print("######################")
print("# #")
print("# W I N N E R #")
print("# #")
print("######################")
return True
elif b == 0:
print_x(2)
print("######################")
print("# #")
print("# L O S E R #")
print("# #")
print("######################")
return True
##########################################
##########################################
# gets users guess while ensuring only
# one alpha, lowercase char is entered
##########################################
def get_guess():
get = input(" Guess: ")
a = get.lower()
if len(a) > 1:
print("One letter only")
get_guess()
elif not a.isalpha():
print("One letter only")
get_guess()
return a
##########################################
##########################################
# Aesthetic Functions
##########################################
def clear():
for i in range(25):
print ('\n')
#########################
def print_x(x):
for i in range(x):
print("\n")
#########################
def spacing_fix(tmp):
if tmp == 0:
tmp = 1
return tmp
if tmp == 2:
print()
tmp = 3
return tmp
##########################################
##########################################
# prints board state
##########################################
def hangmanInterface(index):
if index==0:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /|\ | ')
print(' /\ | ')
print(' | ')
print(' ________|_')
return
if index==1:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /|\ | ')
print(' / | ')
print(' | ')
print(' ________|_')
return
if index==2:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /|\ | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==3:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' /| | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==4:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' | | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==5:
clear()
print(' _____ ')
print(' | | ')
print(' O | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==6:
clear()
print(' _____ ')
print(' | | ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==7:
clear()
print(' _____ ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==8:
clear()
print(' ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' | ')
print(' ________|_')
return
if index==9:
clear()
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ________|_')
return
##########################################
if __name__ == "__main__":
main()
问题是这样的循环:
while True:
play_game(hangman_word)
if play_again() == 2:
return False
break
else:
hangman_word = generate_random()
play_game(hangman_word)
这个循环的起始和play_game(...)
结束,所以你让用户打两场比赛不要求输入插图中。拨打play_game
就足够了。
谢谢我不知道我是如何错过了!感谢您的帮助 – martyworm
取而代之的是:
def main():
hangman_word = generate_random()
while True:
play_game(hangman_word)
if play_again() == 2:
return False
break
else:
hangman_word = generate_random()
play_game(hangman_word)
试试这个:
def main():
while True:
hangman_word = generate_random() ## get ONE word
play_game(hangman_word) ## play ONE game
if play_again() == 2: ## if user doesn't want to play anymore, quit
break ## you could also return False,
## just don't do both because only one will be called
你正在做在原来什么: 当main()
首次调用,你玩过零级的游戏。 在第一个play_game(...)
直接在while True:
下,你正在玩你的第一场比赛(第一场比赛)。之后,如果play_again()
为1,则表示正在玩第二个hang子手游戏(在else:
子句下)。在这两场比赛之后,您将循环回到顶端并再次在while True:
之下玩游戏。
实际上,原始游戏只能玩奇数游戏(1,3,5,7,...)。
谢谢我不知道我是如何错过的!感谢你的帮助 – martyworm
考虑接受答案,如果你发现它有帮助 –