### Setup & Initialization - **Include Header:** `#include ` (Essential for all simplecpp functionalities) - **Standard Main:** `int main() { ... return 0; }` (C++ program entry point) - **Mode 1: Default Global Turtle (Quick Setup)** - `turtleSim();` (Initializes a canvas and a default global turtle, `t`, accessible directly) - Example: `turtleSim(); forward(100); wait(2);` (No `initCanvas` or `Turtle t;` needed) - **Mode 2: Explicit Canvas & Turtle Object (Recommended for Control)** - `initCanvas("Title", width, height);` (Creates a drawing window with specified title and dimensions) - Example: `initCanvas("My Turtle Window", 800, 600);` - `Turtle t;` (Creates a turtle object `t` at canvas center `(0,0)`, heading East (0 degrees)) - **Wait for Input:** `getClick();` or `getChar();` (Pauses program until mouse click or key press) - **Close Graphics:** `closeCanvas();` (Closes the drawing window, important for freeing resources) ### Turtle Object & Global Turtle - **Creating Object:** `Turtle myTurtle;` (Creates an independent turtle object) - **Default Global Turtle:** When `turtleSim()` is used, a global `Turtle` object named `t` is automatically created. All turtle commands can then be called directly (e.g., `forward(100);` instead of `t.forward(100);`). - **Multiple Turtles:** `Turtle t1, t2;` (Declare multiple independent turtles in object mode. Each has its own state: position, heading, pen color/width.) - **Active Turtle:** In object mode, you explicitly call methods on a specific turtle (e.g., `t1.forward(50);`). ### Movement - **Forward:** `t.forward(distance);` (Moves turtle forward in its current heading direction) - Global: `forward(distance);` - Example: `t.forward(100);` - **Backward:** `t.backward(distance);` (Moves turtle backward) - Global: `backward(distance);` - **Left Turn:** `t.left(angle);` (Turns turtle left by `angle` degrees) - Global: `left(angle);` - Example: `t.left(90);` - **Right Turn:** `t.right(angle);` (Turns turtle right by `angle` degrees) - Global: `right(angle);` - **Go To Coordinates:** `t.penUp(); t.moveTo(x, y); t.penDown();` (Moves turtle to absolute canvas coordinates `(x, y)` without drawing. Pen control is crucial here.) - Global: `penUp(); moveTo(x, y); penDown();` - **Set X Coordinate:** `t.setX(x);` (Sets turtle's X position on the canvas) - **Set Y Coordinate:** `t.setY(y);` (Sets turtle's Y position on the canvas) - **Set Position:** `t.setPos(x, y);` (Sets both X and Y position simultaneously) - **Set Heading:** `t.setHeading(angle);` (Sets turtle's direction in degrees, 0 is East/Right, 90 is North/Up) - **Get Position:** `t.getX();`, `t.getY();` (Returns current X or Y coordinate) - **Get Heading:** `t.getHeading();` (Returns current heading in degrees) ### Pen & Fill Control - **Pen Up:** `t.penUp();` (Lifts the pen; subsequent movement will not draw) - Global: `penUp();` - **Pen Down:** `t.penDown();` (Puts the pen down; subsequent movement will draw) - Global: `penDown();` - **Pen Color:** `t.setColor(COLOR_NAME);` or `t.setColor(r, g, b);` (Sets the pen color) - Global: `setColor(COLOR_NAME);` - Predefined colors: `RED`, `BLUE`, `GREEN`, `BLACK`, `WHITE`, `YELLOW`, `MAGENTA`, `CYAN`, `ORANGE`, `BROWN`, `GREY` - Example: `t.setColor(BLUE);` or `t.setColor(0, 255, 0);` (for bright green) - **Pen Width:** `t.setPenWidth(width);` (Sets the thickness of the line drawn) - Global: `setPenWidth(width);` - Example: `t.setPenWidth(5);` - **Fill Toggle:** `t.setFill(true);` (Enables filling for subsequent closed shapes) - Global: `setFill(true);` - `t.setFill(false);` (Disables filling) - **Fill Color:** `t.setFillColor(COLOR_NAME);` or `t.setFillColor(r, g, b);` (Sets the color for filling shapes) - Global: `setFillColor(COLOR_NAME);` ### Drawing Shapes - **Circle:** `t.circle(radius);` (Draws a circle with the turtle at its center) - Global: `circle(radius);` - **Arc:** `t.arc(radius, start_angle, end_angle);` (Draws an arc) - Global: `arc(radius, start_angle, end_angle);` - **Rectangle:** `t.rectangle(width, height);` (Draws a rectangle with turtle at its center) - Global: `rectangle(width, height);` - **Polygon:** `t.polygon(sides, side_length);` (Draws a regular polygon) - Global: `polygon(sides, side_length);` - Example: `t.polygon(3, 100);` (equilateral triangle) - **Begin/End Path:** `t.begin_new_path(); ... t.end_new_path();` (Marks the start and end of a path for potential filling) - Global: `begin_new_path(); ... end_new_path();` ### Canvas Properties & Utilities - **Canvas Width:** `canvas_width();` (Returns current canvas width in pixels) - **Canvas Height:** `canvas_height();` (Returns current canvas height in pixels) - **Clear Canvas:** `clearCanvas();` (Erases all drawings from the canvas) - **Background Color:** `setBackgroundColor(COLOR_NAME);` or `setBackgroundColor(r, g, b);` (Sets the canvas background color) - Example: `setBackgroundColor(LIGHT_BLUE);` - **Hide Turtle:** `t.hide();` (Makes the turtle icon invisible) - **Show Turtle:** `t.show();` (Makes the turtle icon visible) - **Home Position:** `t.home();` (Resets turtle to center `(0,0)` and heading East `(0 degrees)`) - **Canvas Coordinates:** Origin `(0,0)` is the center of the canvas. X increases to the right, Y increases upwards. - **Heading Convention:** 0 degrees is East (right), 90 degrees is North (up), 180 degrees is West (left), 270 degrees is South (down). ### Time & Animation Control - **Wait:** `wait(seconds);` (Pauses program execution for `seconds` (double) to observe drawing) - Example: `wait(0.5);` (pauses for half a second) - **Animate:** `t.animate(time_in_ms);` (Smoothly animates the turtle's last movement over `time_in_ms` milliseconds) - Global: `animate(time_in_ms);` - Example: `t.forward(100); t.animate(200);` (turtle moves 100 units over 0.2 seconds) - **Set Speed:** `t.setSpeed(speed_factor);` (Controls the speed of animation. `speed_factor` is a multiplier, default is 1.0) - Global: `setSpeed(speed_factor);` - Example: `t.setSpeed(2.0);` (double speed) - **Repeat Macro:** `REPEAT(N) { ... }` (A simple macro for repeating code N times) - Example: `REPEAT(4) { forward(100); right(90); }` (draws a square) ### Text Output - **Add Text:** `addText(x, y, "text content");` (Displays text at canvas coordinates `(x, y)`) - Example: `addText(-100, 250, "Hello Turtle!");` - **Set Font:** `setFont("font_name");` (Sets font for subsequent text; e.g., "Arial", "Times New Roman") - **Set Font Size:** `setFontSize(size);` (Sets font size in points) - **Set Font Color:** `setFontColor(COLOR_NAME);` or `setFontColor(r, g, b);` (Sets font color) ### User Interaction - **Get Mouse Click:** `getClick();` (Pauses until a mouse click, returns an `int` (x*1000 + y) but actual coordinates are in `_last_mouseX`, `_last_mouseY` globals) - Example: `getClick(); int clickedX = _last_mouseX; int clickedY = _last_mouseY;` - **Check Key Press:** `char c = getChar();` (Waits for and returns a character key press) - **Is Key Down:** `bool is_key_down(int key_code);` (Checks if a specific key is currently pressed, non-blocking) - Key codes: `KEY_UP`, `KEY_DOWN`, `KEY_LEFT`, `KEY_RIGHT`, `KEY_SPACE`, `KEY_ESC`, or ASCII char codes - Example: `if (is_key_down(KEY_UP)) { t.forward(10); }` - **Check Mouse Click (Non-blocking):** `bool checkMouseClick(int &x, int &y);` (Returns true if a click occurred, stores coords in `x`, `y`)