added the circles and score varriable
float x = 300, y = 300, xs = 0, ys = 0, friction = 0.9, r = 20, acc = 0.3, tx = 500, ty = 300, tr = 10;
int score = 0;
boolean accelerate[] = new boolean[4];
void setup() {
size(600,600);
}
void draw() {
background(0);
fill(255);
ellipse(x,y,r*2,r*2);
x += xs;
y += ys;
println(x,y,xs,ys);
xs *= friction;
ys *= friction;
if(x + r > width) {
xs = 0;
x = width-r;
} else if(x - r < 0) {
xs = 0;
x = r;
}
if(y + r > height) {
ys = 0;
y = height-r;
} else if(y - r < 0) {
ys = 0;
y = r;
}
if(keyPressed && keyCode == LEFT) {
xs -= acc;
ys = 0;
}
if (keyPressed && keyCode == RIGHT) {
xs += acc;
ys = 0;
}
if (keyPressed && keyCode == UP) {
ys -= acc;
xs = 0;
}
if (keyPressed && keyCode == DOWN) {
ys += acc;
xs = 0;
}
fill(255,0,0);
circle(tx,ty,tr*2);
if(dist(x,y,tx,ty) < tr + r) {
tx = random(tr, width-tr);
ty = random(tr, height-tr);
score++;
}
}
edit: a bit better version:
float x = 300, y = 300, xs = 0, ys = 0, friction = 0.9, r = 20, acc = 0.6, tx = 500, ty = 300, tr = 10;
int score = 0;
boolean pressed[] = new boolean[4];
void setup() {
size(600,600);
}
void draw() {
background(0);
fill(255);
ellipse(x,y,r*2,r*2);
x += xs;
y += ys;
println(x,y,xs,ys);
xs *= friction;
ys *= friction;
if(x + r > width) {
xs = 0;
x = width-r;
} else if(x - r < 0) {
xs = 0;
x = r;
}
if(y + r > height) {
ys = 0;
y = height-r;
} else if(y - r < 0) {
ys = 0;
y = r;
}
if(pressed[2]) {
xs -= acc;
ys = 0;
}
if (pressed[3]) {
xs += acc;
ys = 0;
}
if (pressed[0]) {
ys -= acc;
xs = 0;
}
if (pressed[1]) {
ys += acc;
xs = 0;
}
fill(255,0,0);
circle(tx,ty,tr*2);
if(dist(x,y,tx,ty) < tr + r) {
tx = random(tr, width-tr);
ty = random(tr, height-tr);
score++;
}
}
void keyPressed() {
if(key == 'w') pressed[0] = true;
if(key == 's') pressed[1] = true;
if(key == 'a') pressed[2] = true;
if(key == 'd') pressed[3] = true;
}
void keyReleased() {
if(key == 'w') pressed[0] = false;
if(key == 's') pressed[1] = false;
if(key == 'a') pressed[2] = false;
if(key == 'd') pressed[3] = false;
}