pxt-calliope/docs/lessons/guess-the-number/activity.md

46 lines
815 B
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# guess the number activity
Guess the number with math random.
## ~avatar avatar
2016-03-26 00:47:20 +01:00
Welcome! This tutorial will help you create a guess the number game! Let's get started!
## ~
2016-03-26 00:47:20 +01:00
Add an event handler when button `A` is pressed.
```blocks
input.onButtonPressed(Button.A, () => {
})
```
2016-05-09 19:32:02 +02:00
Create a local variable of type number `x` and set it to a random number using `pick random`. `pick random` 9 generates a random number between `0` and `9`.
2016-03-26 00:47:20 +01:00
```blocks
input.onButtonPressed(Button.A, () => {
2018-04-21 19:52:09 +02:00
let x = Math.randomInt(10)
2016-03-26 00:47:20 +01:00
})
```
Show the random number on the screen.
```blocks
input.onButtonPressed(Button.A, () => {
2018-04-21 19:52:09 +02:00
let x = Math.randomInt(10)
2016-03-26 00:47:20 +01:00
basic.showNumber(x)
})
```
## ~avatar avatar
2016-03-26 00:47:20 +01:00
2016-04-13 17:27:45 +02:00
Excellent, you're ready to continue with the [challenges](/lessons/guess-the-number/challenges)!
2016-03-26 00:47:20 +01:00
## ~
2016-03-26 00:47:20 +01:00