moving turtle examples

This commit is contained in:
Peli de Halleux
2018-09-11 09:02:40 -07:00
parent e27232667d
commit fe444a8371
7 changed files with 85 additions and 39 deletions

View File

@ -0,0 +1,35 @@
# Turtle Scanner
## Setting up your project
This project uses an **microturtle** Extension. You'll need to add it to your project to make it work.
* create a new project
* click on the gearwheel menu and click **Extensions**
* select the **microturtle** extension
After the project reloads, you should see the **turtle** category in the blocks.
## Code
The turtle scans the display over and over again.
```blocks
turtle.setPosition(0, 0)
turtle.turnRight()
turtle.setSpeed(20)
basic.forever(() => {
turtle.forward(4)
turtle.turnRight()
turtle.forward(1)
turtle.turnRight()
turtle.forward(4)
turtle.turnLeft()
turtle.forward(1)
turtle.turnLeft()
})
```
```package
microturtle=github:Microsoft/pxt-microturtle#master
```

View File

@ -0,0 +1,35 @@
# Turtle Spiral
## Setting up your project
This project uses an **microturtle** Extension. You'll need to add it to your project to make it work.
* create a new project
* click on the gearwheel menu and click **Extensions**
* select the **microturtle** extension
After the project reloads, you should see the **turtle** category in the blocks.
## Code
A turtle that spirals into the center of the display and back out again.
```blocks
let index = 0
turtle.setPosition(0, 0)
turtle.turnRight()
basic.forever(() => {
for (let index = 0; index <= 4; index++) {
turtle.forward(4 - index)
turtle.turnRight()
}
for (let index = 0; index <= 4; index++) {
turtle.turnLeft()
turtle.back(index)
}
})
```
```package
microturtle=github:Microsoft/pxt-microturtle#master
```

View File

@ -0,0 +1,37 @@
# Turtle Square
## Setting up your project
This project uses an **microturtle** Extension. You'll need to add it to your project to make it work.
* create a new project
* click on the gearwheel menu and click **Extensions**
* select the **microturtle** extension
After the project reloads, you should see the **turtle** category in the blocks.
## Moving the turtle
Imagine that a virtual turtle, as big as an LED, that you can control with commands. It starts in the center of the screen facing up.
You can use the ``||turtle:forward||`` and ``||turtle::turnRight||`` to move and turn the turtle.
```blocks
turtle.forward(2)
turtle.turnRight()
```
## Drawing a square
Drawing a square can be decomposed into a sequence of moves and turns. Since this is quite repetitive, you can use a **for** loop to repeat code.
```blocks
for (let i = 0; i < 4; i++) {
turtle.forward(2)
turtle.turnRight()
}
```
```package
microturtle=github:Microsoft/pxt-microturtle#master
```

21
docs/projects/turtle.md Normal file
View File

@ -0,0 +1,21 @@
# Turtle
Turtle graphics on your @boardname@ screen!
## Turtle graphics
```codecard
[{
"name": "Square",
"description": "Move in a square",
"url": "/projects/turtle-square"
}, {
"name": "Spiral",
"description": "Move in a spiral",
"url": "/projects/turtle-spiral"
}, {
"name": "Scanner",
"description": "Scan down the screen",
"url": "/projects/turtle-scanner"
}]
```