如何为我的蛋捕手游戏创建落蛋?面向对象编程c++的新手
How to create falling eggs for my egg catcher game? New to object oriented programming c++
我已经成功地对移动的篮子和碰撞检测进行了编码,但我发现很难一个接一个地打印鸡蛋。我是面向对象编程的新手,我发现与普通函数相比,很难操作类。我想做的是让我的鸡蛋一个接一个地掉下来,而不是完全掉下来。旁注:我想在产卵之间加一个缓冲液。我该如何做到这一点?
#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;
}
}
相关文章:
- 分段错误(内核转储) C++面向对象编程
- 面向对象编程 - 位域私有结构的公共参考
- 面向对象编程和对象组合,如何放置组合属性?
- 面向对象编程错误
- 我如何向只用Fortran 77编码的人解释面向对象编程?
- 面向对象编程:在GUI中启动ab对象
- 指针和面向对象编程
- 如何为我的蛋捕手游戏创建落蛋?面向对象编程c++的新手
- 凯撒密码面向对象编程
- Haskell中的面向对象编程
- 使用 IGRAPH 面向对象编程
- 单元测试 非面向对象编程
- C++ 面向对象编程
- 面向对象编程风格
- 如何在C++中启动面向对象编程
- 在学习C#之前,必须先学习OOP(C++上的面向对象编程)
- 异常处理需要面向对象编程吗?
- 多文件和面向对象编程
- VBA面向对象编程
- 面向对象编程意味着只使用对象吗?