PyGame - 无法使用blit()调用在屏幕上提供文本

时间:2013-03-25 10:23:24

标签: python pygame

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

在我的代码中,在for-loop(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)

2 个答案:

答案 0 :(得分: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 。否则,如果事件队列中有事件,则只绘制文本

答案 1 :(得分:0)

你的问题是你在for循环中blit消息图像。循环事件是一个循环,它处理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。