For循环中断整个脚本



我试图建立一个世界克隆,一个文字游戏,其中一个有6次机会猜测一个5个字母的单词。游戏本身可以运行,但当我试图在游戏最后显示单词的定义时,我的整个游戏都卡住了。

我的代码是:

import pygame
import pygame.display
import sys
import random
from words import *
from PyDictionary import PyDictionary
pygame.init()

# a py file with list of words
CORRECT_WORD = WORDS
CORRECT_WORD_LIST = list(CORRECT_WORD)
word = random.choice(CORRECT_WORD_LIST)

# number of attempts, 6 in total
attempts = 0
# a list to store the previous guesses
guesses = [[]] * 6
cur_guess = []
cur_guess_str = ""
current_letter_bg_x = 110

game_result = ""
# get the meaning of the word
def get_meaning(word):
dicto = PyDictionary()
meaning = dicto.meaning(word)
if meaning == "":
meaning = "No definition found"
return meaning
meaning = get_meaning(word)

def blit_text(surface, text, pos, font):
"""Multi-line text blit from https://stackoverflow.com/a/42015712/2280890"""

def check_guess(guess):
# Check if letter is Green, Yellow or grey if not in word
# trimmed, for not being part of the problem

attempts += 1
cur_guess = []
cur_guess_str = ""
current_letter_bg_x = 110
def play_again():    
SCREEN.blit(BACKGROUND, BG_RECT)

play_again_text = play_again_font.render("ENTER to Play Again? or Q to Quit!", True, "#FCFCFC")

pygame.display.update()

word_text = play_again_font.render(f"{word.upper()}", True, "#FFB90F")

SCREEN.blit(play_again_text, play_again_rect)
SCREEN.blit(word_text, word_rect)
pygame.display.flip()

def reset():
# Reset all global variables

我已经修剪/删除了一堆与创建/绘制字母和退格等相关的线条。

在此之前,一切都按照预期进行。如果位置正确,字母会按预期的颜色显示。

我的问题从这里开始。它不是在6次尝试后才显示定义,而是在游戏一开始就显示含义。这是破坏游戏的代码块:

for i in enumerate(guesses[attempts]):
if i == 6 and game_result is not "W":
word = random.choice(CORRECT_WORD_LIST)
meaning = get_meaning(word)
txt = f"{meaning}"
txt_pos = (40, 70)
window.blit(BACKGROUND, [-317, -300])
f"{blit_text(window, txt, txt_pos, sys_font)}", True, "#FFB90F"
pygame.display.update

如果我省略这个block,游戏就会像预期的那样运行,如果我像下面那样离开,它就会像预期的那样运行。

游戏

while running:
if game_result != "":
play_again()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if game_result != "":
reset()
else:
if len(cur_guess_str) == 5 and cur_guess_str.lower() in WORDS:
check_guess(cur_guess)
elif event.key == pygame.K_BACKSPACE:
if len(cur_guess_str) > 0:
delete_letter()
else:
key_pressed = event.unicode.upper()
if key_pressed in "QWERTYUIOPASDFGHJKLZXCVBNM" and key_pressed != "":
if len(cur_guess_str) < 5:
new_letter()

有人能帮我理解为什么我的逻辑/推理是有缺陷的吗?导致问题的代码块我将其插入### game之后,在while running:之间和 之前
if game_result != "":
play_again()
while True:
meaning = ""
for i in range(len(guesses[attempts])):
if i == 6 and game_result is not "W":
word = random.choice(CORRECT_WORD_LIST)
meaning = get_meaning(word)

txt = f"{meaning}"
txt_pos = (40, 70)
window.blit(BACKGROUND, [-317, -300])
f"{blit_text(window, txt, txt_pos, sys_font)}", True, "#FFB90F"
pygame.display.update()

对于上面的代码,你在运行时放置,但你从来没有改变运行,所以当它为True不是更好吗?另外,如果您设置while为True,结束游戏的更好方法是执行break而不是pygame.quit()和sys.exit()。当游戏中断时,我还可以看到打印的输出吗?

不工作(我已经编辑了代码,它打印,因为一旦游戏开始运行,你已经把txt=f"{含义}"只有当i == 6且game_result不是"W"时才会发生这种情况。我不知道这是否有效,但值得一试。

如果这不起作用,从我可以看到你不使用枚举,所以我会用range(len(guess [attempts]))代替它

最新更新