2024年4月3日发(作者:)

c语言c++动态爱心表白代码

以下是一个使用C++编写的动态爱心表白代码示例。这个代码使用

了Windows API中的MessageBox函数来显示爱心形状和表白文字。

```cpp

#include

#include

#include

// 定义爱心形状的坐标点

const int LOVE_COORDINATES[] = {

100, 100,

150, 100,

200, 150,

250, 150,

300, 100,

250, 50,

200, 100,

150, 150,

100, 100};

void DrawLove(HDC hdc, int x, int y) {

HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));

SelectObject(hdc, hPen);

for (int i = 0; i < 8; i++) {

int tx = x + LOVE_COORDINATES[i * 2];

int ty = y - LOVE_COORDINATES[i * 2 + 1];

MoveToEx(hdc, tx, ty, NULL);

int cx = tx + LOVE_COORDINATES[i * 2 + 1];

int cy = ty - LOVE_COORDINATES[i * 2];

LineTo(hdc, cx, cy);

}

DeleteObject(hPen);

}

void PrintMessage(HDC hdc, int x, int y, const TCHAR* message) {

HFONT hFont = CreateFont(16, 0, 0, 0, FW_NORMAL, 0, 0, 0,

ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,

DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("宋体"));

SelectObject(hdc, hFont);

SetBkColor(hdc, RGB(255, 255, 255));

SetTextColor(hdc, RGB(0, 0, 0));

TextOut(hdc, x, y, message, _tcslen(message));

DeleteObject(hFont);

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow) {

const TCHAR* message = TEXT("我爱你"));

HDC hdc = GetDC(NULL);

int screenWidth = GetSystemMetrics(SM_CXSCREEN);

int screenHeight = GetSystemMetrics(SM_CYSCREEN);

int loveX = (screenWidth - 300) / 2;

int loveY = (screenHeight - 150) / 2;

DrawLove(hdc, loveX, loveY);

PrintMessage(hdc, loveX + 100, loveY + 50, message);

MessageBox(NULL, message, TEXT("动态爱心表白"), MB_OK);

ReleaseDC(NULL, hdc);

return 0;

}

```

编译和运行此代码后,你将看到一个带有红色爱心和表白文字的窗口。

点击确定按钮后,窗口会消失。请注意,此代码仅在Windows操作

系统上运行。

如果你希望在其他平台上实现类似效果,可以考虑使用其他图形库,

如OpenGL或SDL。