4 Replies - 1370 Views - Last Post: 30 May 2012 - 02:16 PM Rate Topic: -----

#1 barberic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-April 12

Get player name for Pygame game

Posted 26 May 2012 - 11:52 PM

I've done a mashup of some code snippets I found on the web and would appreciate some help to get it working. I have a text box on screen for the player to enter their name when there is a new highscore. I have tested the input box by hard coding a name entry and everything works fine. I'm now trying to get it to work with some text entry code and I'm stuck.
Here's my code.
First I ask the player for a name with data["name"] = ask("Your name")
def ask(question):
    '''ask(question) -> answer'''
    current_string = ""
    display_box(question + ": " + current_string)
    while 1:
        current_string += str(enterName.getCharacter)
        # show the full string while typing
        display_box(question + ": " + current_string)
    return current_string # this is the answer


The box appears on screen with text "Your name : " ready for the player to add their name.

To get the keys being typed I have a class enterName:
class enterName:
    def getKeyPress(self):
      for event in pygame.event.get():
         if event.type == KEYDOWN:    
             return event.key
         else:
             return False

    def getCharacter(self):
      # Check to see if the player has used a modifier key (Shift, Alt, Ctrl)
      keyinput = pygame.key.get_pressed()  

      character = "NULL"

      # Get all "Events" that have occurred.
      pygame.event.pump()
      keyPress = self.getKeyPress()

      #If the user presses a key on the keyboard then get the character
      #If the user presses the shift key while pressing another character
      #then capitalise it
      if keyPress >= 32 and keyPress <= 126:
          if keyinput[K_LSHIFT]: 
              keyPress -= 32

          character = chr(keyPress)

      return character


The display_box puts the name entry box on screen and updates while typing
def display_box(message):
  "Print a message in a box in the middle of the screen"
  left = (SCREENWIDTH / 2) - 156
  top = (SCREENHEIGHT / 2) + 4  #16
  pygame.draw.rect(SCREEN, DARKGREEN, (left, top, 320, 200)) # 320  240
  SCREEN.blit(BASICFONT.render("New High Score!", True, GREEN),
              (left + 90, top + 35)) # 55
  SCREEN.blit(BASICFONT.render("Press return when done.", True, GREEN),
              (left + 51, top + 160)) # 180
  
  pygame.draw.rect(SCREEN, BLACK, (left + 39, top + 110, 240, 20))
  pygame.draw.rect(SCREEN, WHITE, (left + 38, top + 108, 244, 24), 1)
  
  if len(message) != 0:
    SCREEN.blit(BASICFONT.render(message, True, WHITE), (left+42, top + 111))
                
  pygame.display.flip()


When done, the player hits the Return key and the name is stored in the data file.
Well that's what I want but when the program gets to the last SCREEN.blit in display_box(message): I get this error and a long name entry like <unbound method enterName.getCh... vanishing off the screen and the game crashes.
SCREEN.blit(BASICFONT.render(message, True, WHITE), (left+42, top + 111))
error: Width or height is too large

This is my last attempt after trying several solutions with various error resuls.

Please can anyone put me staight.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Get player name for Pygame game

#2 barberic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-April 12

Re: Get player name for Pygame game

Posted 28 May 2012 - 05:28 PM

Hey guys
Is this in the right forum?
Not enough info?
Or put in the too hard basket? Surely not!
Anyone can help? Please.
Was This Post Helpful? 0
  • +
  • -

#3 JonBernal  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 199
  • Joined: 14-March 09

Re: Get player name for Pygame game

Posted 29 May 2012 - 10:07 PM

I just started with Python so I been checking the forum every now and then but haven't noticed it too be very crawled.
Hope someone will be able to help you out.
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Get player name for Pygame game

Posted 30 May 2012 - 08:08 AM

I watch this forum like a hawk, but I don't have pygame and I really don't have the time to set this up for testing right now.
Was This Post Helpful? 0
  • +
  • -

#5 barberic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-April 12

Re: Get player name for Pygame game

Posted 30 May 2012 - 02:16 PM

Thanks for looking folks I'll try a diifferent approach.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1