Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pygame detecting any keydown instead of just 'S' key

When running the following lines of code, a user should press the ‘W’ button to start meditating, and press the ‘S’ button to stop meditating, however what I’m finding is that if ANY button is pressed, the user starts or stops meditating depending on whether DL[19] = 4 or 0. This is a problem as I need the keys ‘A’ and ‘D’ to navigate through rooms. Any suggestions on how to fix this?

        if DL[19] == 0: # no activity
            displaydata1 = font.render(" 'W' Key: meditate", True, (255,255,255))
            screen.blit(displaydata1, (coordinates[0], coordinates[1] + 14))
            if event.type == pygame.KEYDOWN:
                if pygame.K_w:
                    DL[19] = 4 # set activity to meditating

        elif DL[19] == 4: # meditating
            displaydata1 = font.render(" 'S' Key: stop meditating", True, (255,255,255))
            screen.blit(displaydata1, (coordinates[0], coordinates[1] + 14))
            if event.type == pygame.KEYDOWN:
                if pygame.K_s:
                    DL[19] = 0 # set to no activity

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if DL[19] == 0: # no activity
            displaydata1 = font.render(" 'W' Key: meditate", True, (255,255,255))
            screen.blit(displaydata1, (coordinates[0], coordinates[1] + 14))
            if event.type == pygame.KEYDOWN:
                if event.type == pygame.K_w:
                    DL[19] = 4 # set activity to meditating

        elif DL[19] == 4: # meditating
            displaydata1 = font.render(" 'S' Key: stop meditating", True, (255,255,255))
            screen.blit(displaydata1, (coordinates[0], coordinates[1] + 14))
            if event.type == pygame.KEYDOWN:
                if event.type == pygame.K_s:
                    DL[19] = 0 # set to no activity


You say event.type == pygame.K_w to see if "w" was clicked

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading