```rust
// Movement
plan.forward(100);
plan.backward(50);
// Rotation
plan.left(90); // degrees
plan.right(45);
// Circular arcs
plan.circle_left(50.0, 180.0, 36); // radius, angle (degrees), segments
plan.circle_right(50.0, 180.0, 36); // draws arc to the right
// Pen control
plan.pen_up();
plan.pen_down();
// Appearance
plan.set_color(RED);
plan.set_pen_width(5.0);
plan.hide();
plan.show();
// Turtle shape
plan.shape(ShapeType::Triangle);
plan.shape(ShapeType::Turtle); // Default classic turtle shape
plan.shape(ShapeType::Circle);
plan.shape(ShapeType::Square);
plan.shape(ShapeType::Arrow);
// Custom shape
let custom = TurtleShape::new(
vec![vec2(10.0, 0.0), vec2(-5.0, 5.0), vec2(-5.0, -5.0)],
true // filled
);
plan.set_shape(custom);
// Chaining
plan.forward(100).right(90).forward(50);
```
75 lines
1.7 KiB
Rust
75 lines
1.7 KiB
Rust
//! Nikolaus example - draws a house-like figure
|
|
|
|
use macroquad::prelude::*;
|
|
use turtle_lib_macroquad::*;
|
|
|
|
fn nikolausquadrat(plan: &mut TurtlePlan, groesse: f32) {
|
|
plan.forward(groesse);
|
|
plan.left(90.0);
|
|
plan.forward(groesse);
|
|
plan.left(90.0);
|
|
plan.forward(groesse);
|
|
plan.left(90.0);
|
|
plan.forward(groesse);
|
|
plan.left(90.0);
|
|
}
|
|
|
|
fn nikolausdiag(plan: &mut TurtlePlan, groesse: f32) {
|
|
let quadrat = groesse * groesse;
|
|
let diag = (quadrat + quadrat).sqrt();
|
|
|
|
plan.left(45.0);
|
|
plan.forward(diag);
|
|
plan.left(45.0);
|
|
nikolausdach2(plan, groesse);
|
|
plan.left(45.0);
|
|
plan.forward(diag);
|
|
plan.left(45.0);
|
|
}
|
|
|
|
fn nikolausdach2(plan: &mut TurtlePlan, groesse: f32) {
|
|
let quadrat = groesse * groesse;
|
|
let diag = (quadrat + quadrat).sqrt();
|
|
plan.left(45.0);
|
|
plan.forward(diag / 2.0);
|
|
plan.left(90.0);
|
|
plan.forward(diag / 2.0);
|
|
plan.left(45.0);
|
|
}
|
|
|
|
fn nikolaus(plan: &mut TurtlePlan, groesse: f32) {
|
|
nikolausquadrat(plan, groesse);
|
|
nikolausdiag(plan, groesse);
|
|
}
|
|
|
|
#[macroquad::main("Nikolaus")]
|
|
async fn main() {
|
|
// Create a turtle plan
|
|
let mut plan = create_turtle();
|
|
plan.shape(ShapeType::Turtle);
|
|
|
|
// Position the turtle (pen up, move, pen down)
|
|
plan.pen_up();
|
|
plan.backward(80.0);
|
|
plan.left(90.0);
|
|
plan.forward(50.0);
|
|
plan.right(90.0);
|
|
plan.pen_down();
|
|
|
|
nikolaus(&mut plan, 100.0);
|
|
|
|
// Create turtle app with animation (speed = 100 pixels/sec)
|
|
let mut app = TurtleApp::new().with_commands(plan.build(), 100.0);
|
|
|
|
// Main loop
|
|
loop {
|
|
clear_background(WHITE);
|
|
|
|
// Update and render
|
|
app.update();
|
|
app.render();
|
|
|
|
next_frame().await
|
|
}
|
|
}
|