two buttons at the same time?

Hi, I’m new to panda, and programming in general.
I am making a game and I want to control the player like those old 2D rpgs, just 8 directions of movement.

I think I got the hang of moving an object around with the arrow keys using the Roaming Ralph tutorial. I can currently move an object in four directions, up, down, left, and right.

What I can’t seem to figure out is how to move an object diagonally by checking for when 2 arrow keys are being pressed at the same time.

I would really appreciate some help :smiley:

Well, using Roaming Ralph, replace:

#move west
if (self.keyMap[“left”]!=0):

with:

#move northwest
if (self.keyMap[“left”]!=0) and (self.keyMap[“up”]!=0):

Thanks for the quick reply. I’ll go and try that now.

By the way, you wouldn’t happen to know what these mean in the tutorial:
camright = base.camera.getNetTransform().getMat().getRow3(0)
camright.normalize()

Froggy, forget about those two lines. It’s an extremely inefficient way of moving Ralph forward/right on its own axis.
A much better way would be:

#forward:
self.ralph.setY(self.ralph,-elapsed)
#left:
self.ralph.setX(self.ralph,-elapsed)
#right:
self.ralph.setX(self.ralph,elapsed)

or just
self.ralph.setX(self.ralph, 6888)

Well I have been using the self.playerObj(self.playerObj,moveSpeed) method to move my character around, and that works fine, but when I press an arrow button while holding down another arrow button, the messenger thing registers the first arrow button as up, So my keyBank is updated accordingly, which means that I can only read one keystroke at a time…

reading that back, it was a little hard to follow :laughing: , so I’ll try to give a little example.

I’m pressing the up button
the messenger sends arrow_up
the control event accepts this and updated my keyBank, so that ‘up’ = True
My PlayerMovement task reads this and moves the character up

(Everything is going fine so far)

I’m now pressing left button (I never stop pressing the up button)
the messenger sends arrow_up-up ([color=red]here is the problem)
the control event accepts this and updated my keyBank, so that ‘up’ = False
the messenger send arrow_left
the control event accepts this and updated my keyBank, so that ‘left’ = True
My PlayerMovement task reads this and moves the character left (crap :confused:)

So, if there was some way to read when two keys are being pressed at the same time, it would help me bunches :slight_smile:

Its probably just the way you have your ‘ifs’ arranged, but to give you a specific answer we need to see your movement code.

What happens if you change your code to have the bank accepts

arrow_up and arrow_up-up as the moving forward event?

Does it works better?

Manakel: If I did that, the player wouldn’t stop moving. That would be great if I was making pacman :stuck_out_tongue:

T Rex: Thanks, that solved my problem! It was my if statements. The before and after code is below. (I’m so brain dead, should have posted the code from the beginning…)

Before:

    
    if timeElapsed >= self.movementUpdateTime:
      
      if self.keyBank['up']:  
         if not self.keyBank['down'] and not self.keyBank['right'] and not self.keyBank['left']:  
          self.playerPosY = self.playerPosY + self.playerMoveSpeed
          self.playerHeading = 0
          self.previousMovementUpdateTime = Task.time             
             
      if self.keyBank['left']: 
          if not self.keyBank['up'] and not self.keyBank['down'] and not self.keyBank['right']:
          self.playerPosX = self.playerPosX - self.playerMoveSpeed
          self.playerHeading = 90
          self.previousMovementUpdateTime = Task.time
                    
      if self.keyBank['down']: 
          if not self.keyBank['up'] and not self.keyBank['right'] and not self.keyBank['left']:
          self.playerPosY = self.playerPosY - self.playerMoveSpeed
          self.playerHeading = 180
          self.previousMovementUpdateTime = Task.time
          
      if self.keyBank['right']: 
          if not self.keyBank['up'] and not self.keyBank['down'] and not self.keyBank['left']:
          self.playerPosX = self.playerPosX + self.playerMoveSpeed
          self.playerHeading = 270
          self.previousMovementUpdateTime = Task.time
        
      self.player.setPos(self.playerPosX,self.playerPosY,self.playerPosZ)
      self.player.setH(self.playerHeading)
    return Task.cont

After:

    
    if timeElapsed >= self.movementUpdateTime:
      
      if self.keyBank['up']:  
          self.playerPosY = self.playerPosY + self.playerMoveSpeed
          self.playerHeading = 0
          self.previousMovementUpdateTime = Task.time             
             
      if self.keyBank['left']: 
          self.playerPosX = self.playerPosX - self.playerMoveSpeed
          self.playerHeading = 90
          self.previousMovementUpdateTime = Task.time
                    
      if self.keyBank['down']: 
          self.playerPosY = self.playerPosY - self.playerMoveSpeed
          self.playerHeading = 180
          self.previousMovementUpdateTime = Task.time
          
      if self.keyBank['right']: 
          self.playerPosX = self.playerPosX + self.playerMoveSpeed
          self.playerHeading = 270
          self.previousMovementUpdateTime = Task.time
        
      self.player.setPos(self.playerPosX,self.playerPosY,self.playerPosZ)
      self.player.setH(self.playerHeading)
      self.playerDummyObject.setPos(self.player,0,0,0)
      base.camera.lookAt(self.player)
    return Task.cont

The only problem now is that, as you can see, I’m setting the player’s heading according to which button is being pressed, but I don’t know how to set the player’s heading to the diagonal ones (45,135,225,315)
Any suggestions :question:

edit: Sorry, disregard that last part, easily solved. :blush:

this should work :

up=down=0
if self.keyBank['up']:
    self.playerPosY = self.playerPosY + self.playerMoveSpeed
    self.playerHeading = 0
    up=1
    self.previousMovementUpdateTime = Task.time

if self.keyBank['down']:
    self.playerPosY = self.playerPosY - self.playerMoveSpeed
    self.playerHeading = 180
    down=1
    self.previousMovementUpdateTime = Task.time

if self.keyBank['left']:
    self.playerPosX = self.playerPosX - self.playerMoveSpeed
    if up:
       self.playerHeading = 45
    elif down:
       self.playerHeading = 135
    else:
       self.playerHeading = 90
    self.previousMovementUpdateTime = Task.time

if self.keyBank['right']:
    self.playerPosX = self.playerPosX + self.playerMoveSpeed
    if up:
       self.playerHeading = 315
    elif down:
       self.playerHeading = 225
    else:
       self.playerHeading = 270
    self.previousMovementUpdateTime = Task.time

ynjh_jo: Thanks, I was doing it this way…

if self.keyBank['up'] and self.keyBank['left']: self.playerHeading = 45 
if self.keyBank['left'] and self.keyBank['down']: self.playerHeading = 135
if self.keyBank['down'] and self.keyBank['right']: self.playerHeading = 225
if self.keyBank['right'] and self.keyBank['up']: self.playerHeading = 315

but your way is much more clever :slight_smile:

No, my way is silly. Why would I want to do 4 memory writes (up & down), if I could use the already existing keybanks ‘up’ & ‘down’ ?
:laughing: :laughing: God knows why : it were some shorts in my head. :unamused: