#### Archived

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

# Seperating block colors into different rows - Breakout clone

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

## Recommended Posts

Hey, I have the game working, collision detection etc.. One small problem I''m now having is getting the blocks of the same color to appear on different rows IE; RED RED RED RED RED RED RED RED BLUE BLUE BLUE BLUE BLUE BLUE BLUE BLUE GREEN GREEN GREEN GREEN GREEN GREEN GREEN GREEN GREEN ECT ECT ECT ECT ECT..... What I have done is probably go the long way around and basically duplicated the Draw_Blocks function, called it say Draw_Blocks1, Draw_Blocks2 etc.. I added an x2,y2,BLOCK_ORIGIN_X1 and a BLOCK_ORIGIN_Y1 vars to this function and declared another blocks2_bmp.
  void Draw_Blocks1(void) { int x1 = BLOCK_ORIGIN_X, y1 = BLOCK_ORIGIN_Y; for (int row=0; row
  // load the blocks for row 1 Load_Bitmap_File(&bitmap8bit, "MUSH.BMP"); Set_Palette(bitmap8bit.palette); Create_Bitmap(&blocks1_bmp,0,0,32,32); Load_Image_Bitmap&blocks1_bmp,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_CELL); Unload_Bitmap_File(&bitmap8bit); // unload and restore resources // end of load blocks // load the blocks for row 2 Load_Bitmap_File(&bitmap8bit, "MUSH.BMP"); Create_Bitmap(&blocks2_bmp,40,40,32,32); Load_Image_Bitmap&blocks2_bmp,&bitmap8bit,1,0,BITMAP_EXTRACT_MODE_CELL); Unload_Bitmap_File(&bitmap8bit); // unload and restore resources // end of load blocks 
This worked in getting the blocks on the screen in the different colors kind of - the second row was slightly out of place but easily fixed. The problem with this came with the collision function: -
  // ball to block collision function void Ball_Block(void) { int ball_cx = ball.x + (BALL_SIZE / 2), ball_cy = ball.y + (BALL_SIZE / 2); int x1 = BLOCK_ORIGIN_X; int y1 = BLOCK_ORIGIN_Y; for (int row=0; row < NUM_BLOCK_ROWS; row++) { int x1 = BLOCK_ORIGIN_X; for (int col=0; col < NUM_BLOCK_COLUMNS; col++) { if ((ball_cx > x1) && (ball_cx < x1 + BLOCK_WIDTH) && (ball_cy > y1) && (ball_cy < y1 + BLOCK_HEIGHT) && (blockgrid[row][col]==1)) { MessageBeep(MB_OK); blockgrid[row][col] = 0; ball_dy =- ball_dy; ball_dx += (-1+rand()%3); return; } x1+=BLOCK_X_GAP; } y1+=BLOCK_Y_GAP; } } 
Rather than duplicate this function I tried to stick an OR and then an ELSE on the end of it by removing the last bracket and then duplicating it but putting in the vars relevant to the other row - row2. What I got was when the ball hit the a block in the first row, that block, would be removed but also the block on row2. All the blocks on row2 were not being picked up in the collision detection. Am I going about this in the right way, do I need to duplicate so much code or is there any easier way around it?? Cheers, Paul

##### Share on other sites
Looks like you''re just using one single array - blockgrid - to store whether a block is there or not. Drawing a single block in 2 or 3 places will just mean that 2 or 3 blocks disappear when you hit the ''original''.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
You set up an array of surfaces 1 to 4, simply replace your 1''s with 1 to 4 and when you blit each block to the backbuffer use this value with the surface array. Also when you test for 1''s replace with a test for not zero.

eg.

this
  Draw_Bitmap(&blocks1_bmp, back_buffer, back_lpitch, 1); 

becomes

  Draw_Bitmap(&blocks[blockgrid[row][col]]_bmp, back_buffer, back_lpitch, 1); 

and this

  if blockgrid[row][col]==1 

becomes this

  if blockgrid[row][col]>0 

If you hav''nt already you may have to setup and load your block surface as an array of block surfaces. Red(1), Blue(2), Yellow(3), Green(4) etc.

,Jay

##### Share on other sites
Hey,

Thanks for all the help: -

I understand about re-arranging the 2d array I initialized with all 1''s. I have changed that no problem: -

  UCHAR blockgrid[NUM_BLOCK_ROWS][NUM_BLOCK_COLUMNS] = { {1,1,1,1,1,1,1,1}, {2,2,2,2,2,2,2,2}, {3,3,3,3,3,3,3,3}, {3,3,3,3,3,3,3,3}, {2,2,2,2,2,2,2,2}, {1,1,1,1,1,1,1,1}}; 

What I don''t get is loading the blocm surfaces as an array. I know I need to load the actual bitmap files for which I use: -

  // load the blocks for row 1Load_Bitmap_File(&bitmap8bit, "MUSH.BMP"); Set_Palette(bitmap8bit.palette); Create_Bitmap(&blocks1_bmp,0,0,32,32);Load_Image_Bitmap(&blocks1_bmp,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_CELL);Unload_Bitmap_File(&bitmap8bit); // unload and restore resources// end of load blocks 

So I know I''ll have to have a set of these instructions per block bitmap loaded.

It''s this part I don''t follow: -

**If you hav''nt already you may have to setup and load your block surface as an array of block surfaces. Red(1), Blue(2), Yellow(3), Green(4)**

Paul.

##### Share on other sites
Hey,

I don''t suppose anyone can ellaborate any further on this because I still don''t get it.

Thanks,

Paul.

##### Share on other sites

You shouldn''t have separate functions for drawing each set of coloured blocks. It should be one function which loops through one array of blocks and draws each one. The number in the array tells it what colour to draw it.

You don''t need to load a bitmap for every block. You may need to load a bitmap for each colour of block. When you draw them to the screen, all you are doing is copying the archetypical bitmap (the one that you loaded in) to all the places on the screen where it appears. So if you have 10 red blocks, you draw them by copying a single red block to the screen 10 times in 10 different places. These places are determined by the block array. A certain number in that array will correspond to red blocks.)

I don''t recognise your bitmap manipulation functions. I believe you aren''t really using them effectively, but it may be a limitation of your system.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
Umm, I thought the code was clear enough, sorry about that - it''s more or less the same style as in TOTWGPG.

What I''m getting at is, I''m loading in a red,block and a blue block using the following: -

  // load the blocks for row 1Load_Bitmap_File(&bitmap8bit, "MUSH.BMP"); Set_Palette(bitmap8bit.palette); Create_Bitmap(&blocks1_bmp,0,0,32,32);Load_Image_Bitmap(&blocks1_bmp,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_CELL);Unload_Bitmap_File(&bitmap8bit); // unload and restore resources// end of load blocks// load the blocks for row 2Load_Bitmap_File(&bitmap8bit, "MUSH.BMP"); Create_Bitmap(&blocks2_bmp,40,40,32,32);Load_Image_Bitmap(&blocks2_bmp,&bitmap8bit,1,0,BITMAP_EXTRACT_MODE_CELL);Unload_Bitmap_File(&bitmap8bit); // unload and restore resources// end of load blocks 

I am putting these in an array with some other blocks using this: -

  UCHAR blockgrid[NUM_BLOCK_ROWS][NUM_BLOCK_COLUMNS] = { {1,1,1,1,1,1,1,1}, {2,2,2,2,2,2,2,2}, {3,3,3,3,3,3,3,3}, {3,3,3,3,3,3,3,3}, {2,2,2,2,2,2,2,2}, {1,1,1,1,1,1,1,1}}; 

The blocks are drawn to thw screen using: -

  void Draw_Blocks1(void){int x1 = BLOCK_ORIGIN_X, y1 = BLOCK_ORIGIN_Y; for (int row=0; row

I then use the following to check collision: -

  void Ball_Block(void){int ball_cx = ball.x + (BALL_SIZE / 2),ball_cy = ball.y + (BALL_SIZE / 2);int x1 = BLOCK_ORIGIN_X;int y1 = BLOCK_ORIGIN_Y;for (int row=0; row < NUM_BLOCK_ROWS; row++){int x1 = BLOCK_ORIGIN_X;for (int col=0; col < NUM_BLOCK_COLUMNS; col++){if ((ball_cx > x1) && (ball_cx < x1 + BLOCK_WIDTH) &&(ball_cy > y1) && (ball_cy < y1 + BLOCK_HEIGHT)&& (blockgrid[row][col]==1)) {MessageBeep(MB_OK);blockgrid[row][col] = 0;ball_dy =- ball_dy;ball_dx += (-1+rand()%3);return;}x1+=BLOCK_X_GAP;}y1+=BLOCK_Y_GAP;}}

** At the moment this draws **

BLUE BLOCK BLUE BLOCK BLUE BLOCK BLUE BLOCK BLUE BLOCK etc..
--------------------- BLANK --------------------------
--------------------- BLANK --------------------------
--------------------- BLANK --------------------------
BLUE BLOCK BLUE BLOCK BLUE BLOCK BLUE BLOCK BLUE BLOCK etc..

Or something similiar!!

The Blue blocks are set to equal 1 in the array so they are drawn as per the array position. The blank lines are were I have put different numbers 2,3,4 etc.. in the array which are supposed to represent different colored blocks (yellow,green) etc..

What I''m having trouble with is setting the yellow, green blocks to their relevant numbers as set in the array, IE Yellow might equal 2 for example.

I could do with it breaking down somewhat if possible.

What I thought about doing was loading the different colored block bitmaps in using the code above, and then I''m stumped LOL

Paul

##### Share on other sites
I assume that "mush.bmp" hold the bitmaps for all of the different colours. yeah? good.

Right, do me a favour and stop posting so much code, it really does turn people off of reading through it all and you''ll get less help. I''m only reading the last post you made and it''s got the collision detection stuff in it which you said worked. Stop it. Rant over with, i''ll try to be helpful now ;-) Ha ha, actually after saying that it''s just as well that you put the collision code up.

Use
if ((ball_cx > x1) && (ball_cx < x1 + BLOCK_WIDTH) &&(ball_cy > y1) && (ball_cy < y1 + BLOCK_HEIGHT)&& blockgrid[row][col])

if ((ball_cx > x1) && (ball_cx < x1 + BLOCK_WIDTH) &&(ball_cy > y1) && (ball_cy < y1 + BLOCK_HEIGHT)&& (blockgrid[row][col]==1))

your testing for the existance of a block, not that its assigned the value 1.

Okey dokey, looks like you want to be using only 1 drawing function that chnges the bitmap it uses depending upon the value in the array, right? So what you want is something like this that tests the value in the array and uses the relevant bitmap (the function might not be spot on cos i moved blocks1_bmp.x = x1-4; and blocks1_bmp.y = y1+4; out of the loop, i don''t see what they''re doing and you''ll probably know where you want them)

  void Draw_Blocks(void) { int x1 = BLOCK_ORIGIN_X, y1 = BLOCK_ORIGIN_Y; for(int row=0; row

That should get you different coloured blocks relating to the numbers in the array. If not post again and i''ll see what else i can do.

/* Ignorance is bliss, then you go and spoil it by learning stuff */

##### Share on other sites
Actualy it will draw the same colour block. I think you meant:

  case 2: { Draw_Bitmap(&blocks2_bmp,back_buffer,back_lpitch,1); }break; 

But what I meant by using an array of blocks was this:

  // load the blocks for row 1Load_Bitmap_File(&bitmap8bit, "MUSH.BMP"); Set_Palette(bitmap8bit.palette); Create_Bitmap(&blocks_bmp[1],0,0,32,32);Load_Image_Bitmap(&blocks_bmp[1],&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_CELL);Create_Bitmap(&blocks_bmp[2],0,0,32,32);Load_Image_Bitmap(&blocks_bmp[2],&bitmap8bit,1,0,BITMAP_EXTRACT_MODE_CELL);Create_Bitmap(&blocks_bmp[3],0,0,32,32);Load_Image_Bitmap(&blocks_bmp[3],&bitmap8bit,2,0,BITMAP_EXTRACT_MODE_CELL);Create_Bitmap(&blocks_bmp[4],0,0,32,32);Load_Image_Bitmap(&blocks_bmpCreate_Bitmap(&blocks[4]_bmp,0,0,32,32);,&bitmap8bit,3,0,BITMAP_EXTRACT_MODE_CELL);Unload_Bitmap_File(&bitmap8bit); // unload and restore resources// end of load blocks 

See, another array, you only need to change your &blocks1_bmp to &blocks_bmp[n] and initalize the blocks_bmp as an array of 4 surfaces.

Zipless''s method is probably easier but you need to alter your create_bitmap function to :

  Create_Bitmap(&blocks2_bmp,0,0,32,32); `

In this function you only change the name, the dimentions of the blocks are identical.

Note that I''m assuming how you call your Load_Image_Bitmap is correct, with a single value determining which image is returned.

You can work the rest out from this.

,Jay