我正在创建游戏子手,我希望能够记录哪些字母是正确的,以及多少字母是不正确的,然后在以后使用它在屏幕上写消息并结束游戏。我该怎么做?
当前,correct_guesses
和incorrect_guesses
都是列表,并且每次有正确或不正确的猜测时,都应将猜测追加到其列表中。
import turtle
import string
import random
import time
words = ["gypsy"]
letter_width = 25
space_width = 15
incorrect_guesses = []
correct_guesses = []
def create_turtle():
global tori
tori = turtle.Turtle()
tori.shape('classic')
tori.pensize(5)
tori.speed(.5)
def play_game():
create_turtle()
choose_word()
draw_scaffold()
draw_hints()
draw_blanks()
listen_for_keypress()
def report_incorrect_guess(letter):
if letter not in incorrect_guesses:
body_parts[len(incorrect_guesses)]()
incorrect_guesses.append(letter)
tori.penup()
x = -270 + len(incorrect_guesses * letter_width)
y = 200
tori.goto(x, y)
tori.color("black")
tori.write(letter, font=("Times New Roman", 33, "normal"))
def report_correct_guess(letter):
tori.penup()
tori.color("black")
for i in range(len(word)):
if word[i] == letter:
offset = i * (letter_width + space_width)
tori.goto(-245 + offset, -242)
tori.write(letter, font=("Times New Roman", 33, "normal"))
def listen_for_keypress():
screen = turtle.Screen()
for letter in string.ascii_lowercase:
screen.onkeypress((lambda l: lambda: on_key_press(l))(letter), letter)
screen.listen()
screen.mainloop()
def choose_word():
global word
word = random.choice(words)
len(word)
def end_game(message):
tori.pensize(50)
tori.color("red")
tori.penup()
tori.goto(-150, 0)
tori.pendown()
tori.write(message)
time.sleep(3)
exit(0)
def on_key_press(letter):
if letter in word:
report_correct_guess(letter)
if correct_guesses == len(word):
end_game("YOU WIN! :)")
elif letter not in word:
report_incorrect_guess(letter)
if incorrect_guesses == 9:
end_game("YOU LOSE! X_X")
现在,游戏运行良好,直到结束。正确猜出所有字母后,游戏就会继续进行,您可以猜出更多不正确的字母,但是Turtle只会覆盖正确的字母。绘制完所有身体部位后,游戏会继续进行,但是会显示一条错误消息,指出这两个部位(特别是所指的部位)都有问题。
def report_incorrect_guess(letter):
if letter not in incorrect_guesses:
body_parts[len(incorrect_guesses)]() <-----
incorrect_guesses.append(letter)
tori.penup()
x = -270 + len(incorrect_guesses * letter_width)
y = 200
tori.goto(x, y)
tori.color("black")
tori.write(letter, font=("Times New Roman", 33, "normal"))
def on_key_press(letter):
if letter in word:
report_correct_guess(letter)
if correct_guesses == len(word):
end_game("YOU WIN! :)")
elif letter not in word:
report_incorrect_guess(letter) <------
if incorrect_guesses == 9:
end_game("YOU LOSE! X_X")
答案 0 :(得分:0)
为防止游戏继续到某一点,可以在游戏结束时使用变量gameOver
,让gameOver = true
结束。在您的listen_for_keypress()
函数中,您的第一个测试应为if !gameOver
。这样,游戏只能在gameOver = false
时运行。
def on_key_press(letter):
if letter in word:
report_correct_guess(letter)
if correct_guesses == len(word):
gameOver = true
end_game("YOU WIN! :)")
elif letter not in word:
report_incorrect_guess(letter)
if incorrect_guesses == 9:
gameOver = true
end_game("YOU LOSE! X_X")
def listen_for_keypress():
if !gameOver :
screen = turtle.Screen()
for letter in string.ascii_lowercase:
screen.onkeypress((lambda l: lambda: on_key_press(l))(letter), letter)
screen.listen()
screen.mainloop()