Python 如何在pygame上设置输入文本框模块的条件?

Python 如何在pygame上设置输入文本框模块的条件?,python,merge,pygame,python-module,pygame-surface,Python,Merge,Pygame,Python Module,Pygame Surface,我是python的初学者,目前正在使用pygame。我正在制作一个需要用户输入的游戏。我有一个叫做“键入问题”的算法,它有一个条件,即如果用户出错,他们的分数和寿命是多少。该算法运行正确。它在pygame的底部运行脚本,因此不会加载单独的屏幕 键入问题: Score = 0 Lives = 3 Question_4 = "Code print" Answer = "print" print(T_Question_4) user_input = input(

我是python的初学者,目前正在使用pygame。我正在制作一个需要用户输入的游戏。我有一个叫做“键入问题”的算法,它有一个条件,即如果用户出错,他们的分数和寿命是多少。该算法运行正确。它在pygame的底部运行脚本,因此不会加载单独的屏幕

键入问题:

Score = 0
Lives = 3
Question_4 = "Code print"
Answer = "print"
print(T_Question_4)
user_input = input("Enter: ")

if user_input == Answer:
   Lives = Lives
   Score = Score + 1
   print(Lives)
   print(Score)
   print("Well Done!")

while user_input != Answer:
   user_input = input()
   if user_input == Answer:
       Lives = Lives
       Score = Score + 1
       print(Lives)
       print(Score)
       print("Well Done!")
   else:
       Lives = Lives - 1
       Score = Score - 1
       print(Lives)
       if Score == -1:
          Score = 0
          print(Score)
       if Lives == 0:
          print("Game Over")
          quit()
       else:
          print("Try again.")
          print(T_Question_4)
我还有一个输入文本框的算法。这让我可以键入任何内容并加载一个单独的屏幕。这也是正确的

文本框:

import pygame as pg
def main():
    screen = pg.display.set_mode((1275, 775))
    font = pg.font.Font(None, 32)
    clock = pg.time.Clock()
    input_box = pg.Rect(400, 300, 142, 32)
    color_inactive = pg.Color('lighskyblue3')
    color_active = pg.Color('dodgerblue2')
    color = color_inactive
    active = False
    text = ''
    done = False

   while not done:
      for event in pg.event.get():
          if event.type == pg.QUIT:
             done = True
          if event.type == pg.MOUSEBUTTONDOWN:
             if input_box.collidepoint(event.pos):
                 active = not active
             else:
                 active = False
             color = color_active if active else color_inactive
          if event.type == pg.KEYDOWN:
             if active:
                if event.key == pg.K_RETURN:
                   print(text)
                   text = ''
                elif event.key == pg.K_BACKSPACE:
                   text = text[:-1]
                else:
                   text += event.unicode

       screen.fill(250, 250, 250)
       txt_surface = font.render(text, True, color)
       width = max(200, txt_surface.get_width()+10)
       input_box.w = width
       screen.blit(txt_surface, (input_box.x + 5, input_box.y + 5))
       pg.draw.rect(screen, color, input_box, 2)

       pg.display.flip()
       clock.tick(30)

if __name__ == '__main__':
   pg.init()
   main()
   pg.quit()
我的目标是将这两个模块合并在一起。我尝试将另一个导入到一个人的模块中,但只有输入问题会运行,文本框模块的屏幕不会加载,反之亦然

我希望文本框屏幕显示,以便用户可以输入他们的答案从那里。当他们在框中回答时,我希望程序遵循输入问题算法中的脚本(因此它将只接受答案,否则将减少寿命或在必要时结束程序)


有办法做到这一点吗?谢谢。

请使用答案中的示例作为应用程序的基础。该示例包含一个实现文本输入框的完整类。
您必须实现一个应用程序循环。有关实现应用程序循环的更多信息,请参阅的答案

典型的PyGame应用程序循环必须:

  • 通过或处理事件
  • 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
  • 清除整个显示或绘制背景
  • 绘制整个场景(
    blit
    所有对象)
  • 通过或更新显示
列一张问题清单和问题的答案清单。为
分数
寿命
当前问题
指定变量。例如:

问题=[“问题1”、“问题2”、“问题3”]
答案=[“答案1”、“答案2”、“答案3”]
当前问题=0
生命=3
分数=0
使用应用程序循环从文本输入字段获取输入。评估结果并相应地更改
得分
寿命
当前问题
的变量:

运行时:
# [...]`
如果寿命>0且当前问题
查看并生成文本输出。输入:

game\u over\u surf=font100.render(“game over”,True,(255,255))
blit(game-over-surf,game-over-surf.get-rect(center=window.get-rect().center))

完整示例:

导入pygame
类文本输入框(pygame.sprite.sprite):
定义初始化(self,x,y,w,font):
super()。\uuuu init\uuuuu()
self.color=(255、255、255)
self.backcolor=None
self.pos=(x,y)
自宽=w
self.font=font
self.active=True
self.text=“”
self.render_text()
def渲染_文本(自身):
t\u surf=self.font.render(self.text、True、self.color、self.backcolor)
self.image=pygame.Surface((最大值(self.width,t_surf.get_width()+10),t_surf.get_height()+10),pygame.SRCALPHA)
如果self.backcolor:
self.image.fill(self.backcolor)
self.image.blit(t_surf,(5,5))
pygame.draw.rect(self.image,self.color,self.image.get_rect().充气(-2,-2),2)
self.rect=self.image.get_rect(左上角=self.pos)
def重置(自):
self.text=“”
self.active=True
self.render_text()
def更新(自身、事件列表):
对于事件列表中的事件:
如果event.type==pygame.KEYDOWN和self.active:
如果event.key==pygame.K_返回:
self.active=False
elif event.key==pygame.K_退格:
self.text=self.text[:-1]
其他:
self.text+=event.unicode
self.render_text()
问题=[“问题1”、“问题2”、“问题3”]
答案=[“答案1”、“答案2”、“答案3”]
当前问题=0
生命=3
分数=0
pygame.init()
window=pygame.display.set_模式((500500))
clock=pygame.time.clock()
font100=pygame.font.SysFont(无,100)
font50=pygame.font.SysFont(无,50)
文本输入框=文本输入框(20、170、460、100)
group=pygame.sprite.group(文本输入框)
运行=真
运行时:
时钟滴答(60)
event_list=pygame.event.get()
对于事件列表中的事件:
如果event.type==pygame.QUIT:
运行=错误
如果寿命>0且当前问题