私は完全な初心者としてハングマンの簡易版を実装しています。
私のコードでは、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)