当前位置:网站首页>els strip collision deformation judgment
els strip collision deformation judgment
2022-08-02 13:43:00 【joker_0030】
#include"els_h.h"
//背景数组
char g_arrBackGroud[20][10] = {0};
char g_arrSqare[2][4] = {0};
int g_nSqareID = -1;
// g_Line与g_nList记录当前模块左上角位置.
int g_nLine = -1;
int g_nList = -1;
void ChangeLineSqare()
{
if (1 == g_arrBackGroud[g_nLine][g_nList - 1])//横着的.
{
//清零.
g_arrBackGroud[g_nLine][g_nList - 1] = 0;
g_arrBackGroud[g_nLine][g_nList + 1] = 0;
g_arrBackGroud[g_nLine][g_nList + 2] = 0;
if (2 == g_arrBackGroud[g_nLine + 1][g_nList])
{
g_arrBackGroud[g_nLine - 1][g_nList] == 1;
g_arrBackGroud[g_nLine - 2][g_nList] == 1;
g_arrBackGroud[g_nLine - 3][g_nList] == 1;
}
else if (2 == g_arrBackGroud[g_nLine + 2][g_nList])
{
g_arrBackGroud[g_nLine + 1][g_nList] == 1;
g_arrBackGroud[g_nLine - 1][g_nList] == 1;
g_arrBackGroud[g_nLine - 2][g_nList] == 1;
}
else
{
//元素赋值.
g_arrBackGroud[g_nLine - 1][g_nList] = 1;
g_arrBackGroud[g_nLine + 1][g_nList] = 1;
g_arrBackGroud[g_nLine + 2][g_nList] = 1;
}
}
else//竖着的.
{
//清零.
g_arrBackGroud[g_nLine - 1][g_nList] = 0;
g_arrBackGroud[g_nLine + 1][g_nList] = 0;
g_arrBackGroud[g_nLine + 2][g_nList] = 0;
if (2 == g_arrBackGroud[g_nLine][g_nList + 1])
{
//赋值.
g_arrBackGroud[g_nLine][g_nList - 1] = 1;
g_arrBackGroud[g_nLine][g_nList - 2] = 1;
g_arrBackGroud[g_nLine][g_nList - 3] = 1;
//标记.
g_nList = g_nList - 2;
}
else if (2 == g_arrBackGroud[g_nLine][g_nList + 2])
{
g_arrBackGroud[g_nLine][g_nList + 1] = 1;
g_arrBackGroud[g_nLine][g_nList - 1] = 1;
g_arrBackGroud[g_nLine][g_nList - 2] = 1;
g_nList = g_nList - 1;
}
else if (2 == g_arrBackGroud[g_nLine][g_nList - 1])
{
//赋值.
g_arrBackGroud[g_nLine][g_nList + 1] = 1;
g_arrBackGroud[g_nLine][g_nList + 2] = 1;
g_arrBackGroud[g_nLine][g_nList + 3] = 1;
//标记.
g_nList = g_nList + 1;
}
else
{
//元素赋值.
g_arrBackGroud[g_nLine][g_nList - 1] = 1;
g_arrBackGroud[g_nLine][g_nList + 1] = 1;
g_arrBackGroud[g_nLine][g_nList + 2] = 1;
}
}
}
void OnPaint(HDC hDc)
{
//创建兼容性DC.
HDC hMemDC = CreateCompatibleDC(hDc);//内存id为:HMemDc,窗口id hDc.
//创建兼容性位图.
HBITMAP hBitmapBack= CreateCompatibleBitmap(hDc, 500, 600);
//关联起来.
SelectObject(hMemDC, hBitmapBack);
PaintSqare(hMemDC);
ShowSqare2(hMemDC);
//传递:
//返回值:失败返回0,成功返回非零.
//参数1:目标DC,窗口DC
//参数2,3:目标的起始位置,注意是基于我们的窗口.
//参数4,5:区域的大小.
//参数6:源DC,也就是我们的内存DC.
//参数7,8:内存图片的起始位置.
//参数9:传递方式.
//
BitBlt(hDc, 0, 0, 300, 600, hMemDC, 0, 0, SRCCOPY);
//释放DC
DeleteObject(hBitmapBack);
DeleteDC(hMemDC);
}
void Oncreate()
{
srand((unsigned int)time(NULL));//只执行一次.
CreateRandomSqare();
CopySqareToBack();
}
void PaintSqare(HDC hMemDC)
{
int i = 0,
j = 0;
//画大方块.
//创建的是窗口背景颜色.
HBRUSH hOldBrush1;
//创建一个颜色的画刷.
HBRUSH hNewBrush1 = CreateSolidBrush(RGB(63, 27, 18));
//绑定当前DC与画刷,返回系统默认画刷.
hOldBrush1 = SelectObject(hMemDC, hNewBrush1);
//释放画刷句柄.
//遍历
Rectangle(hMemDC, 0, 0, 300, 600);//矩形框距离运行窗口的开始位置坐标和截至位置坐标.
//指定一个方块.
/*g_arrBackGroud[2][4] = 1;
g_arrBackGroud[3][3] = 1;
g_arrBackGroud[3][4] = 1;
g_arrBackGroud[3][5] = 1;*/
//创建的是还未掉下的方块的颜色
HBRUSH hOldBrush;
//创建一个颜色的画刷.
HBRUSH hNewBrush = CreateSolidBrush(RGB(63, 27, 182));
//绑定当前DC与画刷,返回系统默认画刷.
hOldBrush = SelectObject(hMemDC, hNewBrush);
//释放画刷句柄.
//遍历
for (i=0 ; i < 20; i++)//2、?
{
for (j=0 ; j < 10; j++)
{
if (1 == g_arrBackGroud[i][j])
{
//画方块.
Rectangle(hMemDC, j*30, i*30, j*30 + 30, i*30 + 30);//矩形框距离运行窗口的开始位置坐标和截至位置坐标.
}
}
}
//使用完新画刷,把系统默认画刷选出来,返回创建的画刷.
hNewBrush = SelectObject(hMemDC, hOldBrush);
//释放画刷句柄.
DeleteObject(hNewBrush);
//使用完新画刷,把系统默认画刷选出来,返回创建的画刷.
hNewBrush1 = SelectObject(hMemDC, hOldBrush1);
//释放画刷句柄.
DeleteObject(hNewBrush1);
}
//创建随机块.
int CreateRandomSqare()
{
int nIndex=rand()%7;
switch (nIndex)
{
case 0:
g_arrSqare[0][0] = 1, g_arrSqare[0][1] = 1, g_arrSqare[0][2] = 0, g_arrSqare[0][3] = 0;
g_arrSqare[1][0] = 0, g_arrSqare[1][1] = 1, g_arrSqare[1][2] = 1, g_arrSqare[1][3] = 0;
g_nLine = 0;
g_nList = 3;
break;
case 1:
g_arrSqare[0][0] = 0, g_arrSqare[0][1] = 1, g_arrSqare[0][2] = 1, g_arrSqare[0][3] = 0;
g_arrSqare[1][0] = 1, g_arrSqare[1][1] = 1, g_arrSqare[1][2] = 0, g_arrSqare[1][3] = 0;
g_nLine = 0;
g_nList = 3;
break;
case 2:
g_arrSqare[0][0] = 0, g_arrSqare[0][1] = 1, g_arrSqare[0][2] = 0, g_arrSqare[0][3] = 0;
g_arrSqare[1][0] = 1, g_arrSqare[1][1] = 1, g_arrSqare[1][2] = 1, g_arrSqare[1][3] = 0;
g_nLine = 0;
g_nList = 3;
break;
case 3:
g_arrSqare[0][0] = 1, g_arrSqare[0][1] = 0, g_arrSqare[0][2] = 0, g_arrSqare[0][3] = 0;
g_arrSqare[1][0] = 1, g_arrSqare[1][1] = 1, g_arrSqare[1][2] = 1, g_arrSqare[1][3] = 0;
g_nLine = 0;
g_nList = 3;
break;
case 4:
g_arrSqare[0][0] = 0, g_arrSqare[0][1] = 0, g_arrSqare[0][2] = 1, g_arrSqare[0][3]= 0;
g_arrSqare[1][0] = 1, g_arrSqare[1][1] = 1, g_arrSqare[1][2] = 1, g_arrSqare[1][3] = 0;
g_nLine = 0;
g_nList = 3;
break;
case 5:
g_arrSqare[0][0] = 0, g_arrSqare[0][1] = 1, g_arrSqare[0][2] = 1, g_arrSqare[0][3] = 0;
g_arrSqare[1][0] = 0, g_arrSqare[1][1] = 1, g_arrSqare[1][2] = 1, g_arrSqare[1][3] = 0;
g_nLine = 0;
g_nList = 4;
break;
case 6:
g_arrSqare[0][0] = 1, g_arrSqare[0][1] = 1, g_arrSqare[0][2] = 1, g_arrSqare[0][3] = 1;
g_arrSqare[1][0] = 0, g_arrSqare[1][1] = 0, g_arrSqare[1][2] = 0, g_arrSqare[1][3] = 0;
g_nLine = 0;
g_nList = 4;
break;
}
g_nSqareID = nIndex;
return nIndex;
}
void CopySqareToBack()
{
int i = 0,
j = 0;
for ( i = 0; i <2 ; i++)
{
for ( j = 0; j < 4; j++)
{
g_arrBackGroud[i][j + 3] = g_arrSqare[i][j];
}
}
}
void OnReturn(HWND hWnd)
{
//启动定时器
//返回值:成功返回非零.
//参数1:窗口句柄hWnd,NUL
//参数2:定时器ID 不理会
//参数3:间隔时间,毫秒 1000ms=1s.
//参数4:设置为NULL 处理函数的地址.
SetTimer(hWnd, DEF_TIMER1, 200, NULL);
}
void SqareDwon()
{
int i = 0,
j = 0;
for (i = 19; i >=0; i--)
{
for (j = 0; j < 10; j++)
{
if (1==g_arrBackGroud[i][j])
{
g_arrBackGroud[i + 1][j] = g_arrBackGroud[i][j];
g_arrBackGroud[i][j]=0;
}
}
}
}
void OnTimer(HWND hWnd)
{
HDC hDc=GetDC(hWnd);//内核对象.
if (1 == CanSqareDown()&&1== CanSqareDown2())
{
SqareDwon();//停止下落.
g_nLine++;
}
else
{
//1到2
Change1To2();
//产生随机块.
CreateRandomSqare();
//复制到背景上.
CopySqareToBack();
}
//SqareDwon();
//显示方块:
//PaintSqare(hDc);
OnPaint(hDc);
ReleaseDC(hWnd, hDc);
}
int CanSqareDown()
{
int i = 0;
for ( i = 0; i < 10; i++)
{
if (1 == g_arrBackGroud[19][i])
{
return 0;
}
}
return 1;
}
void Change1To2()
{
int i = 0,
j = 0;
for ( i = 0; i < 20; i++)
{
for ( j = 0; j < 10; j++)
{
if (1==g_arrBackGroud[i][j])
{
g_arrBackGroud[i][j] = 2;
}
}
}
}
void ShowSqare2(HDC hMemDC)
{
int i = 0,
j = 0;
HBRUSH hOldBrush;
//创建一个颜色的画刷.
HBRUSH hNewBrush = CreateSolidBrush(RGB(233, 27, 182));
//绑定当前DC与画刷,返回系统默认画刷.
hOldBrush = SelectObject(hMemDC, hNewBrush);
for ( i = 0; i < 20; i++)
{
for ( j = 0; j < 10; j++)
{
if (2 == g_arrBackGroud[i][j])
{
Rectangle(hMemDC, j * 30, i * 30, j * 30 + 30, i * 30 + 30);//矩形框距离运行窗口的开始位置坐标和截至位置坐标.
}
}
}
//使用完新画刷,把系统默认画刷选出来,返回创建的画刷.
hNewBrush = SelectObject(hMemDC, hOldBrush);
//释放画刷句柄.
DeleteObject(hNewBrush);
}
int CanSqareDown2()
{
int i = 0,
j = 0;
for ( i = 19; i >= 0; i--)
{
for ( j = 0; j < 10; j++)
{
if (1== g_arrBackGroud[i][j])
{
if (2==g_arrBackGroud[i+1][j])
{
return 0;
}
}
}
}
return 1;
}
void OnLeft(HWND hWnd)
{
if (1== CanSqareLeft()&& 1==CanSqareLeft2())
{
HDC hDc = GetDC(hWnd);
//方块左移.
SqareLeft();
g_nList--;
//显示方块.
OnPaint(hDc);
ReleaseDC(hWnd, hDc);
}
}
void SqareLeft()
{
int i = 0,
j = 0;
for ( i = 0; i < 20; i++)
{
for ( j = 0; j < 10; j++)
{
if (1 == g_arrBackGroud[i][j])
{
g_arrBackGroud[i][j - 1] = g_arrBackGroud[i][j];
g_arrBackGroud[i][j] = 0;
}
}
}
}
int CanSqareLeft()
{
int i = 0;
for ( i = 0; i < 20; i++)
{
if (1==g_arrBackGroud[i][0])
{
return 0;
}
}
return 1;
}
int CanSqareLeft2()
{
int i,
j;
for ( i = 0; i < 20; i++)
{
for ( j = 0; j < 10; j++)
{
if (1 == g_arrBackGroud[i][j])
{
if (2==g_arrBackGroud[i][j-1])
{
return 0;
}
}
}
}
return 1;
}
void OnRight(HWND hWnd)
{
//HDC hDc = GetDC(hWnd);
方块右移.
//SqareRight();
//OnPaint(hDc);
释放.
//ReleaseDC(hWnd,hDc);
if (1 == CanSqareRight() && 1 == CanSqareRight2())
{
HDC hDc = GetDC(hWnd);
g_nList++;
//方块右移.
SqareRight();
//显示方块.
OnPaint(hDc);
ReleaseDC(hWnd, hDc);
}
}
void SqareRight()
{
int i,
j;
for ( i = 0; i < 20; i++)
{
for ( j = 9; j >=0 ; j--)
{
if (1 == g_arrBackGroud[i][j])
{
g_arrBackGroud[i][j+1] = g_arrBackGroud[i][j];
g_arrBackGroud[i][j] = 0;
}
}
}
}
int CanSqareRight()
{
int i = 0;
for (i = 0; i < 20; i++)
{
if (1 == g_arrBackGroud[i][9])
{
return 0;
}
}
return 1;
}
int CanSqareRight2()
{
int i,
j;
for (i = 0; i < 20; i++)
{
for (j = 0; j < 10; j++)
{
if (1 == g_arrBackGroud[i][j])
{
if (2 == g_arrBackGroud[i][j+1])
{
return 0;
}
}
}
}
return 1;
}
void OnDown(HWND hWnd)
{
OnTimer(hWnd);
}
void OnChangeSqare(HWND hWnd)
{
HDC hDc= GetDC(hWnd);
switch (g_nSqareID)
{
case 0:
case 1:
case 2:
case 3:
case 4:
if (1== CanSqareChangeShape())
{
ChangeSqare();//普通变形
}
else
{
return;
}
break;
case 5://方形
return;
case 6://长条变形.
ChangeLineSqare();
break;
}
OnPaint(hDc);
ReleaseDC(hWnd, hDc);
}
void ChangeSqare()
{
int i = 0,
j = 0,
nTemp=2;
char arrSqare[3][3] = { 0 };
//将背景块复制出来.
for ( i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
arrSqare[i][j] = g_arrBackGroud[g_nLine+i][g_nList+j];
}
}
//变形后复制回去.
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
g_arrBackGroud[g_nLine + i][g_nList + j]= arrSqare[nTemp][i];
nTemp--;
}
nTemp = 2;
}
}
int CanSqareChangeShape()
{
int i=0,
j=0;
for ( i = 0; i < 3; i++)
{
for ( j = 0; j < 3; j++)
{
if (2 == g_arrBackGroud[g_nLine + i][g_nList + j])
{
return 0;
}
}
}
//if (g_nList < 0 || g_nList + 2>9)//解决方案1.
//{
// return 0;
//}
if (g_nList < 0)
{
g_nList = 0;
}
if (g_nLine+2>9)
{
g_nList = 7;
}
return 1;
}
边栏推荐
猜你喜欢
RISC-V instruction format and 6 basic integer instructions
软件测试和硬件测试的区别及概念
使用Amazon SageMaker 构建基于自然语言处理的文本摘要应用
Article 48 - Analysis of timestamp2 parameters【2022-08-01】
【C语言】细品分支结构——if-else语句
【C语言】虐打循环练习题(2)
Singleton pattern of seven kinds of writing, you know?
自媒体创作怎样提高原创度,打造爆款作品?
k8s之KubeSphere部署有状态数据库中间件服务 mysql、redis、mongo
【622. 设计循环队列】
随机推荐
二进制中1的个数
Do you know Dijkstra of graph theory?
RESTful 风格(详细介绍 + 案例实现)
好用的php空间,推荐国内三个优质的免费PHP空间[通俗易懂]
玉溪卷烟厂通过正确选择时序数据库 轻松应对超万亿行数据
二叉树的类型、构建、遍历、操作
多个驻外使领馆发提醒 事关赴华出行、人身财产安全
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
图文短视频自媒体怎么创作?如何让点击量达到10W?
什么是 commonjs2
苹果,与Web3 “八字不合”
Selenium本地打开远程浏览器
矩阵中的路径
单例模式的七种写法,你都知道吗?
WPF效果第一百九十三篇之登录实现
WiFi Association & Omnipeek Packet Capture Analysis
如何通过DBeaver 连接 TDengine?
Taurus.MVC V3.0.3 microservice open source framework released: Make the evolution of .NET architecture easier in large concurrency.
[typescript] Use the RangePicker component in antd to implement time limit the previous year (365 days) of the current time
[C language] Analysis of function recursion (2)