0

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!

7
  • not KEYDOWN == K_w but even.key == K_w - the same with KEYDOWN == K_a, KEYDOWN == K_s, etc. KEYDOWN and KEYUP is event.type Commented Feb 5, 2017 at 17:32
  • if you have for event in pygame.event.get() then you don't neen pygame.event.pump() Commented Feb 5, 2017 at 17:39
  • BTW: we use lower_case names for variables, functions, instances, ie. player_sprite (or shorter player) player_step. And CamelCase names only for classes. See more: PEP 8 -- Style Guide for Python Code Commented Feb 5, 2017 at 17:46
  • @furas it was the only thing that made the code work in the first place. I've tried the code without pygame.event.pump() and nothing changes Commented Feb 5, 2017 at 17:48
  • you main problem is in my first comment - you use KEYDOWN in wrong way. See your first part - you have event.key == K_s but in second part you use KEYDOWN == K_1 but correctly is event.key == K_1 Commented Feb 5, 2017 at 18:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.