Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with pygame tutorial
#6
You need to make sure you are indenting correctly. As this is the entire issue.

from this:

    while running:
        for game_event in pygame.event.get():
            if game_event.type == QUIT:
                running = False
        if game_event.type == KEYDOWN:
            if game_event.key == K_BACKSPACE:
                if len(text)>0: # if length of text is > 0
                    text = text[:-1] # delete the last letter in the string
        else:
            text += game_event.unicode
            # else add the letter to the end of the string
        img = font.render(text, True, RED)
        rect.size=img.get_size()
        cursor.topleft = rect.topright
 
        screen.fill(background)
        screen.blit(img, (20, 20))
        pygame.display.update()
to this:

    while running:
        for game_event in pygame.event.get():
            if game_event.type == QUIT:
                running = False
            if game_event.type == KEYDOWN:
                if game_event.key == K_BACKSPACE:
                    if len(text)>0: # if length of text is > 0
                        text = text[:-1] # delete the last letter in the string
                else:
                    text += game_event.unicode
                    # else add the letter to the end of the string
            img = font.render(text, True, RED)
            rect.size=img.get_size()
            cursor.topleft = rect.topright
     
            screen.fill(background)
            screen.blit(img, (20, 20))
            pygame.display.update()
Here is an example of a working textbox for comparison:
Recommended Tutorials:
Reply


Messages In This Thread
Help with pygame tutorial - by djwilson0495 - Dec-14-2020, 05:47 PM
RE: Help with pygame tutorial - by Larz60+ - Dec-14-2020, 10:04 PM
RE: Help with pygame tutorial - by djwilson0495 - Dec-15-2020, 11:53 AM
RE: Help with pygame tutorial - by Larz60+ - Dec-15-2020, 12:09 PM
RE: Help with pygame tutorial - by djwilson0495 - Dec-15-2020, 04:42 PM
RE: Help with pygame tutorial - by metulburr - Dec-20-2020, 12:20 PM
RE: Help with pygame tutorial - by MK_CodingSpace - Dec-20-2020, 02:28 PM
RE: Help with pygame tutorial - by Larz60+ - Dec-20-2020, 08:58 PM
RE: Help with pygame tutorial - by MK_CodingSpace - Dec-20-2020, 09:03 PM
RE: Help with pygame tutorial - by Larz60+ - Dec-20-2020, 09:08 PM
RE: Help with pygame tutorial - by djwilson0495 - Dec-21-2020, 11:28 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020