// Called when the robot must run public void run() { // Sets the colors of the robot // body = black, gun = white, radar = red setColors(Color.BLACK, Color.WHITE, Color.RED); // Loop forever for (; ; ) { // Sets the robot to move forward, backward or stop moving depending // on the move direction and amount of pixels to move setAhead(moveAmount * moveDirection); // Decrement the amount of pixels to move until we reach 0 pixels // This way the robot will automatically stop if the mouse wheel // has stopped it's rotation moveAmount = Math.max(0, moveAmount - 1); // Sets the robot to turn right or turn left (at maximum speed) or // stop turning depending on the turn direction setTurnRight(45 * turnDirection); // degrees // Turns the gun toward the current aim coordinate (x,y) controlled by // the current mouse coordinate double angle = normalAbsoluteAngle(Math.atan2(aimX - getX(), aimY - getY())); setTurnGunRightRadians(normalRelativeAngle(angle - getGunHeadingRadians())); // Fire the gun with the specified fire power, unless the fire power = 0 if (firePower > 0) { setFire(firePower); } // Execute all pending set-statements execute(); // Next turn is processed in this loop.. } }