break out clone
I want to make a break out clone and I wonder if this is the right way to store my blocks e.g. an array of 750 structures(possible positions of my blocks)
struct blocks
{
int iBlockId ; // id of block
char State ; // dead / alive
int iPosX ; // X-pos.
int iPosY ; // Y-pos.
char HitsUntillDeath ; // Hits before block dies
} ;
blocks blocks[750] ;
I could also store how many blocks I have in my level in an int and then use that int instead of that 750 so I save time and memory, right?
I guess there are more ways, so any1 got any suggestions
thanx in advance...
Instead of storing the block position in the data structure for the block you could create a 2 dimensional array; the position of each block would be related to its coordinates in the array.
Here''s the data structure I would use.
Here''s the data structure I would use.
#define ROWS 10 //These #define values are arbitrary and up to you.#define COLUMNS 15struct block{int Visible; //If you''re using C++ replace this with bool.int BlockType; //You could have blocks of different colors, strengths, rebound properties, etc.int HitsUntilDestroyed;};block Blocks[COLUMNS][ROWS];
thanx, I might use your code
one question though : I used char because that one is only 1 byte big, and a boolean is 2 bytes, so I would save memory this way. Am I right?
any1 else have other suggestions?
Edited by - da Cobra on March 8, 2002 2:06:58 AM
one question though : I used char because that one is only 1 byte big, and a boolean is 2 bytes, so I would save memory this way. Am I right?
any1 else have other suggestions?
Edited by - da Cobra on March 8, 2002 2:06:58 AM
Yes, using char instead of bool would save memory. However char is typically used for holding characters and could be confusing to some people in code. Also the type char is not "guaranteed" to be 1 byte long. On some systems it is actually 2 bytes long and uses UNICODE instead of ASCII (it I''m not mistaken). Of course the same applies to int and all other basic C data types. If memory space is important you could:
A) Use type char.
B) Use type __int8 which is an 8-bit integer on all systems (I''m not sure this type is universally supported by all compilers; I do know that it works on MVC++6.0 so try it and if it works, great. It''s really nice to automatically know the size of your numeric variable!
However with a breakout game memory probably shouldn''t be a problem and I''d go for clarity of code in a case like this.
Hope this was helpful!
A) Use type char.
B) Use type __int8 which is an 8-bit integer on all systems (I''m not sure this type is universally supported by all compilers; I do know that it works on MVC++6.0 so try it and if it works, great. It''s really nice to automatically know the size of your numeric variable!
However with a breakout game memory probably shouldn''t be a problem and I''d go for clarity of code in a case like this.
Hope this was helpful!
i dunno, with my compiler (borland) sizeof(bool)==1...
EDIT: oh, also about that 750 blocks thing... i don't think you can declare an array with a non-const size like that (although i might be wrong)... but you could use a dynamic array, like this:
you just have to remember to delete[] the dynamically-allocated array...
Edited by - krez on March 8, 2002 4:10:54 PM
EDIT: oh, also about that 750 blocks thing... i don't think you can declare an array with a non-const size like that (although i might be wrong)... but you could use a dynamic array, like this:
int NumBlocks; // set this variable to the number of blocksblock* blockarray = new block(NumBlock);...delete[] blockarray;
you just have to remember to delete[] the dynamically-allocated array...
Edited by - krez on March 8, 2002 4:10:54 PM
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
quote: EDIT: oh, also about that 750 blocks thing... i don''t think you can declare an array with a non-const size like that (although i might be wrong)... but you could use a dynamic array, like this:
I guess you were referring to this code...?
#define ROWS 10 //These #define values are arbitrary and up to you.#define COLUMNS 15struct block{int Visible; //If you''re using C++ replace this with bool.int BlockType; //You could have blocks of different colors, strengths, rebound properties etc.int HitsUntilDestroyed;};block Blocks[COLUMNS][ROWS];
ROWS and COLUMNS are #define constants so it''s legal; however dynamic allocation is good too if you want to change the number of elements after compile time.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement