Page 1 of 1

Please explain the cod line by line in main loop of c++ while (app.isOpen()) { Event event; while (app.po

Posted: Fri May 20, 2022 9:59 am
by answerhappygod
Please explain the cod line by line in main loop of c++
while (app.isOpen())
{
Event event;
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();

if (event.type == Event::KeyPressed)
if (event.key.code == Keyboard::Space)
{
bullet *b = new bullet();
b->settings(sBullet,p->x,p->y,p->angle,10);
entities.push_back(b);
}
}

if (Keyboard::isKeyPressed(Keyboard::Right)) p->angle+=3;
if (Keyboard::isKeyPressed(Keyboard::Left)) p->angle-=3;
if (Keyboard::isKeyPressed(Keyboard::Up)) p->thrust=true;
else p->thrust=false;


for(auto a:entities)
for(auto b:entities)
{
if (a->name=="asteroid" && b->name=="bullet")
if ( isCollide(a,b) )
{
a->life=false;
b->life=false;

Entity *e = new Entity();
e->settings(sExplosion,a->x,a->y);
e->name="explosion";
entities.push_back(e);


for(int i=0;i<2;i++)
{
if (a->R==15) continue;
Entity *e = new asteroid();
e->settings(sRock_small,a->x,a->y,rand()%360,15);
entities.push_back(e);
}

}

if (a->name=="player" && b->name=="asteroid")
if ( isCollide(a,b) )
{
b->life=false;

Entity *e = new Entity();
e->settings(sExplosion_ship,a->x,a->y);
e->name="explosion";
entities.push_back(e);

p->settings(sPlayer,W/2,H/2,0,20);
p->dx=0; p->dy=0;
}
}


if (p->thrust) p->anim = sPlayer_go;
else p->anim = sPlayer;


for(auto e:entities)
if (e->name=="explosion")
if (e->anim.isEnd()) e->life=0;

if (rand()%150==0)
{
asteroid *a = new asteroid();
a->settings(sRock, 0,rand()%H, rand()%360, 25);
entities.push_back(a);
}

for(auto i=entities.begin();i!=entities.end();)
{
Entity *e = *i;

e->update();
e->anim.update();

if (e->life==false) {i=entities.erase(i); delete e;}
else i++;
}