int main()
{
RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");
Event Event;
bullet bullet;
App.SetFramerateLimit(60);
//----------------------------------------------------------------------------------------------------------------
Image imgbox,imghero;
Sprite sprbox,sprhero;
int HeroHP=100,EnemyHP=100 , HeroDmg=10,EnemyDmg=10;
imgbox.LoadFromFile("e.png");
imgbox.CreateMaskFromColor(Color(255,0,255));
imghero.LoadFromFile("h.png");
imghero.CreateMaskFromColor(Color(255,0,255));
sprbox.SetImage(imgbox);
sprhero.SetImage(imghero);
sprbox.SetPosition(20,200);
bullet.Loadfile();
//----------------------------------------------------------------------------------------------------------------
while (App.IsOpened())
{
float ElapsedTime = App.GetFrameTime();
while (App.GetEvent(Event))
{
if (Event.Type == Event::Closed)
App.Close();
// Escape key pressed
if ((Event.Type == Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
if (Event.Type == sf::Event::KeyReleased && Event.Key.Code == sf::Key::Space)
bullet.moveme(App,ElapsedTime);
}
if (App.GetInput().IsKeyDown(Key::Right)) sprhero.Move(100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(Key::Left)) sprhero.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(Key::Up)) sprhero.Move( 0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(Key::Down)) sprhero.Move(0, 100 * ElapsedTime);
App.Clear();
int x=10;
bullet.DrawBullt(App);
if(EnemyHP>=0)
{
if(CheckCollision(sprhero,sprbox))
EnemyHP=EnemyHP-HeroDmg;
int BoxX = sprbox.GetPosition().x;
int BoxY = sprbox.GetPosition().y;
int HeroX = sprhero.GetPosition().x;
int HeroY = sprhero.GetPosition().y;
std::cout<< "HeroHP= " << HeroHP << "\t" << "EnemyHP= " << EnemyHP << "\t" << "X= " << BoxX << "\t" << "Y=" << BoxY << "\t"<< std::endl;
/*
//------------------------ Make enemy follow Hero -------------------------------------------------
if(BoxX<HeroX)
sprbox.Move(20 * ElapsedTime, 0);
if(BoxY<HeroY)
sprbox.Move(0, 20 * ElapsedTime);
if(BoxY>HeroY)
sprbox.Move(0, -20 * ElapsedTime);
if(BoxX>HeroX)
sprbox.Move(-20 * ElapsedTime, 0);
//------------------------ Make enemy follow Hero -------------------------------------------------
*/
int dir=1;
if(BoxX>=700)
{
dir=-1;
//sprbox.Move(100 * dir * ElapsedTime, 0);
}
if(BoxX<=20)
{
dir=1;
sprbox.Move(100 * dir * ElapsedTime, 0);
}
App.Draw(sprbox);
}
App.Draw(sprhero);
App.Display();
}
return 0;
}