I'm a beginner in python and am currently using pygame. I'm making a game which requires user input. I have an algorithm called "Typing_Questions" and it has conditions on what the score and lives for the user are if they make mistakes. This algorithm runs correctly. It runs the script on the bottom of pygame so it doesn't load a separate screen.
Typing_Questions:
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)
I also have an algorithm for the input text box. This lets me type anything and loads a separate screen. This also works correctly.
Textbox:
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()
My aim is to merge these two modules together. I tried importing the other into one's module but only the Typing_Questions will run and the screen won't load for the textbox module and vice versa.
I want the textbox screen to display so the user can enter their answer from there. When they answer it in the box, I want the program to follow the script from the Typing_Questions algorithm (so it will only accept the answer or it will decrease a life or end the program if necessary).
Is there a way to do this? Thank you.