当前位置:网站首页>els long block deformation conditions, boundary collision judgment
els long block deformation conditions, boundary collision judgment
2022-08-02 13:42:00 【joker_0030】
1、头函数(els.h)
#ifndef N_d
#define N_d
#include<Windows.h>
#include<time.h>
#define DEF_TIMER1 1234
void OnPaint(HDC hDc);
//显示方块.
void PaintSqare(HDC hMemDC);
//产生随机块.
int CreateRandomSqare();
//随机块贴近背景.
void CopySqareToBack();
void Oncreate();
//方块加速.
void SqareDwon();
//方块左移.
void SqareLeft();
//方块左移.
void SqareRight();
//处理按键消息
//回车键.
void OnReturn(HWND hWnd);
//定时器响应的函数.
void OnTimer(HWND hWnd);
//左键处理.
void OnLeft(HWND hWnd);
//右键处理.
void OnRight(HWND hWnd);
//下键处理.
void OnDown(HWND hWnd);
//上键处理.
void OnChangeSqare(HWND hWnd);
//方块停在最底层
int CanSqareDown();
//els 方块停在方块上.
int CanSqareDown2();
//判断左移边界是否是边框.
int CanSqareLeft();
//判断左移的边界是否是方块.
int CanSqareLeft2();
//判断右移边界是否是边框.
int CanSqareRight();
//判断右移的边界是否是方块.
int CanSqareRight2();
//判断方块是否变形.
int CanSqareChangeShape();
//The long border changes.
int CanLineSqareChange();
//将1变成2.
void Change1To2();
//改变形状.
void ChangeSqare();
//bar change.
void ChangeLineSqare();
//显示2
void ShowSqare2(HDC hMemDC);
#endif
2、函数实现(els.c)
#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;
int CanLineSqareChange()//Rectangle boundary deformation judgment.
{
int i = 0,
j = 0;
for ( i = 1; i < 4; i++)//右
{
if (2 == g_arrBackGroud[g_nLine][g_nList + i] || g_nList + i > 9)
{
break;
}
}
for ( j = 1; j <4 ; j++)
{
if (2 == g_arrBackGroud[g_nLine][g_nList - j] || g_nList - j < 0)
{
break;
}
}
if ((i-1+j-1)<3)
{
return 0;
}
return 1;
}
void ChangeLineSqare()//The rectangle is deformed.
{
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]||9==g_nList)//The long right boundary line collides
{
//赋值.
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]||8==g_nList)//The long right boundary line collides one position to the left
{
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]||0==g_nList)//The left border of the strip collides.
{
//赋值.
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 (6)//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(6)//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://长条变形.
if (1 == CanLineSqareChange())
{
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;
}
边栏推荐
- Redis all
- Seata Distributed Transaction
- 图文短视频自媒体怎么创作?如何让点击量达到10W?
- 【C语言】剖析函数递归(2)
- Cannot determine loading status from target frame detached when selenium chrome driver is running
- Mysql视图
- Summer training camp-week2 graph theory
- 供应磷脂-聚乙二醇-羧基,DSPE-PEG-COOH,DSPE-PEG-Acid,MW:5000
- 大而全的pom文件示例
- WeChat applet getPhoneNumber interface code=40013
猜你喜欢
随机推荐
单例模式的七种写法,你都知道吗?
短视频美食自媒体怎么做?5步教你快速上手
面试SQL语句,学会这些就够了!!!
Image retrieval method based on deep learning!
wx-wow(微信小程序动效库)
Flashback Technology of Oracle Database
鲁大师7月新机性能/流畅榜:性能跑分突破123万!
this的绑定指向详细解答
GCC版本升级到指定版本
二分查找 && 树
GTK:Gdk-CRITICAL **: IA__gdk_draw_pixbuf: assertion ‘GDK_IS_DRAWABLE (drawable)‘ failed
多个驻外使领馆发提醒 事关赴华出行、人身财产安全
Fabric.js 动态设置字号大小
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
Get out of the machine learning world forever!
删除链表的节点
C# 编译错误:Compiler Error CS1044
How to create short images and short videos from the media?How to make the click volume reach 10W?
玉溪卷烟厂通过正确选择时序数据库 轻松应对超万亿行数据
我的创作纪念日