0

I'm making a local multiplayer game and I wanted to make the character move diagonally as well. Right now it only moves either left, right, up, or down.

def update():
    if keyboard.right:
        fly.x += MOVE_DISTANCE
        if fly.x > WIDTH:
            fly.x = WIDTH
    elif keyboard.left:
        fly.x -= MOVE_DISTANCE
        if fly.x < 0:
            fly.x = 0
    elif keyboard.down:
        fly.y += MOVE_DISTANCE
        if fly.y > HEIGHT:
            fly.y = HEIGHT
    elif keyboard.up:
        fly.y -= MOVE_DISTANCE
        if fly.y < 0:
            fly.y = 0

EDIT: When I type this it goes way too fast

def update():
    if keyboard.right:
        fly.x += MOVE_DISTANCE
        if fly.x > WIDTH:
            fly.x = WIDTH
    elif keyboard.left:
        fly.x -= MOVE_DISTANCE
        if fly.x < 0:
            fly.x = 0
    if keyboard.down:
        fly.y += MOVE_DISTANCE
        if fly.y > HEIGHT:
            fly.y = HEIGHT
    elif keyboard.up:
        fly.y -= MOVE_DISTANCE
        if fly.y < 0:
            fly.y = 0

Swagtonio
  • 13
  • 3
  • Can you explain the necessary logic in plain English? Have you thought about how it would work? Do you understand the existing code? – Karl Knechtel Mar 22 '20 at 03:22
  • I am trying to make an image (the fly) move up, down, left, right, upper left, upper right, downer left, and downer right at the same speed. – Swagtonio Mar 22 '20 at 03:41
  • Not what I asked. For example, can you give me a rule that says whether `fly.x` should change, depending on which keyboard buttons are pressed? – Karl Knechtel Mar 22 '20 at 03:42
  • I'm using pygame so `if keyboard.left: fly.x -= MOVE_DISTANCE` means that whenever pressing the left key on my keyboard is true, the fly moves the amount of pixels I want it to. I made it easier to change a couple things in the beiginning of my code and if you want I can show you the whole thing. – Swagtonio Mar 22 '20 at 03:50
  • ... still not what I asked. If I press down and left, should the `fly.x` change? If I press down and right, should the `fly.x` change? etc. for all combinations. Then, come up with a *rule*. Which keys actually matter? – Karl Knechtel Mar 22 '20 at 03:52
  • Okay so I should add whether or not two keys are being pressed and if so how does it affect the fly, is what you're saying. **Edit :** It should be easy with the `and` function. – Swagtonio Mar 22 '20 at 03:55

0 Answers0