# How to move something diagonally?

**URL:** <https://discourse.processing.org/t/how-to-move-something-diagonally/19884>\
**Category:** Processing.py\
**Created:** [April 18, 2020, 8:50am UTC](https://discourse.processing.org/t/how-to-move-something-diagonally/19884 "2020-04-18T08:50:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iJermo](https://avatars.discourse-cdn.com/v4/letter/i/85f322/32.png) [@iJermo](https://discourse.processing.org/u/iJermo)\
**Post date:** [April 18, 2020, 8:51am UTC](https://discourse.processing.org/t/how-to-move-something-diagonally/19884/1 "2020-04-18T08:51:00Z")

</div>

That’s where I am at. I’ve looked everywhere but I couldn’t convert java solutions into my python code

```auto
class car():
    def __init__ (self):
        self.c1 = 95
        self.c2 = 18
        self.c3 = 132
        self.width = 1280
        self.height = 720
        self.x = 100
        self.y = self.height / 2
        self.speed = 5
        self.circleSize = 50
        self.UPBUTTON = False
        self.DOWNBUTTON = False
        self.RIGHTBUTTON = False
        self.LEFTBUTTON = False

        
    def CircleDisplay(self):
        fill(-196864)
        strokeWeight(10.0)
        stroke(-16777216)
        pushMatrix()
        translate(287.5635, 191.73846)
        rotate(0.0)
        arc(self.x,self.y,247.01285, 247.02548, 0.55413705, 5.770796, PIE)
        popMatrix()
        
        fill(-1)
        strokeWeight(10.0)
        stroke(-16777216)
        pushMatrix()
        translate(273.42612, 124.58582)
        rotate(0.0)
        ellipse(self.x,self.y,41.255127, 41.255127)
        popMatrix()
        
        fill(-16777216)
        strokeWeight(10.0)
        stroke(-16777216)
        pushMatrix()
        translate(278.72766, 125.469406)
        rotate(0.0)
        ellipse(self.x,self.y,9.0108595, 9.0108595)
        popMatrix()

        
                        
# These functions handles the movement 
    def keyPressed(self):
        if keyPressed == True:
            if key == CODED:
                if keyCode == UP:
                    self.y -= self.speed
            if key == CODED:
                if keyCode == DOWN:
                    self.y += self.speed
            if key == CODED:
                if keyCode == RIGHT:
                    self.x += self.speed
            if key == CODED:
                if keyCode == LEFT:
                    self.x -= self.speed 

mycar = car()

def setup():
    size(mycar.width,mycar.height)
    frameRate(120)
    
def draw():
    background(200,154,89)
    mycar.CircleDisplay()
    mycar.keyPressed()

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [April 18, 2020, 9:20am UTC](https://discourse.processing.org/t/how-to-move-something-diagonally/19884/2 "2020-04-18T09:20:22Z")

</div>

Your code is only able to detect a single key-press at any time. You need something like this:  
[https://happycoding.io/tutorials/processing/input#handling-multiple-keys](https://happycoding.io/tutorials/processing/input#handling-multiple-keys)

Adapted for Python, it’s:

```auto
upPressed = False
downPressed = False
leftPressed = False
rightPressed = False
circleX = 50
circleY = 50

def draw():
  global circleX, circleY
  background(200);  
  if upPressed:
    circleY -= 1
  if downPressed:
    circleY += 1
  if leftPressed:
    circleX -= 1
  if rightPressed:
    circleX += 1
  ellipse(circleX, circleY, 20, 20)

def keyPressed():
  global upPressed, downPressed, leftPressed, rightPressed
  if keyCode == UP:
    upPressed = True
  elif keyCode == DOWN:
    downPressed = True
  elif keyCode == LEFT:
    leftPressed = True
  elif keyCode == RIGHT:
    rightPressed = True

def keyReleased():
  global upPressed, downPressed, leftPressed, rightPressed
  if keyCode == UP:
    upPressed = False
  elif keyCode == DOWN:
    downPressed = False
  elif keyCode == LEFT:
    leftPressed = False
  elif keyCode == RIGHT:
    rightPressed = False

```

---

<div class="post-metadata">

**Author:** ![iJermo](https://avatars.discourse-cdn.com/v4/letter/i/85f322/32.png) [@iJermo](https://discourse.processing.org/u/iJermo)\
**Post date:** [April 18, 2020, 9:24am UTC](https://discourse.processing.org/t/how-to-move-something-diagonally/19884/3 "2020-04-18T09:24:35Z")

</div>

Thank you!  
I’ve tried to adapt this code but I failed miserably lol.
