Jump to content
  • Advertisement
Sign in to follow this  
Portishead

Begginer Tetris game help

This topic is 3409 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!