微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在 pygame 上为输入文本框模块设置条件?

我是 Python 初学者,目前正在使用 pygame。我正在制作一个需要用户输入的游戏。我有一个名为“Typing_Questions”的算法,它对用户犯错时的得分和生活有条件。该算法运行正确。它在 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()

我的目标是将这两个模块合并在一起。我尝试将另一个导入到一个模块中,但只有 Typing_Questions 会运行,并且不会为文本框模块加载屏幕,反之亦然。

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

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

解决方法

使用答案中的示例How to create a text input box with pygame? 为您的应用程序奠定基础。该示例包含一个实现文本输入框的完整类。
您必须实现一个应用程序循环。有关实现应用程序循环的更多信息,请参阅对 Why is my PyGame application not running at all? 的回答。

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

制作问题清单和问题的答案清单。为 scorelivescurrent_question 指定变量。例如:

questions = ["question 1","question 2","question 3"]
answer = ["answer 1","answer 2","answer 3"]
current_question = 0 
lives = 3
score = 0

使用应用程序循环从文本输入字段获取输入。评估结果并相应地更改 scorelivescurrent_question 的变量:

while run:
    # [...]`

    if lives > 0 and current_question < len(questions): 
        group.update(event_list)
        if not text_input_box.active:
            if text_input_box.text == answer[current_question]:
                score += 1
                current_question += 1
            else:
                lives -= 1
            text_input_box.reset()

参见 Python display text with font & color? 并生成文本输出。对于输入:

game_over_surf = font100.render("Game Over",True,(255,255,255))
window.blit(game_over_surf,game_over_surf.get_rect(center = window.get_rect().center))

完整示例:

import pygame

class TextInputBox(pygame.sprite.Sprite):
    def __init__(self,x,y,w,font):
        super().__init__()
        self.color = (255,255)
        self.backcolor = None
        self.pos = (x,y) 
        self.width = w
        self.font = font
        self.active = True
        self.text = ""
        self.render_text()

    def render_text(self):
        t_surf = self.font.render(self.text,self.color,self.backcolor)
        self.image = pygame.Surface((max(self.width,t_surf.get_width()+10),t_surf.get_height()+10),pygame.SRCALPHA)
        if self.backcolor:
            self.image.fill(self.backcolor)
        self.image.blit(t_surf,(5,5))
        pygame.draw.rect(self.image,self.image.get_rect().inflate(-2,-2),2)
        self.rect = self.image.get_rect(topleft = self.pos)

    def reset(self):
        self.text = ""
        self.active = True
        self.render_text()

    def update(self,event_list):
        for event in event_list:
            if event.type == pygame.KEYDOWN and self.active:
                if event.key == pygame.K_RETURN:
                    self.active = False
                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                self.render_text()

questions = ["question 1","answer 3"]
current_question = 0 
lives = 3
score = 0

pygame.init()
window = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
font100 = pygame.font.SysFont(None,100)
font50 = pygame.font.SysFont(None,50)

text_input_box = TextInputBox(20,170,460,font100)
group = pygame.sprite.Group(text_input_box)

run = True
while run:
    clock.tick(60)
    event_list = pygame.event.get()
    for event in event_list:
        if event.type == pygame.QUIT:
            run = False

    if lives > 0 and current_question < len(questions): 
        group.update(event_list)
        if not text_input_box.active:
            if text_input_box.text == answer[current_question]:
                score += 1
                current_question += 1
            else:
                lives -= 1
            text_input_box.reset()

    window.fill(0)
    
    if lives == 0:
        game_over_surf = font100.render("Game Over",255))
        window.blit(game_over_surf,game_over_surf.get_rect(center = window.get_rect().center))
    elif current_question < len(questions): 
        question_surf = font100.render(questions[current_question],255))
        window.blit(question_surf,(20,50))
        group.draw(window)
    else:
        well_done_surf = font100.render("Well Done!",255))
        window.blit(well_done_surf,well_done_surf.get_rect(center = window.get_rect().center))

    score_surf = font50.render("score: " + str(score),255))
    window.blit(score_surf,400))
    lives_surf = font50.render("lives: " + str(lives),255))
    window.blit(lives_surf,(300,400))

    pygame.display.flip()

pygame.quit()
exit()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。