#### Archived

This topic is now archived and is closed to further replies.

# Why don't my loops work!

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

## Recommended Posts

Ok I am trying to make a breakout clone but for some reason when I try to draw the blocks it will not go. here is the code: int Game_Main() { int l = 0; int h = 0; int bb = 21; int bt = 1; int bl = 1; int br = 47; blocks[l][h].bottom = bb; blocks[l][h].left = bl; blocks[l][h].right = br; blocks[l][h].top = bt; { for(l;l<14;l++) { for(h;h<5;h++) { ddbltfx.dwFillColor = rand()%256; lpddsprimary->Blt(&blocks[l][h],NULL,NULL,DDBLT_COLORFILL / DDBLT_WAIT,&ddbltfx); } blocks[l][h].left +=48; blocks[l][h].right +=48; } } return(1); } Can someone tell me why it only draws one block and then just sits there. Umm...

##### Share on other sites
With the for (h; h<5; h++) loop, you don''t change any of the values of the rectangle where the Blt takes place. Move this:

blocks[l][h].left +=48;
blocks[l][h].right +=48;

to the h loop.

Then use this:

blocks[l][h].bottom +=48;
blocks[l][h].top +=48;

in the l loop.

thanks man!

Umm...

##### Share on other sites
Its actually a bit worse than wht Densun said - when you initialize the multidimensional array bocks[][] you only enter data into the blocks[0][0] element. Once you enter the loops your indexes into that array (l and h) start at 0,0 (and hence you draw the first block fine) but on the second iteration of the inner loop you are then looking at invalid data in blocks[0][1] For the rest of the loops itterations you are accessing members of the array that have not been initialized and thus have garbage values, therefore the blt isn’t working. You need to rework how you are doing things - you need to either initialize all the array members before the loop with the correct data or totally rework the way you are doing things

B

##### Share on other sites
Because you didn't set value of h in your inner loop, it will not return to 0 each time the loop is complete, I suspect that was not the intention.

the loop should probably look something like this

for (l=0;l<5;++l)
{
for (h=0;h<5;++h)
{
//do stuff here
}
}

I strongly advise you buy a good book on C programming and learn the basics before you try 3D programming.

Ben

btw, I've also written a 3D breakout game. You can find it here if you're interested:

http://www.geocities.com/ben32768

Edited by - benjamin bunny on 5/7/00 1:08:18 PM

##### Share on other sites
Um, this may be off the subject, but you have a pair of brackets that are doing nothing in your code.

PCMCIA - People Can't Memorize Computer Industry Acronyms
ISDN - It Still Does Nothing
APPLE - Arrogance Produces Profit-Losing Entity
SCSI - System Can't See It
DOS - Defunct Operating System
BASIC - Bill's Attempt to Seize Industry Control
IBM - I Blame Microsoft
DEC - Do Expect Cuts
CD-ROM - Consumer Device, Rendered Obsolete in Months
OS/2 - Obsolete Soon, Too.
WWW - World Wide Wait
MACINTOSH - Most Applications Crash; If Not, The Operating System Hangs

##### Share on other sites
You can never have too many brackets! Seriously, though, its really a personal style issue. Some people like to use brackets with every loop/if body (needed or not) and some people only use them when they are needed. I think the first way may lead to less errors, but I''m usually lazy and use the second method

Check out the GPI project today!

##### Share on other sites
Zipster''s right. It may be personal style, but personally I''ve never encountered a personal style where you insert brackets for absolutely no reason.

http://www.geocities.com/ben32768

##### Share on other sites
A basic C/C++ tutorial can be found here:

http://lightning.prohosting.com/~aznandy/cmain.html

http://www.geocities.com/ben32768

##### Share on other sites
windows: will install needless data on whole system
microsoft: most intelligent customers realise our software only feels tested

MENTAL

• 10
• 34
• 29
• 9
• 15