当前位置:网站首页>els block to the right
els block to the right
2022-07-31 03:10:00 【joker_0030】
1、函数实现(els.c)
#include"els_h.h"
//背景数组
char g_arrBackGroud[20][10] = {0};
char g_arrSqare[2][4] = {0};
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;
//画大方块.
//What is created is the window background color.
HBRUSH hOldBrush1;
//Create a color brush.
HBRUSH hNewBrush1 = CreateSolidBrush(RGB(63, 27, 18));
//绑定当前DCwith brush,Returns the system default brush.
hOldBrush1 = SelectObject(hMemDC, hNewBrush1);
//Release the brush handle.
//遍历
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;*/
//Created is the color of blocks that have not yet fallen
HBRUSH hOldBrush;
//Create a color brush.
HBRUSH hNewBrush = CreateSolidBrush(RGB(63, 27, 182));
//绑定当前DCwith brush,Returns the system default brush.
hOldBrush = SelectObject(hMemDC, hNewBrush);
//Release the brush handle.
//遍历
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);//矩形框距离运行窗口的开始位置坐标和截至位置坐标.
}
}
}
//Finish using the new brush,Select the system default brush,Returns the created brush.
hNewBrush = SelectObject(hMemDC, hOldBrush);
//Release the brush handle.
DeleteObject(hNewBrush);
//Finish using the new brush,Select the system default brush,Returns the created brush.
hNewBrush1 = SelectObject(hMemDC, hOldBrush1);
//Release the brush handle.
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;
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;
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;
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_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;
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;
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;
break;
}
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();//停止下落.
}
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;
//Create a color brush.
HBRUSH hNewBrush = CreateSolidBrush(RGB(233, 27, 182));
//绑定当前DCwith brush,Returns the system default brush.
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);//矩形框距离运行窗口的开始位置坐标和截至位置坐标.
}
}
}
//Finish using the new brush,Select the system default brush,Returns the created brush.
hNewBrush = SelectObject(hMemDC, hOldBrush);
//Release the brush handle.
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();
//显示方块.
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);
}
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;
}
}
}
}
2、主函数(els.main.c)
#include<Windows.h>
#include"resource.h"
#include"els_h.h"
LRESULT CALLBACK PELouSi(HWND hWnd, UINT oMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR ipCmdLine,int nCmdShow)
{
//初始化窗口类.
WNDCLASSEX db;
HWND hWnd;
MSG msg;//结构体变量.
db.cbClsExtra = 0;
db.cbSize = sizeof(WNDCLASSEX);
db.cbWndExtra = 0;
db.hbrBackground = (HBRUSH)COLOR_BACKGROUND;//背景颜色.
//返回值:鼠标的句柄.
// 参数1:如果加载系统光标,NULL、如果加载自定义光标,填写实列句柄,hInstance;参数2:系统光标,直接填写定义好的宏、如果加载自定义光标,就用MAKEINTRESOURCE(240)加载相应的资源ID.
//db.hCursor = LoadCursor(NULL,IDC_HAND);//加载系统定义的光标.
db.hCursor = LoadCursor(hInstance,MAKEINTRESOURCE (IDC_NODROP));//自定义的光标.资源文件->添加->资源->Cursor->IDC_NODROP->新建.
//返回值:图标的句柄.
// 参数1:如果加载系统光标,NULL、如果加载自定义光标,填写实列句柄,hInstance;参数2:系统光标,直接填写定义好的宏、如果加载自定义光标,就用MAKEINTRESOURCE(240)加载相应的资源ID.
//db.hIcon = LoadIcon(NULL,IDI_ASTERISK);//加载系统定义的光标.
db.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));//状态栏图标.
//db.hIconSm = NULL;//左上角图标为此定义.如果为空则默认为状态栏图标.
db.hIconSm = LoadIcon(NULL,IDI_HAND);
db.hInstance = hInstance;
db.lpfnWndProc = PELouSi;//回调函数名.
db.lpszClassName = "els";
db.lpszMenuName = NULL;
db.style = CS_HREDRAW | CS_VREDRAW;
if(0==RegisterClassEx(&db))
{
int a = GetLastError();
return 0;
}
//创建窗口.hWnd窗口句柄 失败返回NULL.
//窗口风格:参数4.
hWnd=CreateWindowEx(WS_EX_TOPMOST,"els","els方块",WS_OVERLAPPEDWINDOW,100,100,500,650,NULL,NULL,hInstance,NULL);
//1、?
if (NULL == hWnd)//窗口句柄. 窗口的唯一标识.
{
return 0;
}
//显示窗口
ShowWindow(hWnd, nCmdShow);
//消息循环.队列.
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
//回调函数
//返回值:LRESULT,CALLBACK调用约定.
//参数1:窗口句柄;参数2:消息ID.
LRESULT CALLBACK PELouSi(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)// LRESULT:long类型;UINT:无符号整型,消息的ID.
{
PAINTSTRUCT pt;//定义结构体.
HDC hDc;
//关闭窗口消息
switch (nMsg)
{
case WM_CREATE://窗口信息处理程序接受的第一个消息,也是回调函数处理的第一个消息(为WM_CREATE).此消息只产生一次,一般是用于初始化一些数据.
//GetLastError();//优化.
Oncreate();//游戏数据初始化.
break;
case WM_TIMER:
OnTimer(hWnd);
break;
//是回调函数处理的第二个消息(为WM_PAINT).
//当窗口显示区域的一部分显示内容或者全部变为(无效),以致于必须(更新画面)时,将由这个消息通知程序.
//窗口结构的最后的那个成员CS_HREDRAW|CS_VREDRAW,目的就是窗口大小发生变化的时候,产生WM_PAINT消息.
//窗口重叠时,重叠部分渐渐出现时.
case WM_PAINT:
//GetLastError();//优化.
hDc=BeginPaint(hWnd,&pt);//getDC,窗口可操作区域的标识.
//在此之间画窗口的内容.
//画方块.
OnPaint(hDc);//增加代码可读性.
EndPaint(hWnd, &pt);//结束.
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_RETURN:
OnReturn(hWnd);//3、?
break;
case VK_LEFT:
//左移.
OnLeft(hWnd);
break;
case VK_RIGHT:
// 右移.
OnRight(hWnd);
break;
case VK_UP:
break;
case VK_DOWN:
break;
}
break;
case WM_DESTROY:
KillTimer(hWnd, DEF_TIMER1);
PostQuitMessage(0);//调用PostQuitMessage(0)这个函数发出WM_QUIT消息.//点叉系统依次产生WM_CLOSE,WM_DESTROY,WM_QUIT.
break;
}//手动处理点叉关闭消息.
return DefWindowProc(hWnd, nMsg, wParam, lParam);
//LRESULT a=DefWindowProc(hWnd, oMsg, wParam, lParam);//功能
//return a;
}
3、头函数(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 OnReturn(HWND hWnd);
//方块下落.
void SqareDwon();
//方块左移.
void SqareLeft();
//方块右移.
void SqareRight();
//定时器响应的函数.
void OnTimer(HWND hWnd);
//左键处理.
void OnLeft(HWND hWnd);
//右键处理.
void OnRight(HWND hWnd);
//方块停在最底层
int CanSqareDown();
//els Blocks stop on blocks.
int CanSqareDown2();
//Determines whether the left-shifted border is a border.
int CanSqareLeft();
//Determines whether the left-shifted boundary is a block.
int CanSqareLeft2();
//将1变成2.
void Change1To2();
//显示2
void ShowSqare2(HDC hMemDC);
#endif
边栏推荐
- Compile Hudi
- 【C语言】三子棋(经典解法+一览图)
- SQL injection Less46 (injection after order by + rand() Boolean blind injection)
- 10 Permission introduction
- Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
- els 方块向右移动边界判断、向下加速
- 选好冒烟测试用例,为进入QA的制品包把好第一道关
- The Map Entry understanding and application
- 数据库实现分布式锁
- Recursive query single table - single table tree structure - (self-use)
猜你喜欢
【动态规划】连续子数组的最大和
LeetCode简单题之找到和最大的长度为 K 的子序列
Chapter 9 SVM实践
Moxa NPort device flaw could expose critical infrastructure to devastating attack
7. List of private messages
共模电感的仿真应用来了,满满的干货送给大家
5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
Several common errors when using MP
CorelDRAW2022 streamlined Asia Pacific new features in detail
10. Redis implements likes (Set) and obtains the total number of likes
随机推荐
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
冒泡排序、选择排序、直接插入排序、二分法查找
Modbus on AT32 MCUs
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
Office automation case: how to automatically generate period data?
CorelDRAW2022 streamlined Asia Pacific new features in detail
IDEA 注释报红解决
MP使用时的几个常见报错
什么是系统?
【C语言】表达式求值的一般方法
【编译原理】递归下降语法分析设计原理与实现
Graphical lower_bound & upper_bound
多线程下类对象的服务承诺探讨
PMP微信群日常习题
7、私信列表
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
接口测试关键技术
Mysql 45讲学习笔记(二十五)MYSQL保证高可用
Observer pattern
SIP协议标准和实现机制