class Player { Particle p; float radius, speed; Player (Particle part, float r, float s) { p = part; p.makeFixed(); radius = r; speed = s; } void keyMove() { float moveAmt; if ( keyCode == UP ) { moveAmt = min(y() - 5, speed); p.moveBy(0, -moveAmt, 0); } if ( keyCode == DOWN ) { moveAmt = min(height - y() - 5, speed); p.moveBy(0, moveAmt, 0); } if ( keyCode == LEFT ) { moveAmt = min(x() - 5, speed); p.moveBy(-moveAmt, 0, 0); } if ( keyCode == RIGHT ) { moveAmt = min(width - x() - 5, speed); p.moveBy(moveAmt, 0, 0); } } void move(int x, int y) { p.moveBy(x, -y, 0); if ( x() < radius ) p.moveTo(radius, y(), 0); else if ( x() > width - radius ) p.moveTo(width - radius, y(), 0); if ( y() < radius ) p.moveTo(x(), radius, 0); else if ( y() > height - radius ) p.moveTo(x(), height - radius, 0); } void render() { stroke(200); strokeWeight(1); fill(0, 0, 255); rect(x(), y(), radius, radius); } void speedUp() { speed = constrain(speed+1, 1, 10); println("Speed increased to " + speed + "."); } void slowDown() { speed = constrain(speed-1, 1, 10); println("Speed decreased to " + speed + "."); } float x() { return p.position().x(); } float y() { return p.position().y(); } }