如何为我的蛋捕手游戏创建落蛋?面向对象编程c++的新手

How to create falling eggs for my egg catcher game? New to object oriented programming c++

本文关键字:面向对象编程 c++ 新手 创建 我的蛋 游戏 手游      更新时间:2023-10-16

我已经成功地对移动的篮子和碰撞检测进行了编码,但我发现很难一个接一个地打印鸡蛋。我是面向对象编程的新手,我发现与普通函数相比,很难操作类。我想做的是让我的鸡蛋一个接一个地掉下来,而不是完全掉下来。旁注:我想在产卵之间加一个缓冲液。我该如何做到这一点?

#define RIGHT  0
#define LEFT  1
#define number 10
int isalive = 0;
int score = 0;
int lives = 0;
int direction = 1;
class GameObject
{
public:
    int x, y;
    char symbol;
    virtual void draw() = 0;
};
class basket : public GameObject
{
public:  
    basket(char symbol, int x, int y)
    {
        this->symbol = symbol;
        this->x = x;
        this->y = y;
    }
    void draw()
    {
        for (int z = 0; z < 5; z++)
        {
            gotoxy(x + z, y);
            cout << symbol;
        }
    }
    void movement()
    {
        if (_kbhit())
        {
            char ch = _getch();
            switch (ch)
            {
            case 'a':
                direction = RIGHT;
                break;
            case 'd':
                direction = LEFT;
                break;
            }
        }
    }
    void bounce()
    {
        switch (direction)
        {
        case RIGHT: x--;
            break;
        case LEFT: x++;
            break;
        }
        if (x >= 75)
        {
            direction = RIGHT;
        }
        if (x <= 0)
        {
            direction = LEFT;
        }
    }
};
class egg : public GameObject
{   
private:
public:     
    int isalive;
    egg(char symbol,int y)
    {
        this->symbol = symbol;
        this->y = y;
    }
    ~egg(){}
    void movement()
    {
        y++;
        if (y > 23)
        {
        /*y = 0;*/
        lives = lives +1;
        isalive = 0;
    }
}
    void draw()
    {
        {
        gotoxy(x, y);
        cout << symbol;
        gotoxy(0,2);
        cout<<"Broken Eggs:"<<lives;
        gotoxy(40,2);
        cout<<"Score:"<<score;
        }
    }
    void checkcollison(basket *basket)
    {
    if (x == basket->x && y == basket->y)
        {
            y = 0;
            score += 50;
            isalive = 0; 
        }
    }
};

int main()
{
    basket *f[4];
    egg *eggs[number];
        for (int z = 0; z < 4; z++)
        {
        f[z]= new basket('X', 38+z, 23);
        }
        for(int y = 0; y<number; y++)
        {
            eggs[y] = new egg('0',0);
        }
            while (true)
        {
            clrscr();
            for (int z = 0; z < number;z++)
            {
                 if (eggs[z]->isalive ==0)  
                 {
                    eggs[z]->x = rand() %40+20;
                    eggs[z]->isalive = 1;
                  }
                 if(eggs[z]->isalive !=0)
                 {
                    eggs[z]->draw();
                    eggs[z]->movement();
                 }
            }
            for (int z = 0; z != 4; z++)
            {
                f[z]-> draw();
                if(kbhit())f[z]->movement();
                f[z]->bounce();
            }
        Sleep(100);
        }
        return 0;
}

一旦移动了1个鸡蛋,就可以打破for循环。在这种情况下,一旦鸡蛋移动,你就可以睡100毫秒。

例如。

for (int z = 0; z < number;z++)
{
    if (eggs[z]->isalive ==0)  
    {
        eggs[z]->x = rand() %40+20;
        eggs[z]->isalive = 1;
    }
    if(eggs[z]->isalive !=0)
    {
        eggs[z]->draw();
        eggs[z]->movement();
        break;
    }
}