📄 square.cpp
字号:
#include "stdafx.h"
#include "俄罗斯方块.h"
#include "Square.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#ifndef HONG
#define HONG
#define W_NUMBER 12 //一横排方块的个数
#define H_NUMBER 20 //一竖排方块的个数
#define SIZE 20 //方块的大小
#define BkColor RGB(200,100,200)
#endif
/////////////////////////////////////////////////////////////////////////////
// Square
void Square::MakeEle()
{
switch(type)
{
case 0:
{
Element[1].left = x;
Element[1].right = x + SIZE;
Element[1].top = y;
Element[1].bottom = y + SIZE;
Element[2].left = x;
Element[2].right = x + SIZE;
Element[2].top = Element[1].top + SIZE;
Element[2].bottom = Element[1].bottom + SIZE;
Element[3].left = x;
Element[3].right = x + SIZE;
Element[3].top = Element[2].top + SIZE;
Element[3].bottom = Element[2].bottom + SIZE;
Element[4].left = x;
Element[4].right = x + SIZE;
Element[4].top = Element[3].top + SIZE;
Element[4].bottom = Element[3].bottom + SIZE;
break;
}
case 1:
{
Element[1].left = x;
Element[1].right = x + SIZE;
Element[1].top = y;
Element[1].bottom = y + SIZE;
Element[2].left = x;
Element[2].right = x + SIZE;
Element[2].top = Element[1].top + SIZE;
Element[2].bottom = Element[1].bottom + SIZE;
Element[3].left = x + SIZE;
Element[3].right = x + 2*SIZE;
Element[3].top = y;
Element[3].bottom = y + SIZE;
Element[4].left = x + 2*SIZE;
Element[4].right = x + 3*SIZE;
Element[4].top = y;
Element[4].bottom = y + SIZE;
break;
}
case 2:
{
Element[1].left = x;
Element[1].right = x + SIZE;
Element[1].top = y;
Element[1].bottom = y + SIZE;
Element[2].left = x + 2*SIZE;
Element[2].right = x + 3*SIZE;
Element[2].top = y + SIZE;
Element[2].bottom = y + 2*SIZE;
Element[3].left = x + SIZE;
Element[3].right = x + 2*SIZE;
Element[3].top = y;
Element[3].bottom = y + SIZE;
Element[4].left = x + 2*SIZE;
Element[4].right = x + 3*SIZE;
Element[4].top = y;
Element[4].bottom = y + SIZE;
break;
}
case 3:
{
Element[1].left = x;
Element[1].right = x + SIZE;
Element[1].top = y;
Element[1].bottom = y + SIZE;
Element[2].left = x + SIZE;
Element[2].right = x + 2*SIZE;
Element[2].top = y + SIZE;
Element[2].bottom = y + 2*SIZE ;
Element[3].left = x + SIZE;
Element[3].right = x + 2*SIZE;
Element[3].top = y;
Element[3].bottom = y + SIZE;
Element[4].left = x + 2*SIZE;
Element[4].right = x + 3*SIZE;
Element[4].top = y;
Element[4].bottom = y + SIZE;
break;
}
case 4:
{
Element[1].left = x;
Element[1].right = x + SIZE;
Element[1].top = y;
Element[1].bottom = y + SIZE;
Element[2].left = x;
Element[2].right = x + SIZE;
Element[2].top = y + SIZE;
Element[2].bottom = y + 2*SIZE;
Element[3].left = x - SIZE;
Element[3].right = x ;
Element[3].top = y + SIZE;
Element[3].bottom = y + 2*SIZE;
Element[4].left = x - SIZE;
Element[4].right = x;
Element[4].top = y + 2*SIZE;
Element[4].bottom = y + 3*SIZE;
break;
}
case 5:
{
Element[1].left = x;
Element[1].right = x + SIZE;
Element[1].top = y;
Element[1].bottom = y + SIZE;
Element[2].left = x;
Element[2].right = x + SIZE;
Element[2].top = y + SIZE;
Element[2].bottom = y + 2*SIZE;
Element[3].left = x + SIZE;
Element[3].right = x + 2*SIZE ;
Element[3].top = y + SIZE;
Element[3].bottom = y + 2*SIZE;
Element[4].left = x + SIZE;
Element[4].right = x + 2*SIZE;
Element[4].top = y + 2*SIZE;
Element[4].bottom = y + 3*SIZE;
break;
}
case 6:
{
Element[1].left = x;
Element[1].right = x + SIZE;
Element[1].top = y;
Element[1].bottom = y + SIZE;
Element[2].left = x;
Element[2].right = x + SIZE;
Element[2].top = Element[1].top + SIZE;
Element[2].bottom = Element[1].bottom + SIZE;
Element[3].left = x + SIZE;
Element[3].right = x + 2*SIZE ;
Element[3].top = y ;
Element[3].bottom = y + SIZE;
Element[4].left = x + SIZE;
Element[4].right = x + 2*SIZE;
Element[4].top = y + SIZE ;
Element[4].bottom = y + 2*SIZE;
break;
}
}
}
Square::Square()
{
x = 5 + W_NUMBER/2*SIZE;
y = 5;
TurnNumber = 0;
m_Score = 0;
Rand();
MakeEle();
Grade = 1;
CTime tm = CTime::GetCurrentTime();
srand(tm.GetSecond());
m_picture = rand()%11;
bitmap.LoadMappedBitmap(IDB_BITMAP8);
bitmap1[0].LoadBitmap(IDB_BITMAP1);
bitmap1[1].LoadBitmap(IDB_BITMAP2);
bitmap1[2].LoadBitmap(IDB_BITMAP3);
bitmap1[3].LoadBitmap(IDB_BITMAP4);
bitmap1[4].LoadBitmap(IDB_BITMAP5);
bitmap1[5].LoadBitmap(IDB_BITMAP6);
bitmap1[6].LoadBitmap(IDB_BITMAP7);
bitmap1[7].LoadBitmap(IDB_BITMAP10);
bitmap1[8].LoadBitmap(IDB_BITMAP9);
bitmap1[9].LoadBitmap(IDB_BITMAP10);
MyBrush.CreatePatternBrush(&bitmap1[m_picture]);
}
void Square::Rand()
{
CTime tm = CTime::GetCurrentTime();
srand(tm.GetSecond());
type = rand()%7;
}
//当judge == 1时不擦除上一时刻的图像
//judge == 2 时只擦除上一时刻的图像
void Square::Move(int a,int b,CDC *pDC,int judge)
{
if(judge == 0||judge == 2)
{//擦除上一时刻的图形
CBrush brush,*pOldBrush;
CPen pen,*pOldPen;
brush.CreateSolidBrush(BkColor);
pen.CreatePen(PS_SOLID,1,BkColor);
pOldPen = pDC->SelectObject(&pen);
pOldBrush = pDC->SelectObject(&brush);
for(int i = 1;i <= 4;i++)
{
if(Element[i].top >= RgRect.top )
{
pDC->Rectangle(Element[i]);
}
}
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
for( i = 1;i <= 4;i++)
{
MoveElement(i,a,b);
}
}
if(judge == 1||judge == 0)
{//画出此刻的图形
CBrush *pOldBrush1;//brush最好不用指针
CPen pen1,*pOldPen1;
pen1.CreatePen(PS_SOLID,1,RGB(300,100,200));
pOldPen1 = pDC->SelectObject(&pen1);
pOldBrush1 = pDC->SelectObject(&MyBrush);
for(int i = 1;i <= 4;i++)
{
if(Element[i].top >= RgRect.top )
{
pDC->Rectangle(Element[i]);
}
}
pDC->SelectObject(pOldBrush1);
pDC->SelectObject(pOldPen1);
DeleteObject(pen1);
}
}
void Square::Move(int a,int b)
{
for(int i = 1;i <= 4;i++)
{
MoveElement(i,a,b);
}
}
bool Square::CheckRange(int m_x,int m_y)
{
int wrong = 0;
for(int i = 1;i <= 4;i++)
{
MoveElement(i,m_x,m_y);
}
for(i = 1; i <= 4;i++)
{
if(Element[i].left >= RgRect.left&&Element[i].right <= RgRect.right
&&Element[i].bottom <= RgRect.bottom)
{
wrong += 0;
}
else
{
wrong += 1;
}
}
for( i = 1;i <= 4;i++)
{
MoveElement(i,-m_x,-m_y);
}
if(wrong == 0)
{
return true;
}
else
{
return false;
}
}
void Square::Make()
{
x = 5 + W_NUMBER*SIZE/2;
y = 5;
TurnNumber = 0;
Rand();
MakeEle();
}
bool Square::CheckMatrix(int m_x,int m_y)
{
for(int i = 1;i <= 4;i++)
{
MoveElement(i,m_x,m_y);
}
for(i = 1; i <= 4 ; i++)
{
if((Element[i].left - 5)/SIZE < 0||Element[i].right > RgRect.right)
{
for(i = 1;i <= 4;i++)
{
MoveElement(i,-m_x,-m_y);
}
return true;
}
if(Matrix[(Element[i].left - 5)/SIZE][(Element[i].top - 5)/SIZE] != 0)
{
for(int i = 1;i <= 4;i++)
{
MoveElement(i,-m_x,-m_y);
}
return false;
}
}
for(i = 1;i <= 4;i++)
{
MoveElement(i,-m_x,-m_y);
}
return true;
}
void Square::ChangeMatrix()
{
for(int i = 1;i <= 4;i++)
{
Matrix[(Element[i].left - 5)/SIZE][(Element[i].top - 5)/SIZE] = 1;
}
}
void Square::PaintMatrix(CDC *pDC,int Style)
{
CDC memDC;
memDC.CreateCompatibleDC(NULL);
CBitmap memBitmap;
memBitmap.CreateCompatibleBitmap(pDC,RgRect.Width(),RgRect.Height());
memDC.SelectObject(&memBitmap);
memDC.FillSolidRect(5,5,RgRect.Width(),RgRect.Height(),BkColor);
CRect rect;
CBrush brush1,*pOldBrush1;
CPen pen1,*pOldPen1;
if( Style == 0)
{
brush1.CreatePatternBrush(&bitmap1[m_picture]);
pen1.CreatePen(PS_SOLID,1,RGB(300,100,200));
}
else if(Style == 1)
{
brush1.CreateSolidBrush(BkColor);
pen1.CreatePen(PS_SOLID,1,BkColor);
}
pOldPen1 = memDC.SelectObject(&pen1);
pOldBrush1 = memDC.SelectObject(&brush1);
for(int i = 0; i <= W_NUMBER; i++)
{
for(int j = 0; j <= H_NUMBER; j++)
{
rect.left = i*SIZE + 5;
rect.top = j*SIZE + 5;
rect.right = rect.left + SIZE;
rect.bottom = rect.top + SIZE;
if(Matrix[i][j] == 1)
{
memDC.Rectangle(rect);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -