1

im trying to create a game where it needs a username and password to be able to login. I have only created an input for the letter "w" just to make the code a lot smaller. my problem is that if I press the w key it won't pop up on the display, but then all of a sudden 2-4 "w"s pop up after the word "hello". I don't understand why this happens

thanks in advance

import pygame


FPS = 60

black = (0, 0, 0)
white = (255, 255, 255)
blue = (0, 0, 255)
cyan = (0, 255, 255)
grey = (150, 150, 150)
brown = (139, 69, 19)
lBrown = (235, 188, 128)
peach = (255, 211, 129)
selected_colour = (0, 0, 0)

#height of game display
height = 960
#width of game display
width = 960

game_display = pygame.display.set_mode((height, width))
pygame.display.set_caption("Chess")
pygame.font.init()
font = pygame.font.SysFont("calibri", 30)
clock = pygame.time.Clock()
Login = True
username = "hello"

def update_fps():
    fps_counter = str(int(clock.get_fps()))
    fps_text = font.render(fps_counter, 1, pygame.Color("coral"))
    return (fps_text)

Typing = False
gameRunning = True
while gameRunning:
    pygame.display.flip()
    game_display.fill(white)
    game_display.blit(update_fps(), (910, 0))

    clock.tick(FPS)
    mouse_x, mouse_y = pygame.mouse.get_pos()


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameRunning = False

    if Login:
        pygame.draw.rect(game_display, black, (330, 400, 300, 100),5)
        font = pygame.font.SysFont("calibri", 30)
        if mouse_x >= 330 and mouse_x <= 630 and mouse_y >= 400 and mouse_y <= 500:

            if event.type == pygame.MOUSEBUTTONDOWN:
                #Login = False
                #mainPage = True
                Typing = True

        if mouse_x <= 330 and mouse_x >= 630 and mouse_y <= 400 and mouse_y >= 500:
            if event.type == pygame.MOUSEBUTTONDOWN:
                Typing = False
        if Typing:
            for event in pygame.event.get():
                pressed = pygame.key.get_pressed()
                if pressed[pygame.K_w]:
                    print("w is pressed")
                    username = username + "w"
                if pressed[pygame.K_BACKSPACE]:
                    username[:-1]
        game_display.blit(font.render(username, True, selected_colour), (410, 625))

1 Answers1

0

pygame.event.get() get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. [...]

If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.

Get the events once per frame and use them in multiple loops or pass the list of events to functions and methods where they are handled:

gameRunning = True
while gameRunning:
    # [...]

    event_list = pygame.event.get()
    for event in event_list :
        if event.type == pygame.QUIT:
            gameRunning = False

    if Login:
       # [...]

       for event in event_list:
            if event.type == pygame.MOUSEBUTTONDOWN: 
                mouse_x, mouse_y = event.pos  
                if mouse_x >= 330 and mouse_x <= 630 and mouse_y >= 400 and mouse_y <= 500:
           
                    #Login = False
                    #mainPage = True
                    Typing = True

                if mouse_x <= 330 and mouse_x >= 630 and mouse_y <= 400 and mouse_y >= 500:
                    Typing = False
        if Typing:
            for event in event_list:
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        print("w is pressed")
                        username = username + "w"
                    if event.key == pygame.K_BACKSPACE:
                        username[:-1]
        
        # [...]

Use the KEYDOWN event instead of pygame.key.get_pressed().
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action.
pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174