如何解决有没有办法在其中带有文本框以及文本输入框?
因此,我正在尝试使用pygame制作基于文本的游戏,并且正在测试两个教程。一个谈论如何创建一个用于容纳文本的框,另一个谈论向我展示如何创建一个文本输入框。现在,我试图结合这两个方面来创建同时具有这两个方面的游戏,但是每当我尝试运行它时,都会打开一个运行该窗口的窗口,然后我收到一条通知,指出该窗口没有响应。我该如何做才能使其在带有宽恕文本框和文本输入框的情况下正常运行游戏?
注意:游戏的目的是让用户在提示部分中找到错误并键入正确的答案,因此提示元素中的错误是有意的。
import pygame
pygame.init()
green = (0,255,0)
blue = (0,128)
white = (255,255)
prompts = ["hOW are you doing","I writed it using a pencal.","My dog ate the bone!.","8+1 = 7","Please stop!!","Here's a challenge; That movie was terribly bad!","I go home now meester person.","I' d like to stay here.","umm...hello?","[darn it!!! I think I forget that I am Robin","After I sated down I wanted to eating.","I can see the Firetruck!?","Is the usa a countery."]
answers = ["How are you doing?","I wrote it using a pencil.","My dog ate the bone!","8+1 = 9","Here's a challenge: That movie was bad!","I am going to go home now,mister person.","I'd like to stay here.","Darn it!!! I think I forgot that I am Robin.","After I sat down,I wanted to eat","I can see the firetruck!","Is the U.S.A a country?"]
score = 0
screen = pygame.display.set_mode((640,480))
def main():
font = pygame.font.Font(None,32)
clock = pygame.time.Clock()
input_box = pygame.Rect(100,100,140,32)
color_inactive = pygame.Color('lightskyblue3')
color_active = pygame.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if input_box.collidepoint(event.pos):
# Toggle the active variable.
active = not active
else:
active = False
# Change the current color of the input box.
color = color_active if active else color_inactive
if event.type == pygame.KEYDOWN:
if active:
if event.key == pygame.K_RETURN:
print(text)
if text == answers[0]:
print("correct!")
del answers[0]
del prompts[0]
text = ''
else:
print("wrong")
text = ''
del answers[0]
del prompts[0]
elif event.key == pygame.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
screen.fill((30,30,30))
# Render the current text.
txt_surface = font.render(text,True,color)
# Resize the box if the text is too long.
width = max(200,txt_surface.get_width()+10)
input_box.w = width
# Blit the text.
screen.blit(txt_surface,(input_box.x+5,input_box.y+5))
# Blit the input_box rect.
pygame.draw.rect(screen,color,input_box,2)
pygame.display.flip()
clock.tick(30)
font2 = pygame.font.Font('freesansbold.ttf',32)
# create a text suface object,# on which text is drawn on it.
text2 = font2.render(prompts[0],green,blue)
# create a rectangular object for the
# text surface object
textRect = text2.get_rect()
# set the center of the rectangular object.
textRect.center = (200,200)
while True:
screen.fill(white)
screen.blit(text2,textRect)
if __name__ == '__main__':
pygame.init()
main()
pygame.quit()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。