利用C++语言编写一个动画程序
时间: 2024-03-08 13:48:20 浏览: 32
好的,下面是一个简单的C++动画程序示例,它使用了Windows API和GDI库来绘制动画:
```c++
#include <windows.h>
const int WIDTH = 800;
const int HEIGHT = 600;
// 定义球的结构体
struct Ball {
int x, y; // 球的位置
int r; // 球的半径
int vx, vy; // 球的速度
COLORREF color; // 球的颜色
};
// 初始化球的属性
void initBall(Ball& ball) {
ball.x = WIDTH / 2;
ball.y = HEIGHT / 2;
ball.r = 20;
ball.vx = 5;
ball.vy = 3;
ball.color = RGB(255, 0, 0); // 红色
}
// 绘制球
void drawBall(HDC hdc, const Ball& ball) {
HBRUSH hBrush = CreateSolidBrush(ball.color);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, ball.x - ball.r, ball.y - ball.r,
ball.x + ball.r, ball.y + ball.r);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
// 移动球的位置
void moveBall(Ball& ball) {
ball.x += ball.vx;
ball.y += ball.vy;
// 碰到边界反弹
if (ball.x - ball.r < 0 || ball.x + ball.r > WIDTH) {
ball.vx = -ball.vx;
}
if (ball.y - ball.r < 0 || ball.y + ball.r > HEIGHT) {
ball.vy = -ball.vy;
}
}
// 窗口过程函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static Ball ball;
switch (message)
{
case WM_CREATE:
// 初始化球
initBall(ball);
// 设置定时器
SetTimer(hWnd, 1, 20, NULL);
break;
case WM_TIMER:
// 移动球
moveBall(ball);
// 绘制球
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// 绘制背景
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
// 绘制球
drawBall(hdc, ball);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
// 销毁定时器
KillTimer(hWnd, 1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 注册窗口类
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "MyWindowClass";
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
RegisterClassEx(&wcex);
// 创建窗口
HWND hWnd = CreateWindow("MyWindowClass", "My Window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
WIDTH, HEIGHT, NULL, NULL, hInstance, NULL);
if (!hWnd) {
return FALSE;
}
// 显示窗口
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
```
这个程序创建了一个窗口,并在窗口内绘制了一个移动的球。程序使用了SetTimer函数来设置定时器,每隔20毫秒就会触发WM_TIMER消息,然后更新球的位置并重新绘制窗口。在绘制窗口时,程序使用了GDI库提供的函数来绘制球的形状和颜色。您可以根据自己的需要对程序进行修改和扩展。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)