pxt-calliope/docs/reference/game/game-library.md

154 строки
3.7 KiB
Markdown
Исходник Обычный вид История

2016-03-26 02:47:20 +03:00
# Game Library
The game library supports simple single-player time-based games. The player has a **sprite**, number of **lives** and a **score**. The game has a sprite, number of **levels** and a **countdown clock**. The general goal of a game will be to move the sprite and achieve a top score before time runs out or the number of lives goes to zero.
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
2016-06-02 07:19:16 +03:00
```blocks
input.onButtonPressed(Button.A, () => {
2016-03-26 02:47:20 +03:00
game.addScore(1)
})
game.startCountdown(10000)
```
2016-04-13 18:27:45 +03:00
### [Create sprite](/functions/game-library/create-sprite)
2016-03-26 02:47:20 +03:00
Create sprite with x, y coordinates and returns a LED Sprite. Create a new LED sprite.
![](/static/mb/create-sprite-0.png)
```
export function createSprite(x: number, y: number) : micro_bitSprites.LedSprite
```
2016-04-13 18:27:45 +03:00
### [Move](/functions/game-library/move)
2016-03-26 02:47:20 +03:00
Sprite move by a certain number
![](/static/mb/game-library/move-0.png)
```
export function move(_this: micro_bitSprites.LedSprite, leds: number)
```
2016-04-13 18:27:45 +03:00
### [Turn](/functions/game-library/turn)
2016-03-26 02:47:20 +03:00
Rotates a sprite to the right by a certain number of degrees
![](/static/mb/game-library/turn-0.png)
```
export function turnRight(_this: micro_bitSprites.LedSprite, degrees: number)
```
Rotates a sprite to the left by a certain number of degrees
```
export function turnLeft(_this: micro_bitSprites.LedSprite, degrees: number)
```
2016-04-13 18:27:45 +03:00
### [Change](/functions/game-library/change)
2016-03-26 02:47:20 +03:00
Sprite will change the x position by this number
![](/static/mb/change-0.png)
```
export function changeXBy(_this: micro_bitSprites.LedSprite, x: number)
```
Sprite will change the y position by this number
```
export function changeYBy(_this: micro_bitSprites.LedSprite, y: number)
```
2016-04-13 18:27:45 +03:00
### [Set](/functions/game-library/set)
2016-03-26 02:47:20 +03:00
Sprite will change the x position by this number
```
export function setX(_this: micro_bitSprites.LedSprite, x: number)
```
Sprite will change the y position by this number
![](/static/mb/change-0.png)
```
export function changeYBy(_this: micro_bitSprites.LedSprite, y: number)
```
2016-04-13 18:27:45 +03:00
### [If on edge, bounce](/functions/game-library/if-on-edge-bounce)
2016-03-26 02:47:20 +03:00
Sprite - If the sprite is on the edge, the sprite will bounce
![](/static/mb/game-library/if-on-edge-bounce-0.png)
```
export function ifOnEdge_Bounce(_this: micro_bitSprites.LedSprite)
```
2016-04-13 18:27:45 +03:00
### [Change score by](/functions/game-library/change-score-by)
2016-03-26 02:47:20 +03:00
When a player achieves a goal, you can increase the game score
* add score points to the current score
![](/static/mb/game-library/pic1.png)
```
export function addScore(points: number)
```
2016-04-13 18:27:45 +03:00
### [Score](/functions/game-library/score)
2016-03-26 02:47:20 +03:00
* set the current score to a particular value.
```
export function setScore(value: number)
```
* get the current score value
![](/static/mb/game-library/pic2.png)
```
export function score() : number
```
2016-04-13 18:27:45 +03:00
### [Countdown](/functions/game-library/start-countdown)
2016-03-26 02:47:20 +03:00
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
* start a countdown with the maximum duration of the game in milliseconds.
![](/static/mb/game-library/pic3.png)
```
export function startCountdown(ms: number)
```
2016-04-13 18:27:45 +03:00
### [Game over](/functions/game-library/game-over)
2016-03-26 02:47:20 +03:00
If the `life` reaches zero or the time expires (see countdown), the game enters the **game over** mode. When the game is over, `game->is running` returns false
* check if the game still running.
```
let running = game.isRunning()
```
You can also end the game by calling the `game -> game over` function:
![](/static/mb/game-library/pic0.png)
```
game.gameOver()
```
### Lessons
2016-04-13 18:27:45 +03:00
[game of chance](/lessons/game-of-chance) | [game counter](/lessons/game-counter)
2016-03-26 02:47:20 +03:00