pygame用户输入在按回车后不呈现



我试图在python中创建一个世界克隆。我对pygame不是很有经验,我试图在屏幕上的小灰色框中设置用户输入。但是,我使用的代码要么不能正确地接受用户输入,要么不能正确地呈现它。(我怀疑是后者。)我看过代码教程,我引用了很多次,检查堆栈溢出的帖子,但似乎没有工作。我希望在键入并点击return之后,单词将显示出来,因为在键入时呈现的单词已被删除,以简化代码。如有任何帮助,不胜感激。

import random
import pygame
word_options = ("birth", "happy", "nancy")
word = random.choice(word_options).upper
WIDTH = 600
HEIGHT = 700
MARGIN = 10
T_MARGIN = 100
B_MARGIN = 100
LR_MARGIN = 100
GREY = (225, 227, 229)
GREY_FILLED = (120, 124, 126)
GREEN = (6,214,160)
YELLOW = (255, 209, 102)
INPUT = ""
GUESSES = ["berry"]
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
UNGUESSED = ALPHABET
GAME_OVER = False
pygame.init()
pygame.font.init()
pygame.display.set_caption("Nancdle")
SQ_SIZE = (WIDTH-4*MARGIN-2*LR_MARGIN) // 5
FONT = pygame.font.SysFont("free sans bold", SQ_SIZE)
FONT_SMALL = pygame.font.SysFont("free sans bold", SQ_SIZE//2)
#screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
white = (255, 255, 255)
#animation loop
animating = True
while animating:
screen.fill(white)
letters =FONT_SMALL.render(UNGUESSED, False, GREY_FILLED)
surface = letters.get_rect(center = (WIDTH//2, T_MARGIN//2))
screen.blit(letters, surface)

y = T_MARGIN
for i in range(6):
x = LR_MARGIN
for j in range(5):
square = pygame.Rect(x, y, SQ_SIZE, SQ_SIZE)
pygame.draw.rect(screen, GREY, square, 2)
#to add past letters/words
if i < len(GUESSES):
color = GREY_FILLED
pygame.draw.rect(screen, color, square)
letter = FONT.render(GUESSES[i][j], False, (255,255,255))
surface = letter.get_rect(center = (x+SQ_SIZE//2, y+SQ_SIZE//2))
screen.blit(letter, surface)
x += SQ_SIZE + MARGIN
y += SQ_SIZE + MARGIN


#to update screen
pygame.display.flip()
for event in pygame.event.get():
#so you can close window
if event.type == pygame.QUIT:
animating = False

elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if len(INPUT) == 5:
GUESSES.append(INPUT)
GAME_OVER = True if INPUT == ANSWER else false
INPUT = ""


elif len(INPUT) < 5 and not GAME_OVER:
INPUT = INPUT + event.unicode.upper()

如果问题措辞不当或问得不正确,我很乐意删除这篇文章。

您的代码只处理K_Return的事件,而不是任何其他键。这是由于elif len(INPUT) < 5 and not GAME_OVER:缩进太远造成的。

如果您按照以下方式更改事件处理程序代码,它应该可以工作:

elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if len(INPUT) == 5:
GUESSES.append(INPUT)
GAME_OVER = True if INPUT == word else False
INPUT = ""
elif len(INPUT) < 5 and not GAME_OVER:
INPUT = INPUT + event.unicode.upper()

注意我还必须将ANSWER替换为word并将false大写。

您使用两个空格进行缩进,如果您使用四个空格,可能会更容易识别范围不匹配。

使用event.unicode是相当健壮的,但是用户仍然可以添加数字和空格。如果你想循环遍历所有的字母键,但又不想使用像if event.key in [pygame.K_a, pygame.K_b, ...这样可怕的东西,你可以使用range,例如

elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if len(INPUT) == 5:
GUESSES.append(INPUT)
GAME_OVER = True if INPUT == ANSWER else False
INPUT = ""
elif event.key in range(pygame.K_a, pygame.K_z + 1):
# any other letter keys pressed
if len(INPUT) < 5 and not GAME_OVER:
INPUT = INPUT + event.unicode.upper()
elif event.key in [pygame.K_BACKSPACE, pygame.K_DELETE]:
INPUT = INPUT[:-1]  # don't need to check length

最新更新