更新时间:2015-12-27 21:38:39浏览次数:1+次
实现矩形的移动的效果如下图所示:
for (int i=0;i<3;i++)
{
int left = (i+count)*20;
int top = (row*20);
Rectangle(hdc,left,top,left+20,top+20);
}
每一个矩形是20x20的大小,收尾相连。第一条贪吃蛇的x坐标在不断地增加,也就在每次刷新客户区的时候,可以向右推进一节。这样就移动了。在后面会对x坐标进行检测,只要头部的矩形触及客户区右边了,就换一个行。此时就是归零增加的贪吃蛇的位置的count变量。换一行就是通过递增row。当然,也在后面会检测贪吃蛇在垂直方向的位置,即用row*20来做检测。count++;
if ((count+3)*20>(rect.right-rect.left))
{
count=0;
row++;
if ((row+1)*20>(rect.bottom-rect.top))
{
row=0;
}
}
而促使不停的刷新就是InvalidateRect(hwnd,NULL,TRUE);让客户区主动失效而产生WM_PAINT消息。然后就可以不停的循环了。为了看到比较清晰的动态效果,使用了Sleep函数让线程稍微睡眠下,这样画面上的贪吃蛇才会走的比较慢,我们人眼才能看的清楚。#include "windows.h"
#include <tchar.h>
// - 项目是Unicode字符集
LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int count=0;//水平上控制移动
static int row=0;//垂直上控制移动
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
//画贪吃蛇三个相连的矩形
for (int i=0;i<3;i++)
{
int left = (i+count)*20;
int top = (row*20);
Rectangle(hdc,left,top,left+20,top+20);
}
InvalidateRect(hwnd,NULL,TRUE);//促使客户区循环重绘
Sleep(100);//线程休眠,看到移动的效果
count++;
//边界检测,到达边缘做出归零循环
if ((count+3)*20>(rect.right-rect.left))
{
count=0;
row++;
if ((row+1)*20>(rect.bottom-rect.top))
{
row=0;
}
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmd,int iShow)
{
TCHAR ClassName[] = _T("MyClass");
TCHAR title1[] = _T("C++技术网http://www.cjjjs.com");
WNDCLASS wndClass;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WinProc;
wndClass.lpszClassName = ClassName;
wndClass.lpszMenuName=NULL;
wndClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndClass))return 0;
HWND hwnd = CreateWindow(ClassName,title1,WS_OVERLAPPEDWINDOW,0,0,440,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
相关资讯