pxt-calliope/docs/projects/snap-the-dot.md

93 lines
2.5 KiB
Markdown
Raw Normal View History

# Snap the Dot
2018-09-14 20:22:08 +02:00
## Introduction @unplugged
2018-09-14 23:55:52 +02:00
Snap the dot is a game of skill where the player has to press **A** exactly when the dot reaches the center of the screen.
2018-09-14 20:22:08 +02:00
2018-09-14 23:55:52 +02:00
This tutorial shows how to use the game engine.
2018-09-14 20:22:08 +02:00
## Create a sprite @fullscreen
2018-09-14 23:55:52 +02:00
Drag a ``||game:create sprite||`` block onto the workspace. A sprite is a single pixel that can move on the screen. It has an ``x`` and ``y`` position along with a direction of motion.
2018-09-14 20:22:08 +02:00
```blocks
let sprite: game.LedSprite = null
sprite = game.createSprite(2, 2)
```
## Move the dot @fullscreen
2018-09-14 23:55:52 +02:00
The sprite starts in the center facing right. Put a ``||game:move||`` block into the ``||basic:forever||`` to make it move. Notice how it moves to the right but does not bounce back.
2018-09-14 20:22:08 +02:00
```blocks
let sprite: game.LedSprite = null
sprite = game.createSprite(2, 2)
basic.forever(function () {
sprite.move(1)
})
```
## Bounce @fullscreen
2018-09-14 23:55:52 +02:00
Grab a ``||game:if on edge, bounce||`` block to make the sprite bounce on the side of the screen. Also, add a ``||basic:pause||`` block to slow down the sprite.
2018-09-14 20:22:08 +02:00
```blocks
let sprite: game.LedSprite = null
sprite = game.createSprite(2, 2)
basic.forever(function () {
sprite.move(1)
sprite.ifOnEdgeBounce()
basic.pause(100)
})
```
## Test and download
2018-09-14 23:55:52 +02:00
Use the simulator to find the best speed. If you have a @boardname@, press ``|Download|`` to try it out on the device.
2018-09-14 20:22:08 +02:00
## Button handling @fullscreen
When **A** is pressed, we test if the sprite is in the center or not.
2018-09-14 23:55:52 +02:00
Use a ``||input:on button pressed||`` block to handle the **A** button. Put in a ``||logic:if||`` block and test if ``||game:x||`` is equal to `2`.
2018-09-14 20:22:08 +02:00
```blocks
let sprite: game.LedSprite = null
input.onButtonPressed(Button.A, function () {
if (sprite.get(LedSpriteProperty.X) == 2) {
} else {
}
})
sprite = game.createSprite(2, 2)
basic.forever(function () {
sprite.move(1)
basic.pause(100)
sprite.ifOnEdgeBounce()
})
```
2018-09-14 23:55:52 +02:00
2018-09-14 23:10:13 +02:00
## Score and game over
2018-09-14 20:22:08 +02:00
2018-09-14 23:55:52 +02:00
Finally, pull out an ``||game:add score||`` and a ``||game:game over||`` block to handle both success (sprite in the center) and failure (sprite not in the center).
2018-09-14 20:22:08 +02:00
```blocks
let sprite: game.LedSprite = null
input.onButtonPressed(Button.A, function () {
if (sprite.get(LedSpriteProperty.X) == 2) {
game.addScore(1)
} else {
game.gameOver()
}
})
sprite = game.createSprite(2, 2)
basic.forever(function () {
sprite.move(1)
basic.pause(100)
sprite.ifOnEdgeBounce()
})
```
## Test and download
2018-09-14 23:55:52 +02:00
Your game is ready! If you have a @boardname@, press ``|Download|`` to try it out on the device.