当前位置:网站首页>els 长条碰撞变形判断
els 长条碰撞变形判断
2022-08-02 13:32: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;
}
边栏推荐
- Embedded system driver primary [2] - based on character device driver _ basic framework
- Flashback Technology of Oracle Database
- 【C语言】剖析函数递归(2)
- Detailed explanation of network flow (what information can the flow network diagram generally reflect)
- tinymce 如何实现动态国际化
- Closures in JS
- ThinkPHP 5.1反序列化分析和poc
- How to connect DBeaver TDengine?
- 80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
- 【C语言】函数哪些事儿,你真的get到了吗?(1)
猜你喜欢
随机推荐
基于华为eNSP的企业网络规划
RISC-V 指令格式和6种基本整数指令
鲁大师7月新机性能/流畅榜:性能跑分突破123万!
图论之Kruskal,最小生成树如何优雅解题?
SQL函数 UCASE
eclipse连接数据库后插入数据报错null
Embedded system driver primary [2] - based on character device driver _ basic framework
【C语言】函数哪些事儿,你真的get到了吗?(2)
Article 48 - Analysis of timestamp2 parameters【2022-08-01】
动态组件-component
CSDN(成长一夏竞赛)- 最大数
【C语言】夏日一题 —— 求最大公约数和最小公倍数
3 ways for OpenFeign to set headers
qt 编译报错 No rule to make target
How to do short video food from the media?5 steps to teach you to get started quickly
Word | 关于删除分节符(下一页)前面的版式就乱了解决方案
二叉树的类型、构建、遍历、操作
wait() ,notify(),notifyAll()以及wait()与sleep()比较
线程安全问题及关键字synchronized,volatile
Oracle数据库的闪回技术