# How to move something diagonally?

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

``````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()
``````
1 Like

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

``````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
``````
2 Likes

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