Consider this code:
float xo;
float angle=0;
void setup() {
size(800, 600);
smooth();
noStroke();
reset();
}
void reset() {
angle = 0;
xo = 0;
}
void draw() {
background(0);
translate(width/2, height/2);
rotate(angle);
pushMatrix();
translate(5, -20);
fill(255, 255, 255);
rect(xo, 0, 30, 30);
popMatrix();
fill (255, 0, 0);
rect (0, 0, 300, 10);
}
void keyPressed() {
if (key==CODED) {
if (keyCode== RIGHT) {
angle+= radians(1);
} else if (keyCode== LEFT) {
angle-= radians(1);
} else if (keyCode== UP) {
xo+= 1;
} else if (keyCode== DOWN) {
xo-= 1;
}
}
if (key==' ') {
reset();
}
}
Notice the complete lack of a y0
variable!