Sign in to follow this  

Begginer Tetris game help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create a simple Tetris clone, but I'm having trouble with the logic.. So far I made a class to represent a Block in the game, but I dont know how to keep track of all the pieces that are already on the floor.. I thought of a two dimensional array, but I dont know if that would work.. Any suggestions? (If I wasn't clear, tell me and I'll try to explain better) Thanks for any helpers, Portishead.

Share this post


Link to post
Share on other sites
I just finished my Tetris clone and I just used a 2-D array to fill in the color that takes its place in the grid. That way when I draw my grid I know what color goes where. Once a block drops the color of the block gets transferred to the array.

Share this post


Link to post
Share on other sites
well, you could always create a class for holding all the different blocks. For example, you could design it like this:


class PlayingField
{
public:
PlayingField(unsigned int nWidth, unsigned int nHeight)
: m_nWidth(nWidth)
, m_nHeight(nHeight)
{
for (unsigned int y = 0; y < nHeight; y++) {
for (unsigned int x = 0; x < nWidth; x++) {
//set all colors to black
SetColor(x, y, 0, 0, 0)
}
}
}

void SetColor(unsigned int x, unsigned int y, uchar r, uchar g, uchar b)
{
m_vePixels[y * m_nWidth + x] = Color(r, g, b);
}

void Draw()
{
//fill an array of pixels on the screen, somehow
//I don't know if you're using DirectX, OpenGL,
//PixelToaster or anything else
}

void Clear()
{
//set all pixels to black again
}

protected:
struct Color
{
Color(uchar vr, uchar vg, uchar vb)
: r(vr)
, g(vg)
, b(vb)
{
}

union {
struct {
unsigned char r;
unsigned char g;
unsigned char b;
};
unsigned char c[3];
};

std::vector<Color> m_vePixels
unsigned int m_nWidth;
unsigned int m_nHeight;
};


Then you'll need to create a block class, to represent the... well... blocks


class Block
{
Block(unsigned int nWidth, unsigned int nHeight, unsigned int nX, unsigned int nY)
: m_nWidth(nWidth)
, m_nHeight(nHeight)
, m_nX(nX)
, m_nY(nY)
{
//set all pixels to black
}

bool SetPixel(unsigned int x, unsigned int y, uchar r, uchar g, uchar b)
bool Draw(PlayingField *pField)
//maybe you could add collision detection code? etc. etc. etc.
};


I hope you understand how my approach would be, I'm not saying it's perfect, but it does the job ;-)

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this