Struct turtle::Turtle [] [src]

pub struct Turtle { /* fields omitted */ }

A turtle with a pen attached to its tail.

The idea: You control a turtle with a pen tied to its tail. As it moves across the screen, it draws the path that it follows. You can use this to draw any picture you want just by moving the turtle across the screen.

turtle moving forward

See the documentation for the methods below to learn about the different drawing commands you can use with the turtle.

Methods

impl Turtle
[src]

[src]

Create a new turtle.

This will immediately open a new window with the turtle at the center. As each line in your program runs, the turtle shown in the window will update.

extern crate turtle;
use turtle::Turtle;

fn main() {
    let mut turtle = Turtle::new();
    // Do things with the turtle...
}

[src]

Returns the current speed of the turtle

turtle.set_speed(8);
assert_eq!(turtle.speed(), Speed::Eight);

[src]

Set the turtle's movement speed to the given setting. This speed affects the animation of the turtle's movement and rotation. The turtle's speed is limited to values between 0 and 10. If you pass in values that are not integers or outside of that range, the closest possible value will be chosen.

This method's types make it so that it can be called in a number of different ways:

turtle.set_speed("normal");
turtle.set_speed("fast");
turtle.set_speed(2);
turtle.set_speed(10);
// Directly using a Speed variant works, but the methods above are usually more convenient.
turtle.set_speed(Speed::Six);

If input is a number greater than 10 or smaller than 1, speed is set to 0 (Speed::Instant). Strings are converted as follows:

String Value
"slowest" Speed::One
"slow" Speed::Three
"normal" Speed::Six
"fast" Speed::Eight
"fastest" Speed::Ten
"instant" Speed::Instant

Anything else will cause the program to panic! at runtime.

Moving Instantly

A speed of zero (Speed::Instant) results in no animation. The turtle moves instantly and turns instantly. This is very useful for moving the turtle from its "home" position before you start drawing. By setting the speed to instant, you don't have to wait for the turtle to move into position.

Learning About Conversion Traits

Using this method is an excellent way to learn about conversion traits From and Into. This method takes a generic type as its speed parameter. That type is specified to implement the Into trait for the type Speed. That means that any type that can be converted into a Speed can be passed to this method.

We have implemented that trait for several types like strings and 32-bit integers so that those values can be passed into this method. Rather than calling this function and passing Speed::Six directly, you can use just 6. Rust will then allow us to call .into() as provided by the Into<Speed> trait to get the corresponding Speed value.

You can pass in strings, 32-bit integers, and even Speed enum variants because they all implement the Into<Speed> trait.

[src]

Returns the turtle's current location (x, y)

turtle.forward(100.0);
let pos = turtle.position();
assert_eq!(pos, [0.0, 100.0]);

[src]

Returns the turtle's current heading.

Units are by default degrees, but can be set using the Turtle::use_degrees or Turtle::use_radians methods.

The heading is relative to the positive x axis (east). When first created, the turtle starts facing north. That means that its heading is 90.0 degrees. The following chart contains many common directions and their angles.

Cardinal Direction Heading (degrees) Heading (radians)
East 0.0° 0.0
North 90.0° PI/2
West 180.0° PI
South 270.0° 3*PI/2

You can test the result of heading() with these values to see if the turtle is facing a certain direction.

// Turtles start facing north
let mut turtle = Turtle::new();
// The rounding is to account for floating-point error
assert_eq!(turtle.heading().round(), 90.0);
turtle.right(31.0);
assert_eq!(turtle.heading().round(), 59.0);
turtle.left(193.0);
assert_eq!(turtle.heading().round(), 252.0);
turtle.left(130.0);
// Angles should not exceed 360.0
assert_eq!(turtle.heading().round(), 22.0);

[src]

Returns true if Angle values will be interpreted as degrees.

See Turtle::use_degrees() for more information.

[src]

Returns true if Angle values will be interpreted as radians.

See Turtle::use_radians() for more information.

[src]

Change the angle unit to degrees.

assert!(!turtle.is_using_degrees());
turtle.use_degrees();
assert!(turtle.is_using_degrees());

[src]

Change the angle unit to radians.

assert!(!turtle.is_using_radians());
turtle.use_radians();
assert!(turtle.is_using_radians());

[src]

Return true if pen is down, false if it’s up.

assert!(turtle.is_pen_down());
turtle.pen_up();
assert!(!turtle.is_pen_down());
turtle.pen_down();
assert!(turtle.is_pen_down());

[src]

Pull the pen down so that the turtle draws while moving.

assert!(!turtle.is_pen_down());
// This will move the turtle, but not draw any lines
turtle.forward(100.0);
turtle.pen_down();
assert!(turtle.is_pen_down());
// The turtle will now draw lines again
turtle.forward(100.0);

[src]

Pick the pen up so that the turtle does not draw while moving

assert!(turtle.is_pen_down());
// The turtle will move and draw a line
turtle.forward(100.0);
turtle.pen_up();
assert!(!turtle.is_pen_down());
// Now, the turtle will move, but not draw anything
turtle.forward(100.0);

[src]

Returns the size (thickness) of the pen. The thickness is measured in pixels.

See Turtle::set_pen_size() for more details.

[src]

Sets the thickness of the pen to the given size. The thickness is measured in pixels.

The turtle's pen has a flat tip. The value you set the pen's size to will change the width of the stroke created by the turtle as it moves. See the example below for more about what this means.

Example

extern crate turtle;
use turtle::Turtle;

fn main() {
    let mut turtle = Turtle::new();

    turtle.pen_up();
    turtle.right(90.0);
    turtle.backward(300.0);
    turtle.pen_down();

    turtle.set_pen_color("#2196F3"); // blue
    turtle.set_pen_size(1.0);
    turtle.forward(200.0);

    turtle.set_pen_color("#f44336"); // red
    turtle.set_pen_size(50.0);
    turtle.forward(200.0);

    turtle.set_pen_color("#4CAF50"); // green
    turtle.set_pen_size(100.0);
    turtle.forward(200.0);
}

This will produce the following:

turtle pen thickness

Notice that while the turtle travels in a straight line, it produces different thicknesses of lines which appear like large rectangles.

[src]

Returns the color of the pen

[src]

Sets the color of the pen to the given color

[src]

Returns the color of the background

[src]

Sets the color of the background to the given color

[src]

Returns the current fill color

This will be used to fill the shape when begin_fill() and end_fill() are called.

[src]

Sets the fill color to the given color

Note: Only the fill color set before begin_fill() is called will be used to fill the shape.

[src]

Begin filling the shape drawn by the turtle's movements

[src]

Stop filling the shape drawn by the turtle's movements

[src]

Returns true if the turtle is visible.

let mut turtle = Turtle::new();
assert!(turtle.is_visible());
turtle.hide();
assert!(!turtle.is_visible());
turtle.show();
assert!(turtle.is_visible());

[src]

Makes the turtle invisible. The shell will not be shown, but drawings will continue.

Useful for some complex drawings.

assert!(turtle.is_visible());
turtle.hide();
assert!(!turtle.is_visible());

[src]

Makes the turtle visible.

assert!(!turtle.is_visible());
turtle.show();
assert!(turtle.is_visible());

[src]

Delete the turtle's drawings from the screen.

Do not move turtle. Position and heading of the turtle are not affected.

[src]

Move the turtle forward by the given amount of distance. If the pen is down, the turtle will draw a line as it moves.

distance is given in "pixels" which are like really small turtle steps. distance can be negative in which case the turtle can move backward using this method.

[src]

Move the turtle backward by the given amount of distance. If the pen is down, the turtle will draw a line as it moves.

distance is given in "pixels" which are like really small turtle steps. distance can be negative in which case the turtle can move forwards using this method.

[src]

Rotate the turtle right (clockwise) by the given angle. Since the turtle rotates in place, its position will not change and it will not draw anything at all.

Units are by default degrees, but can be set using the methods Turtle::use_degrees or Turtle::use_radians.

[src]

Rotate the turtle left (counterclockwise) by the given angle. Since the turtle rotates in place, its position will not change and it will not draw anything at all.

Units are by default degrees, but can be set using the methods Turtle::use_degrees or Turtle::use_radians.

[src]

Rotates the turtle to face the given coordinates. Coordinates are relative to the center of the window.

If the coordinates are the same as the turtle's current position, no rotation takes place. Always rotates the least amount necessary in order to face the given point.

UNSTABLE

This feature is currently unstable and completely buggy. Do not use it until it is fixed.

[src]

Returns the next event (if any).

[src]

Convenience function that waits for a click to occur before returning.

Useful for when you want your program to wait for the user to click before continuing so that it doesn't start right away.