0

使用 BBC 微型钻头。

微型钻头上有两个按钮,在我的代码中,按钮 A 用于偶数,按钮 B 用于负数。如果数字只有 1 或 0,则游戏运行良好,否则它会结束游戏,我不知道为什么了。

尝试仅使用数组中的 1 个数字并且它可以工作,或者甚至更改数组的顺序,数组中的第一个数字是唯一可以接受的数字。

#include "MicroBit.h"

MicroBit uBit;

bool game = true;
bool press = false;
int i;
int score;
int number;
int nextNumber;
int odd[5] = {1, 3, 5, 7, 9};
int even[5] = {0, 2, 4, 6, 8};

void endGame()
{
    game = false;
}

void evenNumber()
{
    if(number == even[i])
    {
        score++;
        //number = nextNumber;
    }
    else
    {
        endGame();
    }
}

void oddNumber()
{
    if(number == odd[i])
    {
        score++;
        //number = nextNumber;
    }
    else
    {
        endGame();
    }
}

void onButtonA(MicroBitEvent)
{
    evenNumber();
    press = true;
}

void onButtonB(MicroBitEvent)
{
    oddNumber();
    press = true;
}

void reset(MicroBitEvent)
{
    game = true;
}

int main()
{
    // Initialise the micro:bit runtime.
    uBit.init();
    // Random number between 0 - 10
    uBit.seedRandom(uBit.random(10));
    number = uBit.random(10);
    nextNumber = number;

    while (game == true)
    {
        press = false;
        number = nextNumber;
        nextNumber = uBit.random(10);
        // If numbers are the same, randomise again.
        while(nextNumber == number)
        {
            nextNumber =  uBit.random(10);
        }

        uBit.display.print(number);

        // Button press and shake
        uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
        uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
        uBit.messageBus.listen(MICROBIT_ID_ACCELEROMETER, MICROBIT_ACCELEROMETER_EVT_SHAKE, reset);

        while (press == false){
            uBit.sleep(100);
        }
    }

    // Make sure we don't exit main
    uBit.sleep(1000);

    // Shows score
    uBit.display.scroll("YOUR SCORE IS:", 80);
    uBit.display.scroll(score, 80);

    release_fiber();
}

所有最多 8 的偶数都应该在按钮单击 A 上工作,而在按钮单击 B 上的奇数最多 9。

4

1 回答 1

1

欢迎来到 Stackoverflow。我复制了你的问题。这是一些对我有用的代码。我删除了一些不再需要的行。正如@RetiredNinja 所说,您需要更改检查奇数和偶数的方式。

#include "MicroBit.h"

MicroBit uBit;

bool game = true;
bool press = false;
int score;
int number;
int nextNumber;

void endGame()
{
    game = false;
}

void evenNumber()
{
    if(number%2 == 0)
    {
        score++;
        //number = nextNumber;
    }
    else
    {
        endGame();
    }
}

void oddNumber()
{
    if(number%2 == 1)
    {
        score++;
        //number = nextNumber;
    }
    else
    {
        endGame();
    }
}

void onButtonA(MicroBitEvent)
{
    evenNumber();
    press = true;
}

void onButtonB(MicroBitEvent)
{
    oddNumber();
    press = true;
}

void reset(MicroBitEvent)
{
    game = true;
}

int main()
{
    // Initialise the micro:bit runtime.
    uBit.init();
    // Random number between 0 - 10
    uBit.seedRandom(uBit.random(10));
    number = uBit.random(10);
    nextNumber = number;

    while (game == true)
    {
        press = false;
        number = nextNumber;
        nextNumber = uBit.random(10);
        // If numbers are the same, randomise again.
        while(nextNumber == number)
        {
            nextNumber =  uBit.random(10);
        }

        uBit.display.print(number);

        // Button press and shake
        uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
        uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
        uBit.messageBus.listen(MICROBIT_ID_ACCELEROMETER, MICROBIT_ACCELEROMETER_EVT_SHAKE, reset);

        while (press == false){
            uBit.sleep(100);
        }
    }

    // Make sure we don't exit main
    uBit.sleep(1000);

    // Shows score
    uBit.display.scroll("YOUR SCORE IS:", 80);
    uBit.display.scroll(score, 80);

    release_fiber();
}

于 2019-02-10T20:04:59.443 回答