Game score fix (#543)

* guard against spurious 'add score' animations

* don't render sprites while background animation

* switch to bw while doing score animation

* clean up display mode

* updated enum declarations
This commit is contained in:
Peli de Halleux 2017-09-25 10:54:40 -07:00 committed by GitHub
parent bf658571cd
commit fa8dfd1dc3
3 changed files with 50 additions and 40 deletions

40
libs/core/enums.d.ts vendored
View File

@ -5,7 +5,7 @@ declare namespace basic {
} }
declare enum Button { declare const enum Button {
A = 1, // MICROBIT_ID_BUTTON_A A = 1, // MICROBIT_ID_BUTTON_A
B = 2, // MICROBIT_ID_BUTTON_B B = 2, // MICROBIT_ID_BUTTON_B
//% block="A+B" //% block="A+B"
@ -13,7 +13,7 @@ declare namespace basic {
} }
declare enum Dimension { declare const enum Dimension {
//% block=x //% block=x
X = 0, X = 0,
//% block=y //% block=y
@ -25,7 +25,7 @@ declare namespace basic {
} }
declare enum Rotation { declare const enum Rotation {
//% block=pitch //% block=pitch
Pitch = 0, Pitch = 0,
//% block=roll //% block=roll
@ -33,14 +33,14 @@ declare namespace basic {
} }
declare enum TouchPin { declare const enum TouchPin {
P0 = 7, // MICROBIT_ID_IO_P0 P0 = 7, // MICROBIT_ID_IO_P0
P1 = 8, // MICROBIT_ID_IO_P1 P1 = 8, // MICROBIT_ID_IO_P1
P2 = 9, // MICROBIT_ID_IO_P2 P2 = 9, // MICROBIT_ID_IO_P2
} }
declare enum AcceleratorRange { declare const enum AcceleratorRange {
/** /**
* The accelerator measures forces up to 1 gravity * The accelerator measures forces up to 1 gravity
*/ */
@ -64,7 +64,7 @@ declare namespace basic {
} }
declare enum Gesture { declare const enum Gesture {
/** /**
* Raised when shaken * Raised when shaken
*/ */
@ -123,7 +123,7 @@ declare namespace basic {
} }
declare enum MesDpadButtonInfo { declare const enum MesDpadButtonInfo {
//% block="A down" //% block="A down"
ADown = 1, // MES_DPAD_BUTTON_A_DOWN ADown = 1, // MES_DPAD_BUTTON_A_DOWN
//% block="A up" //% block="A up"
@ -165,7 +165,7 @@ declare namespace input {
* How to create the event. * How to create the event.
*/ */
declare enum EventCreationMode { declare const enum EventCreationMode {
/** /**
* MicroBitEvent is initialised, and no further processing takes place. * MicroBitEvent is initialised, and no further processing takes place.
*/ */
@ -177,7 +177,7 @@ declare namespace input {
} }
declare enum EventBusSource { declare const enum EventBusSource {
//% blockIdentity="control.eventSourceId" //% blockIdentity="control.eventSourceId"
MICROBIT_ID_BUTTON_A = 1, // MICROBIT_ID_BUTTON_A MICROBIT_ID_BUTTON_A = 1, // MICROBIT_ID_BUTTON_A
//% blockIdentity="control.eventSourceId" //% blockIdentity="control.eventSourceId"
@ -239,7 +239,7 @@ declare namespace input {
} }
declare enum EventBusValue { declare const enum EventBusValue {
//% blockIdentity="control.eventValueId" //% blockIdentity="control.eventValueId"
MICROBIT_EVT_ANY = 0, // MICROBIT_EVT_ANY MICROBIT_EVT_ANY = 0, // MICROBIT_EVT_ANY
//% blockIdentity="control.eventValueId" //% blockIdentity="control.eventValueId"
@ -367,7 +367,7 @@ declare namespace control {
} }
declare enum DisplayMode { declare const enum DisplayMode {
//% block="black and white" //% block="black and white"
BackAndWhite = 0, // DISPLAY_MODE_BLACK_AND_WHITE BackAndWhite = 0, // DISPLAY_MODE_BLACK_AND_WHITE
//% block="greyscale" //% block="greyscale"
@ -378,7 +378,7 @@ declare namespace led {
} }
declare enum DigitalPin { declare const enum DigitalPin {
P0 = 7, // MICROBIT_ID_IO_P0 P0 = 7, // MICROBIT_ID_IO_P0
P1 = 8, // MICROBIT_ID_IO_P1 P1 = 8, // MICROBIT_ID_IO_P1
P2 = 9, // MICROBIT_ID_IO_P2 P2 = 9, // MICROBIT_ID_IO_P2
@ -401,7 +401,7 @@ declare namespace led {
} }
declare enum AnalogPin { declare const enum AnalogPin {
P0 = 7, // MICROBIT_ID_IO_P0 P0 = 7, // MICROBIT_ID_IO_P0
P1 = 8, // MICROBIT_ID_IO_P1 P1 = 8, // MICROBIT_ID_IO_P1
P2 = 9, // MICROBIT_ID_IO_P2 P2 = 9, // MICROBIT_ID_IO_P2
@ -437,7 +437,7 @@ declare namespace led {
} }
declare enum PulseValue { declare const enum PulseValue {
//% block=high //% block=high
High = 4, // MICROBIT_PIN_EVT_PULSE_HI High = 4, // MICROBIT_PIN_EVT_PULSE_HI
//% block=low //% block=low
@ -445,7 +445,7 @@ declare namespace led {
} }
declare enum PinPullMode { declare const enum PinPullMode {
//% block="down" //% block="down"
PullDown = 0, PullDown = 0,
//% block="up" //% block="up"
@ -455,7 +455,7 @@ declare namespace led {
} }
declare enum PinEventType { declare const enum PinEventType {
//% block="edge" //% block="edge"
Edge = 1, // MICROBIT_PIN_EVENT_ON_EDGE Edge = 1, // MICROBIT_PIN_EVENT_ON_EDGE
//% block="pulse" //% block="pulse"
@ -467,7 +467,7 @@ declare namespace led {
} }
declare enum SerialPin { declare const enum SerialPin {
P0 = 7, // MICROBIT_ID_IO_P0 P0 = 7, // MICROBIT_ID_IO_P0
P1 = 8, // MICROBIT_ID_IO_P1 P1 = 8, // MICROBIT_ID_IO_P1
P2 = 9, // MICROBIT_ID_IO_P2 P2 = 9, // MICROBIT_ID_IO_P2
@ -480,7 +480,7 @@ declare namespace led {
} }
declare enum BaudRate { declare const enum BaudRate {
//% block=115200 //% block=115200
BaudRate115200 = 115200, BaudRate115200 = 115200,
//% block=57600 //% block=57600
@ -508,7 +508,7 @@ declare namespace led {
} }
declare enum Delimiters { declare const enum Delimiters {
//% block="new line" //% block="new line"
NewLine = 1, NewLine = 1,
//% block="," //% block=","
@ -526,7 +526,7 @@ declare namespace serial {
} }
declare enum NumberFormat { declare const enum NumberFormat {
Int8LE = 1, Int8LE = 1,
UInt8LE = 2, UInt8LE = 2,
Int16LE = 3, Int16LE = 3,

View File

@ -35,6 +35,7 @@ namespace game {
let _img: Image; let _img: Image;
let _sprites: LedSprite[]; let _sprites: LedSprite[];
let _paused: boolean = false; let _paused: boolean = false;
let _backgroundAnimation = false; // indicates if an auxiliary animation (and fiber) is already running
/** /**
* Creates a new LED sprite pointing to the right. * Creates a new LED sprite pointing to the right.
@ -68,15 +69,23 @@ namespace game {
//% parts="ledmatrix" //% parts="ledmatrix"
export function addScore(points: number): void { export function addScore(points: number): void {
setScore(_score + points); setScore(_score + points);
if (!_paused) if (!_paused && !_backgroundAnimation) {
_backgroundAnimation = true;
control.inBackground(() => { control.inBackground(() => {
led.stopAnimation(); led.stopAnimation();
const dm = led.displayMode();
if (dm != DisplayMode.BackAndWhite)
led.setDisplayMode(DisplayMode.BackAndWhite);
basic.showAnimation(`0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 basic.showAnimation(`0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0`, 20); 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0`, 20);
if (dm != DisplayMode.BackAndWhite)
led.setDisplayMode(dm);
_backgroundAnimation = false;
}); });
}
} }
/** /**
@ -731,22 +740,22 @@ namespace game {
} }
function init(): void { function init(): void {
if (_img == null) { if (_img) return;
_img = images.createImage( const img = images.createImage(
`0 0 0 0 0 `0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0`); 0 0 0 0 0`);
_sprites = (<LedSprite[]>[]); _sprites = (<LedSprite[]>[]);
basic.forever(() => { basic.forever(() => {
basic.pause(30); basic.pause(30);
plot(); plot();
if (game.isGameOver()) { if (game.isGameOver()) {
basic.pause(600); basic.pause(600);
} }
}); });
} _img = img;
} }
/** /**
@ -754,14 +763,15 @@ namespace game {
*/ */
//% parts="ledmatrix" //% parts="ledmatrix"
function plot(): void { function plot(): void {
if (game.isGameOver() || game.isPaused() || !_img) { if (game.isGameOver() || game.isPaused() || !_img || _backgroundAnimation) {
return; return;
} }
// ensure greyscale mode // ensure greyscale mode
if (led.displayMode() != DisplayMode.Greyscale) const dm = led.displayMode();
if (dm != DisplayMode.Greyscale)
led.setDisplayMode(DisplayMode.Greyscale); led.setDisplayMode(DisplayMode.Greyscale);
// render sprites // render sprites
let now = input.runningTime(); const now = input.runningTime();
_img.clear(); _img.clear();
for (let i = 0; i < _sprites.length; i++) { for (let i = 0; i < _sprites.length; i++) {
_sprites[i]._plot(now); _sprites[i]._plot(now);

View File

@ -1,7 +1,7 @@
// Auto-generated. Do not edit. // Auto-generated. Do not edit.
declare enum MesCameraEvent { declare const enum MesCameraEvent {
//% block="take photo" //% block="take photo"
TakePhoto = 3, // MES_CAMERA_EVT_TAKE_PHOTO TakePhoto = 3, // MES_CAMERA_EVT_TAKE_PHOTO
//% block="start video capture" //% block="start video capture"
@ -21,7 +21,7 @@
} }
declare enum MesAlertEvent { declare const enum MesAlertEvent {
//% block="display toast" //% block="display toast"
DisplayToast = 1, // MES_ALERT_EVT_DISPLAY_TOAST DisplayToast = 1, // MES_ALERT_EVT_DISPLAY_TOAST
//% block="vibrate" //% block="vibrate"
@ -47,7 +47,7 @@
} }
declare enum MesDeviceInfo { declare const enum MesDeviceInfo {
//% block="incoming call" //% block="incoming call"
IncomingCall = 7, // MES_DEVICE_INCOMING_CALL IncomingCall = 7, // MES_DEVICE_INCOMING_CALL
//% block="incoming message" //% block="incoming message"
@ -65,7 +65,7 @@
} }
declare enum MesRemoteControlEvent { declare const enum MesRemoteControlEvent {
//% block="play" //% block="play"
play = 1, // MES_REMOTE_CONTROL_EVT_PLAY play = 1, // MES_REMOTE_CONTROL_EVT_PLAY
//% block="pause" //% block="pause"