|
1 | 1 | # Turtle |
2 | | -Turtle Graphics in PowerShell |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +## Turtles in a PowerShell |
| 6 | + |
| 7 | +[Turtle Graphics](https://en.wikipedia.org/wiki/Turtle_graphics) are a great way to learn programming and describe shapes. |
| 8 | + |
| 9 | +Turtle graphics start really simple. |
| 10 | + |
| 11 | +Imagine we are a turtle dragging a pen. |
| 12 | + |
| 13 | +We can draw almost any shape by moving. |
| 14 | + |
| 15 | +We can only really move in two ways: |
| 16 | + |
| 17 | +We can turn, and we can take a step forward. |
| 18 | + |
| 19 | +Turtle graphics starts with these two operations: |
| 20 | + |
| 21 | +* Rotate() rotates the turtle |
| 22 | +* Forward() moves forward |
| 23 | + |
| 24 | +We can easily represent these steps in memory, and draw them within a webpage using SVG. |
| 25 | + |
| 26 | +We can implement Turtle in any language. |
| 27 | + |
| 28 | +This module implements Turtle in PowerShell. |
| 29 | +### Installing and Importing |
| 30 | + |
| 31 | +We can install Turtle from the PowerShell Gallery: |
| 32 | + |
| 33 | +~~~PowerShell |
| 34 | +Install-Module Turtle -Scope CurrentUser -Force |
| 35 | +~~~ |
| 36 | + |
| 37 | +Then we can import it like any other module |
| 38 | + |
| 39 | +~~~PowerShell |
| 40 | +Import-Module Turtle -Force -PassThru |
| 41 | +~~~ |
| 42 | + |
| 43 | +#### Cloning and Importing |
| 44 | + |
| 45 | +You can also clone the repository and import the module |
| 46 | + |
| 47 | +~~~PowerShell |
| 48 | +
|
| 49 | +git clone https://github.com/PowerShellWeb/Turtle |
| 50 | +cd ./Turtle |
| 51 | +Import-Module ./ -Force -PassThru |
| 52 | +
|
| 53 | +~~~ |
| 54 | +### Getting Started |
| 55 | + |
| 56 | +Once we've imported Turtle, we can create any number of turtles, and control them with commands and methods. |
| 57 | + |
| 58 | +#### Drawing Squares |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +Let's start simple, by drawing a square. |
| 63 | + |
| 64 | +~~~PowerShell |
| 65 | +
|
| 66 | +New-Turtle | |
| 67 | + Move-Turtle Forward 10 | |
| 68 | + Move-Turtle Rotate 90 | |
| 69 | + Move-Turtle Forward 10 | |
| 70 | + Move-Turtle Rotate 90 | |
| 71 | + Move-Turtle Forward 10 | |
| 72 | + Move-Turtle Rotate 90 | |
| 73 | + Move-Turtle Forward 10 | |
| 74 | + Move-Turtle Rotate 90 | |
| 75 | + Save-Turtle "./Square.svg" |
| 76 | +
|
| 77 | +~~~ |
| 78 | + |
| 79 | +We can also write this using a method chain: |
| 80 | + |
| 81 | +~~~PowerShell |
| 82 | +$turtle = New-Turtle |
| 83 | +$turtle. |
| 84 | + Forward(10).Rotate(90). |
| 85 | + Forward(10).Rotate(90). |
| 86 | + Forward(10).Rotate(90). |
| 87 | + Forward(10).Rotate(90). |
| 88 | + Symbol.Save("$pwd/Square.svg") |
| 89 | +~~~ |
| 90 | + |
| 91 | +This just demonstrates how we can construct shapes out of these two simple primitive steps. |
| 92 | +We can also just say, make a square directly: |
| 93 | + |
| 94 | +~~~PowerShell |
| 95 | +New-Turtle | Move-Turtle Square 10 | Save-Turtle ./Square.svg |
| 96 | +~~~ |
| 97 | +We can use loops: |
| 98 | + |
| 99 | +~~~PowerShell |
| 100 | +$turtle = New-Turtle |
| 101 | +
|
| 102 | +foreach ($n in 1..6) { |
| 103 | + $turtle = $turtle.Forward(10).Rotate(60) |
| 104 | +} |
| 105 | +
|
| 106 | +$turtle | |
| 107 | + Save-Turtle "./Hexagon.svg" |
| 108 | +~~~ |
| 109 | + |
0 commit comments