【发布时间】:2021-01-05 21:04:50
【问题描述】:
我是 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?的代码表单,但你甚至没有尝试找到解决方案。这个问题是那种“你能帮我做吗”。 Stack Overflow 不是代码编写服务。试一试,如果有任何问题,请回来。
-
我什至想过帮助你,但是当我想到一个答案时,我意识到我必须为你实现整个应用程序。对于单个 Stack Overflow 答案来说,这将是一项巨大的努力,而且过于广泛。如果你能展示你的尝试和你在哪里挣扎,那么指出错误会更容易。
-
我在开始之前在文本框脚本顶部导入了 Typng_Questions 模块。当我运行它时,只有 Typing_Questions 模块运行。文本框模块没有运行。
-
我试试看。这需要几分钟...
-
我已经添加了答案。但是,有很多其他答案的链接。
标签: python merge pygame python-module pygame-surface