【发布时间】:2015-09-29 02:47:11
【问题描述】:
我有这个刽子手游戏,一切都完成了,但是在我赢或输后它会终止程序。我正在尝试在其中加入一个(虽然没有完成)声明,但似乎无法让它发挥作用。非常感谢一些帮助!这是下面的代码,这是第一部分:
import drawHangman
import turtle
import random
def main():
#Do not change or remove code from herel
window = turtle.Screen()
window.setup(400, 400, 200, 200)
HG = turtle.Turtle()
drawHangman.default(HG)
print(" Welcome to the HangMan game!!")
print(" You will have six guesses to get the answer correct.")
print(" If you reach the limit of six wrong guess, it will be GAME OVER!")
print(" Below you will see how many letter are in the word,\n","for eveyone you get right the line will be replaced with the letter.")
lines = open("../WordsForGames.txt").read()
line = lines[0:] #lines 21-24 Randomly generate a word from a text file
words = line.split()
myword = random.choice(words)
#print(myword)# this print the random word
done = False
words = myword
guessed = '_'*len(myword)
guessedLetters = ""
guesses = 0
correctGuess = 0
print(guessed)#this prints the blank lines.
while(not done):
while correctGuess != len(myword):
guess = input("Enter a letter you would like to guess: ")
guessed = list(guessed) #This will convert fake to a list, so that we can access and change it.
if len(guess) == 1 and guess.isalpha():
if guessedLetters.find(guess) != -1:
print("(",guess,")letter has already been picked")
else:
guessedLetters = guessedLetters + guess
index1 = myword.find(guess)
if index1 == -1:
print("The letter(", guess,")is not a correct letter in the word", ''.join(guessed))
guesses = guesses + 1
print("You have guessed(", guesses,")times")
print("Remember that you only get 6 guesses!")
if guesses == 1:
drawHangman.drawHead(HG)
elif guesses == 2:
drawHangman.drawBody(HG)
elif guesses == 3:
drawHangman.drawRightArm(HG)
elif guesses == 4:
drawHangman.drawLeftArm(HG)
elif guesses == 5:
drawHangman.drawRightLeg(HG)
elif guesses == 6:
drawHangman.drawLeftLeg(HG)
print("You reached your limit of 6 guesses, GAME OVER! The word was ("+ myword + ").")
break
else:
correctGuess = correctGuess + myword.count(guess)
print("The letter(",guess,")is in the word")
for ch in range(0, len(myword)):#For statement to loop over the answer (not really over the answer, but the numerical index of the answer)
if guess == myword[ch]:
guessed[ch] = guess #change the fake to represent that, EACH TIME IT OCCURS
print(''.join(guessed))
print("The letter(",guess ,")was in the word. Great job keep going")
if correctGuess != len(myword):
print("You have guessed wrong(", guesses,")times!.")
print("Remember that you only get 6 guesses!")
elif guesses <= 0:
print("You reached your limit of 6 guesses, GAME OVER! The word was ("+ myword + ").")
break
else:"Guess any letter you want!"
if correctGuess == len(myword):
print("Congratulations! You won!")
if (x == "n"):
input("Would you like to play again?")
done = True
else:
drawHangman.reset(HG)
main()
这是代码的第二部分,它绘制了所有的东西,比如头部、身体、手臂、腿:
def default(babbage):
#Start drawing stand
babbage.penup()
babbage.setpos(0,-50)
babbage.pendown()
babbage.back(100)
babbage.fd(50)
babbage.left(90)
babbage.forward(175)
babbage.right(90)
babbage.forward(50)
babbage.right(90)
babbage.forward(25)
babbage.right(90)
#End drawing stand
def drawHead(babbage):
babbage.pencolor("red")
babbage.circle(15)
babbage.penup()
babbage.left(90)
babbage.forward(30)
babbage.pendown()
def drawBody(babbage):
babbage.forward(65)
babbage.back(40)
babbage.right(90)
def drawRightArm(babbage):
babbage.forward(30)
babbage.right(180)
babbage.forward(30)
def drawLeftArm(babbage):
babbage.forward(30)
babbage.back(30)
def drawRightLeg(babbage):
#Move to lower body
babbage.right(90)
babbage.forward(40)
#Draws the leg
babbage.right(45)
babbage.forward(40)
babbage.right(180)
babbage.forward(40)
babbage.right(90)
def drawLeftLeg(babbage):
babbage.forward(40)
def reset(babbage):
babbage.reset()
default(babbage)
【问题讨论】:
-
嗯...从哪里开始?首先,你在哪里定义
n?
标签: python while-loop