Jump to content
  • Advertisement
Sign in to follow this  
scorpx333

problem with asteroid clone game

This topic is 2581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi to all, i am making an asteroid game clone, using C++ and allegro5. i got into a big problem( if it will be solved the hardest part will be over, so please help)

the problem is:
when i shoot the asteroid it is being erased from the asteroid list and 3 new ones created instead (till size becomes 0 )
when i don't use function in asteroid class that redraws asteroids as soon as they x and y position is not on screen, everything works, but asteroids go off screen so i can't shoot them anymore.
if i use the function it gives 3 new asteroids position (0,0) and they are left there without movement.

here is the code in main



list<asteroid>::iterator iter;
list<bullet>::iterator bulliter;

iter = AsteroidList.begin();

while(iter != AsteroidList.end())


asteroid& s = *iter;
s.AsteroidVsScreen();// the problematic function that gives 0 0 for 3 smaller asteroids positions



if (s.getAsteroidSize() == 3) { TempAsteroidSize = 25; }
if (s.getAsteroidSize() == 2) { TempAsteroidSize = 12; }
if (s.getAsteroidSize() == 1) { TempAsteroidSize = 6; }


bulliter = BulletList.begin();
while(bulliter != BulletList.end())
{
bullet& bull = *bulliter;


if (bull.getBulletXPosition() > s.getAsteroidXPosition()-TempAsteroidSize &&
bull.getBulletXPosition() < s.getAsteroidXPosition() + TempAsteroidSize &&
bull.getBulletYPosition() > s.getAsteroidYPosition() -TempAsteroidSize&&
bull.getBulletYPosition() < s.getAsteroidYPosition() + TempAsteroidSize)
{
tempSize = s.getAsteroidSize() - 1;
tempx = s.getAsteroidXPosition();
tempy = s.getAsteroidYPosition();

s.~asteroid();



iter = AsteroidList.erase(iter);//if colision asteroid is errased
bulliter = BulletList.erase(bulliter);
collision=true;// withaut this the iter++ will make an error
if (tempSize > 0)
{
BreakingInto_3=true;//going to the end of while loop
}

}
else{bulliter++;}

}
if (!collision){iter++;}
if (collision){collision=false; score+=1;} // the iter error solved
}// while asteroids tier goes on



and now the problematic class and its function :



asteroid::asteroid()
{
dead = false;
asteroidSize = 3;
Width=900;
Height=600;



xspeed= rand() % 5 - 3;
yspeed= rand() % 5 - 3;

xposition = rand() % (Width - 100);
yposition = rand() % (Height - 100);
if (xposition > Width/2 - 200 && xposition < Width/2 + 200 && yposition > Height/2 -200 && yposition < Height + 200)
{
xposition = 33;
yposition = 33;
}

Ast_angl=0;

}



asteroid::asteroid(int size, int x, int y)
{
asteroidSize = size;
xposition = x;
yposition = y;

if (xposition > Width/2 - 200 && xposition < Width/2 + 200 && yposition > Height/2 -200 && yposition < Height + 200)
{
xposition = 33;
yposition = 33;
}

dead = false;

xspeed= rand() % 5 - 3;
yspeed= rand() % 5 - 3;


Ast_angl=0;
}

int asteroid::getAsteroidXPosition()
{
return xposition;
}


int asteroid::getAsteroidYPosition()
{
return yposition;
}


int asteroid::getAsteroidSize()
{
return asteroidSize;
}


void asteroid::setAsteroidDead()
{
dead = true;
}


void asteroid::refreshAsteroid(ALLEGRO_BITMAP *Asteroid,ALLEGRO_FONT *font24)
{


if (dead) return;


xposition = xposition + xspeed;
yposition = yposition + yspeed;
Ast_angl+=0.03;

if (asteroidSize == 3)
{
al_draw_scaled_rotated_bitmap(Asteroid,50,50,xposition,yposition,1,1,Ast_angl,0);
//al_draw_rectangle(xposition-25,yposition-25,xposition+25,yposition+25,al_map_rgb(255,0,0),3);
}
if (asteroidSize == 2)
{
al_draw_scaled_rotated_bitmap(Asteroid,50,50,xposition,yposition,0.5,0.5,Ast_angl,0);
//al_draw_rectangle(xposition-12,yposition-12,xposition+12,yposition+12,al_map_rgb(255,0,0),3);
}
if (asteroidSize == 1)
{
al_draw_scaled_rotated_bitmap(Asteroid,50,50,xposition,yposition,0.25,0.25,Ast_angl,0);
//al_draw_rectangle(xposition-6,yposition-6,xposition+6,yposition+6,al_map_rgb(255,0,0),3);

}
if (asteroidSize == 0)
{
dead = true;
return;
}
}

void asteroid::AsteroidVsScreen()// when standart asteroids are created everything works, when 3 smaller are created this function gives (0,0)
{

if(xposition > Width)
{
xposition =0 ;
}

if(xposition < 0)
{
xposition = Width;
}

if(yposition > Height)
{
yposition =0;
}

if(yposition < 0)
{
yposition = Height;
}


}

asteroid::~asteroid()
{
}




even if you don't know what exactly is wrong with it please tell any of your suggestions for my game

Share this post


Link to post
Share on other sites
Advertisement
I can't see where you are creating the 3 asteroids. If you could post the full "main" code that would be better.

I also recommend that you format your code into a more readable style, using indentation. Look at this example: Coding Styles

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!