C++ Game Part 1
C++ Game Part 1
hpp>//grafiki
#include <list>
#include <Windows.h>
class Animation
public:
Sprite sprite;
std::vector<IntRect> frames;
Animation(){}
Animation (Texture &t, int x, int y, int w, int h, int count, float Speed)
Frame = 0;
speed = Speed;
for (int i=0;i<count;i++)
frames.push_back( IntRect(x+i*w, y, w, h) );
sprite.setTexture(t);
sprite.setOrigin(w/2,h/2);
sprite.setTextureRect(frames[0]);
void update()
Frame += speed;
int n = frames.size();
bool isEnd()
return Frame+speed>=frames.size();
};
class Entity
public:
float x,y,dx,dy,R,angle;
bool life;
std::string name;
Animation anim;
Entity()
life=1;
anim = a;
x=X; y=Y;
angle = Angle;
R = radius;
anim.sprite.setPosition(x,y);
anim.sprite.setRotation(angle+90);
app.draw(anim.sprite);
CircleShape circle(R);
circle.setFillColor(Color(255,0,0,170));
circle.setPosition(x,y);
circle.setOrigin(R,R);
virtual ~Entity(){};
};
public:
asteroid()
dx=rand()%8-4;
dy=rand()%8-4;
name="asteroid";
}
void update()
x+=dx;
y+=dy;
};
public:
bullet()
name="bullet";
void update()
dx=cos(angle*DEGTORAD)*6;
dy=sin(angle*DEGTORAD)*6;
x+=dx;
y+=dy;
};
public:
bool thrust;
player()
name="player";
void update()
if (thrust)
{ dx+=cos(angle*DEGTORAD)*0.2;
dy+=sin(angle*DEGTORAD)*0.2; }
else
{ dx*=0.99;
dy*=0.99; }
int maxSpeed=15;
if (speed>maxSpeed)
{ dx *= maxSpeed/speed;
dy *= maxSpeed/speed; }
x+=dx;
y+=dy;
};
}
int main()
srand(time(0));
t1.loadFromFile("images/spaceship.png");//korab
t2.loadFromFile("images/background.jpg");//fon
t3.loadFromFile("images/explosions/type_C.png");//eksploziq
t4.loadFromFile("images/rock.png");//golqm kamuk
t5.loadFromFile("images/fire_blue.png");//lazer
t6.loadFromFile("images/rock_small.png");//maluk kamuk
t7.loadFromFile("images/explosions/type_B.png");//eksploziq
t1.setSmooth(true);
t2.setSmooth(true);
Sprite background(t2);
std::list<Entity*> entities;
for(int i=0;i<15;i++)
entities.push_back(a);
p->settings(sPlayer,200,200,0,20);
entities.push_back(p);
/////glaven cikul/////
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)//strelba
b->settings(sBullet,p->x,p->y,p->angle,10);
entities.push_back(b);
//////dvijenie///////
if (Keyboard::isKeyPressed(Keyboard::Right)) p->angle+=3;//lqvo
if (Keyboard::isKeyPressed(Keyboard::Left)) p->angle-=3;//dqsno
if (Keyboard::isKeyPressed(Keyboard::Up)) p->thrust=true;//uskorenie
else p->thrust=false;
for(auto a:entities)
for(auto b:entities)
{
if ( isCollide(a,b) )
a->life=false;
b->life=false;
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;
e->settings(sRock_small,a->x,a->y,rand()%360,15);
entities.push_back(e);
if ( isCollide(a,b) )
{
b->life=false;
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;
for(auto e:entities)
if (e->name=="explosion")
if (e->anim.isEnd()) e->life=0;
if (rand()%150==0)
entities.push_back(a);
for(auto i=entities.begin();i!=entities.end();)
Entity *e = *i;
e->update();
e->anim.update();
else i++;
app.draw(background);
for(auto i:entities)
i->draw(app);
app.display();
}
return 0;