[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Is there an InputBox function in pygame/python?
Thanks! I've put that in (and changed it a little, re: the underscores
and so on, will change the color too). But one last thing: This
ignores the shift key in the keypresses:
inkey = get_key()
if inkey == K_BACKSPACE:
current_string = current_string[0:-1]
elif inkey == K_RETURN:
break
elif inkey == K_MINUS:
current_string.append("_")
elif inkey <= 127:
current_string.append(chr(inkey))
I'm not too sure how the shift key works in keypresses (it doesnt
matter for any of my other keystrokes in the game). Is there an "elif
inkey ==" line I can add that will let me use shift keys when entering
in the player name? As is, shift+s just returns s (rather than S). My
tutorial materials don't have anything useful about the shift key...?
~Denise
On 4/27/05, James Reeves <jreeves@xxxxxxxxxxxxxxxxxxx> wrote:
> On Wednesday 27 Apr 2005 10:13 pm, D. Hartley wrote:
> > I am trying to create a box on the graphics window which asks a user
> > for their name. My research led me to the "InputBox" function, which
> > is said to "get user input, allowing backspace etc shown in a box in
> > the middle of the screen" (but ignores the shift key?). I found
> > several pages that refer to it, and one example on google, but it
> > seems very cryptic to me, and I can't find any other help materials.
> > I tried help(InputBox), tried to use the function itself, tried to
> > search for it in the documentation on python.org, and tried to google
> > it (InputBox, Python). I got a half-million websites talking about
> > VBS and other versions of InputBox, and can't find one (besides the
> > cryptic one) for python. Does this function exist? Or is it called
> > something else? Can someone point me to an
> > explanation/definition/simple example? Like I said, I am just trying
> > to ask for a user name, it is nothing more (or less!) complicated than
> > that.
>
> InputBox isn't a function native to Pygame. Pygame doesn't have any GUI
> widgets included; you have to write them yourself, or find one that's written
> already. I do recall there have been some unofficial GUI toolkits written for
> pygame, but I haven't used them (and in my opinion, they all seem a little
> ugly).
>
> However, I found this:
>
> http://www.pygame.org/pcr/inputbox/index.php
>
> And this gives you a simple input box. To put this into your game, copy and
> paste the code below:
>
> #######################
> def get_key():
> while 1:
> event = pygame.event.poll()
> if event.type == KEYDOWN:
> return event.key
> else:
> pass
>
> def display_box(screen, message):
> "Print a message in a box in the middle of the screen"
> fontobject = pygame.font.Font(None,18)
> pygame.draw.rect(screen, (0,0,0),
> ((screen.get_width() / 2) - 100,
> (screen.get_height() / 2) - 10,
> 200,20), 0)
> pygame.draw.rect(screen, (255,255,255),
> ((screen.get_width() / 2) - 102,
> (screen.get_height() / 2) - 12,
> 204,24), 1)
> if len(message) != 0:
> screen.blit(fontobject.render(message, 1, (255,255,255)),
> ((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10))
> pygame.display.flip()
>
> def ask(screen, question):
> "ask(screen, question) -> answer"
> pygame.font.init()
> current_string = []
> display_box(screen, question + ": " + string.join(current_string,""))
> while 1:
> inkey = get_key()
> if inkey == K_BACKSPACE:
> current_string = current_string[0:-1]
> elif inkey == K_RETURN:
> break
> elif inkey == K_MINUS:
> current_string.append("_")
> elif inkey <= 127:
> current_string.append(chr(inkey))
> display_box(screen, question + ": " + string.join(current_string,""))
> return string.join(current_string,"")
> ######################
>
> Then to ask a person's name, use:
> name = ask(screen, "Your name")
>
> --
> James Reeves
> http://www.monkeyengines.co.uk/
>