Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help with array in Space Invaders clone

Started by AlienCharm Nov 22, 2003 at 2:52 PM 2 replies 1k views
Original Post
AlienCharm
AlienCharm
I am making a Space Invaders clone for my final project in Visual C++/MFC. The lil tank moves and fires, I even have the "invaders" on the screen and moving back and forth. As of right now i display the invaders using a for loop. Here is the code I use:
// draws the first row of invaders, the "red invaders"

void CSpace_Invaders_CloneView::DrawFirstSet()
{
	CBitmap bmp;

	// chooses which bitmap to load

	if( m_blInvaderState == false )
	{
		bmp.LoadBitmap( IDB_TOP_INVADER_CLOSE );
	}
	else
	{
		bmp.LoadBitmap( IDB_TOP_INVADER_OPEN );
	}
	
	CClientDC dc(this);
	CDC bmDC;
	bmDC.CreateCompatibleDC(&dc);
	CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

	BITMAP  bi;
	bmp.GetBitmap(&bi);

	for( int i = 0; i < 550; i += 50 )
	{
		dc.BitBlt(( 5 + i ) + m_nMoveInvaderX,60 + m_nMoveInvaderY,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
	}

	bmDC.SelectObject(pOldbmp);
}

// draws the second and third rows of invaders, the "blue invaders"

void CSpace_Invaders_CloneView::DrawSecondSet()
{
	CBitmap bmp;

	// chooses which bitmap to load

	if( m_blInvaderState == false )
	{
		bmp.LoadBitmap( IDB_MIDDLE_INVADER_CLOSE );
	}
	else
	{
		bmp.LoadBitmap( IDB_MIDDLE_INVADER_OPEN );
	}
	  
	CClientDC dc(this);
	CDC bmDC;
	bmDC.CreateCompatibleDC(&dc);
	CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

	BITMAP  bi;
	bmp.GetBitmap(&bi);

	for( int i = 0; i < 550; i += 50 )
	{
		dc.BitBlt(( 0 + i ) + m_nMoveInvaderX,100 + m_nMoveInvaderY,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
		dc.BitBlt(( 0 + i ) + m_nMoveInvaderX,140 + m_nMoveInvaderY,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
	}

	bmDC.SelectObject(pOldbmp);
}

// draws the fourth and fifth rows of invaders, the "white invaders"

void CSpace_Invaders_CloneView::DrawThirdSet()
{
	CBitmap bmp;

	// chooses which bitmap to load

	if( m_blInvaderState == false )
	{
		bmp.LoadBitmap( IDB_BOTTOM_INVADER_CLOSE );
	}
	else
	{
		bmp.LoadBitmap( IDB_BOTTOM_INVADER_OPEN );
	}
   
	CClientDC dc(this);
	CDC bmDC;
	bmDC.CreateCompatibleDC(&dc);
	CBitmap *pOldbmp = bmDC.SelectObject(&bmp);

	BITMAP  bi;
	bmp.GetBitmap(&bi);

	for( int i = 0; i < 550; i += 50 )
	{
		dc.BitBlt(( 0 + i ) + m_nMoveInvaderX,180 + m_nMoveInvaderY,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
		dc.BitBlt(( 0 + i ) + m_nMoveInvaderX,220 + m_nMoveInvaderY,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
	}

	bmDC.SelectObject(pOldbmp);
}
It all works but I don''t think I can do collision detection going about it this way. Everything I have read suggests using a two dimensional array. The only problem is i have no clue as to what to put in the array. Since I am using MFC I would probably create my own CArray class, something along the lines of CArray< CBitmap, CBitmap& > if that is even right I don''t know, I''ve also read about ImageLists. They seem as though they might come in handy but again I have no idea of what to put in them. Of course any help would be appreciated.
AlienCharm
AlienCharm
well heres what i''ve came up with. i made a 2d array:


bool m_blArray[5] [10];


then I set it all to true:


for( int i = 0; i < 5; i++ )
for( int j = 0; j < 10; j++ )
m_blArray[ i ] [ j ] = true;


then when i called my *draw invader* functions i just tested if the element was true or false. if its true i draw it if its false i dont.


for( int i = 0; i < 1; i++ )
for( int j = 0; j < 10; j++ )
if( m_blArray[ i ] [ j ] == true )
{
dc.BitBlt(( 5 + x ) + m_nMoveInvaderX,60 + m_nMoveInvaderY,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
x += 50;
}


i''m not sure if this is the fastest/easiest/most efficient way. but its the only thing i could come up with so i went with it.

now on to figure out the whole collision detection thing and to see if i can get it to work with this array.....
SelethD
SelethD
You have the right idea, about making a class, but it needs to be a generic type class that will work for each invader, with variables describing which one to draw.


class cInvader
{
public:
int row;
int x;
int y;
int active;
int type;
int image;
};

cInvader Invaders[50];

then use for loops to set up inital information in class

for (r=0;r<5;r++)
for (c=0;c<10;c++)
Invaders[r*10+c].type = r;
Invaders[r*10+c].active = 1; (or TRUE)
and so on...

then when drawing, use loop for all, such as
for (i=0;i<50;i++)
if (Invaders.active==TRUE)
//load bitmap according to Invaders[i].image
//draw certain frame from bitmap accoring to
Invaders[i].type
//position image at Invaders[i].x, .y

once you have this setup working collision detection will be easier
KurtCPP
KurtCPP
Hi. BTW, when setting the whole array with two imbricated for loops, ain''t you ever been told memset(array,1,50); was still keeping working fine, too? ;-)

Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Some nice poetry to sweeten your spirit and relax a bit before programming
Prog, Games & Rock'n'Roll :I don't like the Prog but the Prog likes me.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.