简体   繁体   中英

Pygame event not working

This is my code I have for a game I am making and I manage to get the first text written to the screen then it manages to print the events but it won't run the second part of the code. I am assuming that if event.type == pygame.K_RETURN: doesn't work. I am either doing something wrong or it's not working.

while True:   
    for event in pygame.event.get():
        #print(event)
        if event.type==pygame.KEYDOWN:
            print("key pressed")
            if event.type == pygame.K_RETURN:
                print(event)
                print("enter key pressed")
                screen.fill(blue)
                textSurf, textRect = text_objects(t2, smallText)
                textRect.center = ((700),(100))
                screen.blit(textSurf, textRect)
                pygame.display.update()
    pygame.display.update()

Yes, if event.type == pygame.K_RETURN: should be if event.key == pygame.K_RETURN: .

The pygame.KEYDOWN and pygame.KEYUP event types have a key attribute that is used to check which key was pressed.

You can find a list of event types and their attributes in the docs: http://www.pygame.org/docs/ref/event.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM