俄罗斯方块(六)画方块

画完了底板,再来画方块。根据我们最开始的类结构可以知道:不同俄罗斯方块形状的画的方法都是一样的,就是把body的4个小方块显示一下就可以了。所以同样的代码可以写在他们共同的基类中

 

在基类中增加一个print方法,参数就是那个画板CPaintDC & 或者CDC &.(CPaintDC继承自CDC)

void Element::print(CDC& dc)
{
	for(int i = 0; i < 4; i++)
		body[i].print(dc);
}
 

而小方块的现实如下:

void Box::print(CDC& dc)
{
	CRect rect(x*step,y*step,(x+1)*step,(y+1)*step);
	CBrush brush(RGB(255,255,0));
	dc.FillRect(rect, &brush );
}

这里的step用来说明每个小方块占用几个像素。如果step=10,将会画出一个10*10像素的正方形。这个值在整个游戏期间不会变,所以我们可以把它申明成Box类的static const成员

class Box 
{
private:
	int x;
	int y;
public:
	static const int step=10;
 

 

 

 最后,需要寻找时机调用显示函数,这个时机就是在底板Panel获取WM_PAINT消息时

void Panel::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CRect rect;
	this->GetClientRect(rect);
	CBrush brushbg( RGB(125,125,125) );
	dc.FillRect(rect,& brushbg);

	if (element!=NULL)
		element->print(dc);

你可能感兴趣的:(游戏)