#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0

    Help getting pygame to allow text input


    I am using Python 3.3.1 for reference.

    So I am working on simple quiz game to use for review in my classroom. The program references an external text file to get categories, questions and answers for the game.
    Right now I have the program referencing a specific text file, but I would like to be able to select different text files so I can change the game for different classes without overwriting the question text file. So I thought I would give a prompt that would allow the user to type in the name of the file they want to use. Since I am using a pygame window for this, something like file = input("Enter your file here...") won't work (unless there is something I haven't found yet).

    The solution I came up with is this:

    python Code:
     
    import pygame
     
    black    = (   0,   0,   0)
    white    = ( 255, 255, 255)
    green    = (   0, 255,   0)
    red      = ( 255,   0,   0)
     pygame.init() 
    size = [700,500]
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("My Game")
    done = False
    clock = pygame.time.Clock()
    font = pygame.font.Font('freesansbold.ttf', 35)
    text = font.render("",True,black)
    currenttext = ""
    finished = False
    # -------- Main Program Loop -----------
    while done == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                done = True 
            elif event.type == pygame.KEYDOWN:
                if len(currenttext) <= 20:
                    if event.key == pygame.K_a:
                        currenttext += "a"
                        text = font.render((currenttext),True,black)
                    elif event.key == pygame.K_b:
                        currenttext += "b"
    # and so on for the rest of the alphabet and numbers
    # followed by  
                        text = font.render((currenttext),True,black)
                    elif event.key == pygame.K_BACKSPACE:
                        currenttext = currenttext[:len(currenttext)-1]
                        text = font.render((currenttext),True,black)
                    elif event.key == pygame.K_RETURN:
                        finished = True
     
    # the text is later blit'ed further down to 
    # show what has been typed thus far
    # and if the user hits RETURN, it would use that 
    # currenttext string to figure out what file it needs to read


    So yeah, I made an event for each key on the keyboard to add a character to the string variable of what the user types. It works, it just is... inelegant. Surely there must be an easier way to do that, but I have been unable to find it or figure it out. I also am unsure how to make events for a lot of the symbol keys like "." if they were typing in "question.txt" for example. Right now I am working around it by just adding the ".txt" to the input

    I suppose ideally, I would come up with something that would display a list of .txt files that happen to be sitting in the program's directory and allow the user to pick from those, but I dont even know where to begin coding that. If anyone can point me to either of those solutions (an easier way to input text for the user, or somehow displaying a list of the files in the current directory), it would be much appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    17
    I'm afraid if you want to do it inside pygame, there is no simple solution. You need to stick to the available pygame methods. There are some text processing utilities for pygame (look at the libraries section of the pygame projects) that may be helpful. As a workaround you could do the input in a command line window before initializing pygame.
    For the evaluation of key presses, you could use the unicode attribute of the keydown event for keys other than BACKSPACE and RETURN.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Could you mix a tkinter window with pygame?
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    I have never used ktinker before, but I suppose it is worth a study.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    how would I use the unicode? look up the code for the specific character and then put in pygame."unicode_character_here" ?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    17

    Thumbs down


    e.g. like so:
    python Code:
     
    ...
        if event.key == pygame.K_BACKSPACE:
            ...
        elif event.key == pygame.K_RETURN:
            ...
        else:
            currenttext += event.unicode
    ...
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    Japan
    Posts
    20
    Rep Power
    0
    oh wow that looks perfect. thanks! Gosh. That is SO much better than what I had come up with.

IMN logo majestic logo threadwatch logo seochat tools logo