# Tron Light Cycle project I'm making

JAVA**
so im trying to move my point continuously when a key is pressed it will only move that direction.

``````//arrow keys to draw
float x,y;
int speedY = 0;
int speedX = 1;

void setup(){
size(500,500);
background(255,255,255);  //background white
x=150;
y=150;
stroke(0,0,0);  //pen black
}

void draw(){
stroke(255,0,0);
point(x,y);  //draw a point at current (x,y)
updatepoint();
}

void updatepoint(){
x = x + speedX;
y = y - speedY;

}
void keyPressed(){
if(key == CODED)
{
if (keyCode == UP && y>=0){ //restrict to screen edge
y=y-1;
}
else if(keyCode == DOWN && y<=500){
y=y+1;
}
else if (keyCode == LEFT && x>=0){
x=x-1;
}
else if (keyCode == RIGHT && x<=500){
x=x+1;
}
}
}

``````

i was trying to use speedX and SpeedY but the next mistake happens when instead a continuous trail, it just makes multiple dots.

Hey, and welcome to the forum!

Great to have you here!

You need to change speed.

In this version though, for example RIGHT key always means east (absolute);
you could also make it so he can only turn left and right (relative)

Warm regards,

Chrisir

``````//arrow keys to steer
float x, y;
int speedY = 0;
int speedX = 1;

void setup() {
size(500, 500);
background(255, 255, 255);  //background white
x=150;
y=150;
stroke(0, 0, 0);  //pen black
}

void draw() {
stroke(255, 0, 0);
point(x, y);  //draw a point at current (x,y)
updatepoint();
}

void updatepoint() {
x = x + speedX;
y = y + speedY;
}

void keyPressed() {
if (key == CODED)
{
if (keyCode == UP && y>=0) { //restrict to screen edge
speedX=0;
speedY=-1;
} else if (keyCode == DOWN && y<=500) {
speedX=0;
speedY=1;
} else if (keyCode == LEFT && x>=0) {
speedX=-1;
speedY=0;
} else if (keyCode == RIGHT && x<=500) {
speedX=1;
speedY=0;
}
}
}
``````
1 Like