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

作为一个初学者,我正在实现一个简单的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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值