PosMover pm; SpeedMover sm; void setup(){ size(250,250); sm = new SpeedMover(); pm = new PosMover(); mouseX = width/2; mouseY = height/2; } void draw(){ background(200); sm.move(); sm.draw(); pm.move(); pm.draw(); } abstract class Mover{ float x,y,xs,ys; float sz; Mover(){ x = width / 2; y = height / 2; sz = width / 40; } abstract void move(); void draw(){ ellipse(x,y,sz*2,sz*2); } } class SpeedMover extends Mover{ float ACCEL = .1; void move(){ if(keyPressed){ if(keyCode == UP) ys -= ACCEL; if(keyCode == DOWN) ys += ACCEL; if(keyCode == LEFT) xs -= ACCEL; if(keyCode == RIGHT) xs += ACCEL; } x += xs; y += ys; if(x < 0 || x > width) { xs *= -.5; x += xs; } if(y < 0 || y > width) { ys *= -.5; y += ys; } } } class PosMover extends Mover{ float MOVE = 2; void move(){ if(keyPressed){ if(keyCode == UP) y -= MOVE; if(keyCode == DOWN) y += MOVE; if(keyCode == LEFT) x -= MOVE; if(keyCode == RIGHT) x += MOVE; } } }