1

作为一个初学者,我正在实施一个简单版本的 Hangman。

在我的代码中,在 for 循环 ( for event in pygame.event.get():) 中创建的所有文本消息都不会显示在屏幕上。但是,人们可以看到,它们眨眼了几分之一秒,应该在哪里,但它们从未完全出现。

bif = "bg.jpg"

import pygame, sys, time
from pygame.locals import *

pygame.init()

screen_widht = 600
screen_height = 300
user_guess = ''
word = "fuzzy"
guesses = 3
hidden_word = '-'*(len(word))
list_of_indexes = []
valid_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',     'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

screen = pygame.display.set_mode((screen_widht, screen_height), 0, 32)
background = pygame.image.load(bif).convert()
font = pygame.font.Font(None, 20)
text_color = (10, 10, 10)


def find_indexes(s, ch):
    global list_of_indexes
    list_of_indexes = [i for i, ltr in enumerate(s) if ltr == ch]



while True:

    screen.blit(background, (0, 0))

    y = 10
    msg1 = font.render("Welcome to Hangman!", 1, text_color)
    screen.blit(msg1, (10, y))
    y += 20
    msg2 = font.render("The word now looks like this: " + hidden_word, 1, text_color)
    screen.blit(msg2, (10, y))
    y += 20
    msg3 = font.render("You have " + str(guesses) + " guesses left", 1, text_color)
    screen.blit(msg3, (10, y))
    y += 20 
    if guesses > 0 and '-' in hidden_word:
        msg4 = font.render("Input your guess", 1, text_color)
        screen.blit(msg4, (10, y))


    for event in pygame.event.get(): #there is the loop which does not blit messages on the screen
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYUP:
            key = event.key
            user_guess = chr(key)     
            if user_guess not in valid_letters: 
                msg5 = font.render("Input valid guess", 1, text_color)
                screen.blit(msg5, (10, 90))
            else:
                msg7 = font.render("Your guess is " + user_guess, 1, text_color)
                screen.blit(msg7, (10, 90))
        if event.type == KEYUP and user_guess not in word and guesses > 0:
            guesses -= 1
        if user_guess not in word:
            msg8 = font.render("There are no " + user_guess + "`s in this word", 1, text_color)
            screen.blit(msg8, (10, 110))       
        else:
            msg6 = font.render("Your guess is correct", 1, text_color)
            screen.blit(msg6, (10, 110))
            find_indexes(word, user_guess)
            while len(list_of_indexes) > 0:
                for ind in list_of_indexes:
                    hidden_word = hidden_word[:ind] + user_guess + hidden_word[ind+1:]
                    list_of_indexes.remove(ind)


    if guesses == 0 and '-' in hidden_word:
        msg9 = font.render("You are completely hung", 1, text_color)
        screen.blit(msg9, (10, 130))

    if not '-' in hidden_word:
        msg10 = font.render("You guessed the word", 1, text_color)
        screen.blit(msg10, (10, 130))



    pygame.display.update()

time.sleep(0.03)
4

2 回答 2

1

if块_

if user_guess not in word:
    msg8 = font.render("There are no " + user_guess + "`s in this word", 1, text_color)
    screen.blit(msg8, (10, 110))       
else:
    msg6 = font.render("Your guess is correct", 1, text_color)
    screen.blit(msg6, (10, 110))
    find_indexes(word, user_guess)
    while len(list_of_indexes) > 0:
        for ind in list_of_indexes:
            hidden_word = hidden_word[:ind] + user_guess + hidden_word[ind+1:]
            list_of_indexes.remove(ind)

是您的事件处理循环 ( for event in pygame.event.get():) 的一部分,这是错误的。

它缩进一个TAB到远。否则,仅当事件队列中有事件时才绘制文本。

于 2013-03-25T11:13:13.117 回答
0

您的问题是您在 for 循环中对消息图像进行了 blit 处理。events for 循环是一个循环,它处理 pygame 中的所有事件,例如单击屏幕或移动光标。因此,只有在 pygame 事件发生时,您的消息才会被传送到屏幕上。您可以通过在其中创建带有金条的变量来解决此问题。例如:

A=False
if user_guess not in valid_letters: 
     A=True

在代码下方的 for 循环之外,您可以编写:

if A == True:
    msg5 = font.render("Input valid guess", 1, text_color)
    screen.blit(msg5, (10, 90))

这是一种方法。可能有一种更有效的方法可以做到这一点,但如果你想让它留在屏幕上,你就不能在事件循环内对某些东西进行 blit。

于 2013-03-25T21:33:38.223 回答