Mog Pyner
Mog Pyner

Reputation: 1

Key input code (pygame) not responding

I'm using python and the pygame module for my A Level project (creating a very basic, pixelated game - think early pokemon) and I've used the event.type function is two discreet parts of the code, using different keys entirely. The first section works in the main section of the code, but the second is within a function and displays no output whatsoever.

I tried putting both parts into one at the end of the program with different conditions to make them true, but nothing I do seems to work. It's currently put into a function:

#variable that causes Combat Mode, generates 'random encounter'
PlayerStep = 5

while True:#while the screen is up and the program is running
    for event in pygame.event.get():
        if event.type == KEYDOWN and PlayerSprite.canMove:
            #actions based on which key is pressed
            if event.key == K_w:#sprite moves up
                PlayerSprite.moveup()

            elif event.key == K_s:#sprite moves down
                PlayerSprite.movedown()

            elif event.key == K_a:#sprite moves right
                PlayerSprite.moveright()

            elif event.key == K_d:#sprite moves left - no diagonal movement
                PlayerSprite.moveleft()

            PlayerStep = PlayerStep - 1
            pygame.event.pump()
            print(KEYDOWN)
            pygame.display.update()

        if event.type == KEYDOWN and CombatMode() == True:
            if KEYDOWN == K_w or KEYDOWN == K_a or KEYDOWN == K_s or KEYDOWN == K_d: 
                    event.key = KEYUP #stops the WASD keys affecting the Combat Mode function

                #use the number buttons to attack
            if KEYDOWN == K_1:#'attack' option
                ClearMsg()
                TextBox("You attack!", (60, 50), 32)
                MonsterHP = MonsterHP - 2
                print(MonsterHP)

            if KEYDOWN == K_2:#heal option
                PlayerHP = PlayerHP + 10
                print(PlayerHP)

            if KEYDOWN == K_3:#'item's option
                #add square to display list of items
                BoxOption((255,250,191), (204,200,153), 150, 50, 400, 300, 4)

            if KEYDOWN == K_4:#'flee' option - ends Combat Mode and returns to main screen
                break
                return
            pygame.event.pump()

    #gives Player warning when PlayerStep is low    
    if PlayerStep < 5 and PlayerStep != 0:
        TextBox("Wait!", (255,50), 32)
        pygame.event.pump()
    elif PlayerStep <= 0:#causes Combat Mode when PlayerStep is 0
        PlayerSprite.nomove()
        Combat_Mode()
    pygame.display.update()

For context, the code is split into two sections: Explore Mode and Combat Mode. The Explore Mode is the main screen of the game where the sprite moves around, and Combat Mode is the turn-based battle system: a function that is called when the condition is met.

The WASD keys still work just fine, but nothing happens when the 1234 keys are pressed and the Combat Mode function is called.

I'm very new to using pygame but I've been using python for a few years now (still not great, but fairly competent). Any help is very much appreciated!

Upvotes: 0

Views: 159

Answers (1)

SnootierMoon
SnootierMoon

Reputation: 137

You did:

print(KEYDOWN)     (Line 22)
if KEYDOWN == K_w or KEYDOWN == K_a or KEYDOWN == K_s or KEYDOWN == K_d: (Line 26)
if KEYDOWN == K_1: (Line 30)
if KEYDOWN == K_2: (Line 36)
if KEYDOWN == K_3: (Line 40)
if KEYDOWN == K_4: (Line 44)

but you need to replace all those KEYDOWNs with event.key And also on line 27,event.key = KEYUP does not work. Near the beggining, all those event.keys you did were correct.

Upvotes: 1

Related Questions