3

I'm trying to make a simulation, and it needs 3 inputs from the user. The inputs I need are the X and Y dimensions for the display and a number of iterations.

def max_rows():
   while True:
       for event in pygame.event.get():
           if event.type==pygame.QUIT:
               pygame.quit()
               sys.exit()
           if event.type==pygame.KEYDOWN:
               if event.key==pygame.K_RETURN:
                   xmax=user_text
               if event.key==pygame.K_BACKSPACE:
                   user_text=user_text[:-1]
               else:
                   user_text+=event.unicode
       screen.fill((0,0,0))

       pygame.draw.rect(screen,color,input_rect,2)
       text_surface=base_font.render(user_text,True,(255,255,255))
       screen.blit(text_surface,(input_rect.x+5,input_rect.y+5))

       input_rect.w=max(100,text_surface.get_width()+10)

       pygame.display.flip()

I can get the input from the user, but I'm not sure how to use it or how to get the next inputs.

3
  • if you want to set display size then you should get it before pygame.display.set_mode and you could use input() before running pygame - but it needs to run in console. Eventually you could use argparse to run script with arguments python script.py val1 val2 val3 Commented Jul 29, 2021 at 12:59
  • maybe before while True you should set user_text = "" - and for K_RETURN use return user_text. And then you can run it as xmax = max_rows() Commented Jul 29, 2021 at 13:03
  • 1
    you could evenit for x = max_rows(), y = max_rows(), depth = max_rows() Commented Jul 29, 2021 at 13:09

1 Answer 1

1

There are mainly two ways of solving this problem:

1 Is to call inptut() before the screen is initialized (as suggested by the comments).

2 taking the input after the screen is initialized. can be done by using the KEYDOWN events:

import pygame

allowed_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
screen = pygame.display.set_mode((300, 300))

input_list = []

current_input = ''
while len(input_list) < 3:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit(0)
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                input_list.append(int(current_input))
                current_input = ''
            elif event.key == pygame.K_BACKSPACE:
                current_input = current_input[:-1]
            elif chr(event.key) in allowed_chars:
                current_input += chr(event.key)

# do something with your input

things you might want to add:

  • allowing the user to see what he has typed
pygame.font.init()
courir = pygame.font.SysFont('arial', 25)

# in the while loop:
screen.fill((255, 255, 255))
screen.blit(courir.render(current_input, False, (0, 0, 0), (30, 30))
pygame.display.flip()
  • add try - except statements around for the following cases:
    user hits return when nothing is entered
    user hits backspace when nothing is entered
    user hits a key that is not recognized by chr()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.