windows编程:画线,简单的碰撞检测,简单的帧率锁定

2023-05-16,,

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define WINDOW_CLASS_NAME "WINCLASS1" #define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480 #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0:1) //全局变量
HWND main_window_handle = NULL;
HINSTANCE hInstance_App = NULL; char buffer[]; //窗口处理函数
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lPram)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
char buffer[];
switch (msg)
{
case WM_CREATE:
{
return ;
}break;
case WM_PAINT:
{
hdc = GetDC(hwnd);
ReleaseDC(hwnd, hdc);
GetClientRect(hwnd, &rect);
ValidateRect(hwnd, &rect); return ;
}break;
case WM_CLOSE:
{
if (IDYES != MessageBox(hwnd, "确实要退出应用程序?", "退出", MB_YESNO | MB_ICONEXCLAMATION))
{
return ;
}
else
{
PostQuitMessage();
}
}break;
case WM_SIZE:
{ }break;
case WM_DESTROY:
{
PostQuitMessage();
return ;
}break;
default:break;
}
return DefWindowProc(hwnd, msg, wParam, lPram);
}
void GameMain()
{
return;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//定义窗口类
WNDCLASSEX winClass;
HWND hWnd;
MSG msg; HPEN pen = NULL;
int color_change_count = ; //填充窗口类的各成员
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = WindowProc; //窗口消息处理函数
winClass.cbClsExtra = ;
winClass.cbWndExtra = ;
winClass.hInstance = hInstance;
winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.lpszClassName = WINDOW_CLASS_NAME; //窗口类名
winClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //保存实例句柄到全局变量
hInstance_App = hInstance; //注册窗口类
if (!RegisterClassEx(&winClass))
{
return ;
} //创建窗口类的一个成员
if (!(hWnd = CreateWindowEx(NULL,
WINDOW_CLASS_NAME,
"时间锁定的屏幕保护程序",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
, ,
WINDOW_WIDTH, WINDOW_HEIGHT,
NULL,
NULL,
hInstance,
NULL)))
{
return ;
} //保存窗体句柄到全局变量中
main_window_handle = hWnd; //得到设备上下文
HDC hdc = GetDC(hWnd); //设置随机数生成器的种子
srand(GetTickCount()); //线段终点坐标
int x1 = rand() % WINDOW_WIDTH;
int y1 = rand() % WINDOW_HEIGHT;
int x2 = rand() % WINDOW_WIDTH;
int y2 = rand() % WINDOW_HEIGHT; //线段终点的速度
int x1v = - + rand() % ;
int y1v = - + rand() % ;
int x2v = - + rand() % ;
int y2v = - + rand() % ; //消息循环
while (TRUE)
{
DWORD start_time = GetTickCount(); if (PeekMessage(&msg, hWnd, , , PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//100次变换一次颜色
if (++color_change_count >= )
{
color_change_count = ;
if (pen)
{
DeleteObject(pen);
}
pen = CreatePen(PS_SOLID, , RGB(rand() % , rand() % , rand() % ));
SelectObject(hdc, pen);
}
//移动线段终点
x1 += x1v;
y1 += y1v; x2 += x2v;
y2 += y2v; //碰撞检测,看是否碰到窗体的边界
if (x1< || x1>WINDOW_WIDTH)
{
x1v = -x1v;
x1 += x1v;
}
if (y1< || y1>WINDOW_HEIGHT)
{
y1v = -y1v;
y1 += y1v;
} if (x2< || x2>WINDOW_WIDTH)
{
x2v = -x2v;
x2 += x2v;
}
if (y2< || y2>WINDOW_HEIGHT)
{
y2v = -y2v;
y2 += y2v;
} MoveToEx(hdc, x1, y1,NULL);
LineTo(hdc, x2, y2); //锁定帧率为30fps,1/30秒,约等于33毫秒。
while (GetTickCount() - start_time < );
//如果用户按了ESC,发送WM_CLOSE消息。退出程序。
if (KEYDOWN(VK_ESCAPE))
SendMessage(hWnd, WM_CLOSE, , );
}
ReleaseDC(hWnd, hdc);
return msg.wParam;
}

windows编程:画线,简单的碰撞检测,简单的帧率锁定的相关教程结束。

《windows编程:画线,简单的碰撞检测,简单的帧率锁定.doc》

下载本文的Word格式文档,以方便收藏与打印。