/*
in a game , I always restore some enemies in a container ---it may be a linked list or
a ordered-list ,and then I get all the element in the container and check them with
the player object ---collision check --if check ok, then delete the enemy or make
the player dead.
Delete element.
Add element
Get element
*/
#include
#include
#include
#include
using namespace std;
struct Point
{
int m_x;
int m_y;
Point()
{
m_x = 0;
m_y = 0;
}
};
const int ENEMY_TYPE = 3;
const int ENEMY_SIZE[ENEMY_TYPE][2] = {{10,10},{20,20},{30,30}};
const int GAME_AREA_MINX = 0;
const int GAME_AREA_MINY = 0;
const int GAME_AREA_MAXX = 600;
const int GAME_AREA_MAXY = 400;
class CSprite
{
public:
CSprite( int type, int x, int y );
~CSprite();
Point *GetPos() { return &m_pos; }
int GetWidth() { return m_width; }
int GetHeight() { return m_height; }
void Move() { m_pos.m_x += m_vx; m_pos.m_y += m_vy; }
protected:
Point m_pos;
int m_width;
int m_height;
int m_vx;
int m_vy;
int m_type;
};
CSprite::CSprite( int type, int x, int y )
{
m_type = type;
m_pos.m_x = x;
m_pos.m_y = y;
m_vx = rand() % 8 - 4;
m_vy = rand() % 10;
m_width = ENEMY_SIZE[type][0];
m_height = ENEMY_SIZE[type][1];
}
CSprite::~CSprite()
{
}
//becuase they are pointers, so i donot need create CSprite(const CSprite&),and
//operator etc.
list < CSprite*> g_enemyContainer;
CSprite *g_player;
void CreateEnemy();
bool CollisionCheck( CSprite *enemy, CSprite *player );
int main()
{
cout << "Game Init" << endl;
g_player = new CSprite( 1, GAME_AREA_MAXX / 2, GAME_AREA_MAXY / 2 );
srand( time( NULL ) );
cout << "Game Play" << endl;
long time = rand() % 1000 + 500; //run time
long curTime = 0;
while( curTime < time )
{
//update the game time
curTime ++;
CreateEnemy();
//collision check
for( list < CSprite *>::iterator i = g_enemyContainer.begin(); i != g_enemyContainer.end(); ++i )
{
if( g_enemyContainer.empty() ) break;
if( CollisionCheck( *i, g_player ) )
{
//delete the enemy
cout << "####################Collision Check OK##################" << endl;
cout << "********************Delete An Enemy********************" << endl;
delete (*i);
i = g_enemyContainer.erase( i );
}
}
//move enemy
for( list
{
if( g_enemyContainer.empty() ) break;
(*i)->Move();
//check it whether is out of the game area
Point *p = (*i)->GetPos();
if( p->m_x <= GAME_AREA_MINX ||
p->m_x >= GAME_AREA_MAXX ||
p->m_y <= GAME_AREA_MINY ||
p->m_y >= GAME_AREA_MAXY )
{
cout << "####################Out of Area##################" << endl;
cout << "********************Delete An Enemy********************" << endl;
delete (*i);
i = g_enemyContainer.erase( i );
}
}
}
cout << "Game Release" << endl;
delete g_player;
for( list < CSprite* > ::iterator i = g_enemyContainer.begin(); i != g_enemyContainer.end(); ++i )
{
if( g_enemyContainer.empty() ) break;
delete (*i );
}
g_enemyContainer.clear();
cout << "Game End" << endl;
return 0;
}
void CreateEnemy()
{
if( rand() % 100 > 90 )
{
cout << "==================Create An Enemy=================" << endl;
int x = rand() % GAME_AREA_MAXX;
int y = rand() % GAME_AREA_MAXY;
CSprite *enemy = new CSprite( rand() % 3, x, y );
//then ,add it into the list
g_enemyContainer.push_back( enemy );
}
}
bool CollisionCheck( CSprite *enemy, CSprite *player )
{
Point *e = enemy->GetPos();
Point *p = player->GetPos();
if( e->m_x >= p->m_x - enemy->GetWidth() &&
e->m_x <= p->m_x + player->GetWidth() &&
e->m_y >= p->m_y - enemy->GetHeight() &&
e->m_y <= p->m_y + player->GetHeight() )
{
return true;
}
return false;
}