#include <windows.h>
#include <iostream>
// 按键的ID 需要用来 分配数据id
#define stone 10001
#define sisscor 10002
#define paper 10003
#define BackgroundColour CreateSolidBrush(RGB(236, 233, 216))
// #define Background RGB(236, 233, 216)
typedef enum {
Estone = 1,
Esisscor = 2,
Epaper = 3,
} ssp; // 这个只是用来测试的 (enum 用来模拟宏定义)
// 程序必要的句柄(一般由 WinMain 分配 (这里用来全局定义 方便使用))
HINSTANCE appInstance;
short player, pc;// 玩家 和 人机 出的
int score=0;
void SSP(HWND hWnd) {
// 模拟人机出拳(rand()函数可以获取随机数(rand()% n + a) 表示 最大n+a 最小a)
pc = rand() % 4 + 1;
// 旧方法
switch (pc) {
case 1:
// 一个弹窗
::MessageBox(hWnd, TEXT("电脑出了石头"), TEXT("石头剪刀布"), MB_OK/* | MB_ICONWARNING*/); // 一个图标
break;
case 2:
::MessageBox(hWnd, TEXT("电脑出了剪刀"), TEXT("石头剪刀布"), MB_OK);
break;
case 3:
::MessageBox(hWnd, TEXT("电脑出了布"), TEXT("石头剪刀布"), MB_OK);
break;
default:
break;
}
// 新方法 简单 但 绝大多数系统不支持 (放入缓存区,只有接受到paint消息才会绘制)
// PAINTSTRUCT ps;
// HDC hdc = BeginPaint(hWnd, &ps); // 开始绘画
// PAINTSTRUCT ps;
// HDC hdc = BeginPaint(hWnd, &ps);
// switch (pc) {
// case 1:
// TextOut(hdc, 455, 245, TEXT("人机1:石头"), 52);
// break;
// case 2:
// TextOut(hdc, 455, 245, TEXT("人机1:剪刀"), strlen("人机1:石头"));
// break;
// case 3:
// TextOut(hdc, 455, 245, TEXT("人机1:布 "), strlen("人机1:石头"));
// break;
// default:
// //TODO
// break;
// }
// EndPaint(hWnd, &ps);
// 比较 用这样的方法可以快速比较
short mc = player - pc;
switch (mc) {
case 0:
::MessageBox(hWnd, TEXT("游戏平局"), TEXT("石头剪刀布"), MB_OK);
break;
case -1:
case 2:
score += 2;
::MessageBox(hWnd, TEXT("你获得了胜利"), TEXT("石头剪刀布"), MB_OK);
break;
case -2:
case 1:
score -= 1;
::MessageBox(hWnd, TEXT("你输了"), TEXT("石头剪刀布"), MB_OK);
default:
break;
}
std::cout << "score = "<<score<<"\n";
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
// 方便使用
HWND hBtnStone;
HWND hBtnSisscor;
HWND hBtnPaper;
switch (message) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps); // 开始绘画
int drawX = 390;
int drawY = 260;
TextOut(hdc, drawX, drawY, TEXT("作者: C丁洞杀O from fishC"), strlen("作者: C丁洞杀O from fishC"));
EndPaint(hWnd, &ps);
}
break;
case WM_CREATE: { //初始化
// 三个按键
hBtnStone = (HWND)CreateWindow(TEXT("Button"),
TEXT("石头"),
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
150/*x*/, 45/*y*/, 160/*长*/, 65/*高*/,
hWnd,
(HMENU)stone,
appInstance,
NULL
);
hBtnSisscor = (HWND)CreateWindow(TEXT("Button"),
TEXT("剪刀"),
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
350, 45, 160, 65,
hWnd,
(HMENU)sisscor,
appInstance,
NULL
);
hBtnPaper = (HWND)CreateWindow(TEXT("Button"),
TEXT("布"),
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
550, 45, 160, 65,
hWnd,
(HMENU)paper,
appInstance,
NULL
);
break;
}
case WM_COMMAND: {
// 接受到 按下按钮时触发 (判断是哪个)
if (LOWORD(wParam) == stone && HIWORD(wParam) == BN_CLICKED) {
player = Estone;
//MessageBox(hWnd, L"石头", L"石头剪刀布", MB_OK);
} else if (LOWORD(wParam) == sisscor && HIWORD(wParam) == BN_CLICKED) {
player = Esisscor;
//MessageBox(hWnd, L"剪刀", L"石头剪刀布", MB_OK);
} else if (LOWORD(wParam) == paper && HIWORD(wParam) == BN_CLICKED) {
player = Epaper;
//MessageBox(hWnd, L"布", L"石头剪刀布", MB_OK);
}
// 判断
SSP(hWnd);
break;
}
case WM_DESTROY:
PostQuitMessage(0); // 不进入消息队列, 直接执行
return 0;
case WM_CLOSE:
if (MessageBox(hWnd, TEXT("现在就离开吗?\n不再玩会儿?"), TEXT("游戏助手:"), MB_YESNO | MB_ICONWARNING) == IDYES) {
DestroyWindow(hWnd); // 销毁窗口的消息 (不是销毁窗口 )
}
break;
case WM_LBUTTONDOWN:
{
int xPos;
int yPos;
xPos = LOWORD(lParam);
yPos = HIWORD(lParam);
char buf[1024];
sprintf_s(buf, 1024, "Curor( x = %d; y = %d )", xPos, yPos);
std::cout<<buf<<std::endl;
}
break;
}
// 剩余的事件 交给 Windows 自己处理
return DefWindowProc(hWnd, message, wParam, lParam);
}
// WinMain入口函数 还可以写成这样
/*
int main() {
HINSTANCE* hInstance = new HINSTANCE;
HINSTANCE* hPrevInstance = new HINSTANCE;
LPSTR* lpCmdLine = new LPSTR;
INT* nCmdShow = new INT;
}
*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, INT nCmdShow) { // WINAPI 回调方法 __stdcall
score = 0;
appInstance = hInstance;
LPCSTR szAppName = TEXT("Window"); // 这里 类名
WNDCLASS wndClass;
MSG msg;
// Windows 类的风格写法
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = NULL;
wndClass.hCursor = NULL;
wndClass.hbrBackground = (BackgroundColour); // 颜色 (这个个人比较喜欢可以调整, 上面调)
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szAppName; // 名字
/**/
if (!RegisterClass(&wndClass)) {
MessageBox(NULL, TEXT("打开失败"),
TEXT("error"), MB_OK | MB_ICONERROR);
return 0;
}
HWND hWnd = CreateWindow(szAppName, // 类名 来找到已经定义好的WNCLASS 窗口风格
TEXT("石头剪刀布") /*标题*/, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
885, 300,
NULL, NULL, hInstance, NULL);
// 设置窗口不可改动大小
DWORD dwStyle = GetWindowLong(hWnd, GWL_STYLE);
dwStyle &= ~(WS_SIZEBOX);
SetWindowLong(hWnd, GWL_STYLE, dwStyle);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// 消息函数 (C++ 11 & +)可以这么写 (C++ 11 以下)需要使用阻塞的写法
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// 返回程序正常退出(-1 为 异常退出)
return msg.wParam;
}