c++游戏代码在线相关内容

C++做一个小游戏,有源代码的最好,谢谢

6508

用C++编写的小游戏源代码

9143

dev-c++小游戏代码,急急急急!!!!!

11800

C++ 游戏 (看你有多色) 代码

/* 看你有多色小游戏
 *
 * 曙光 2014年11月19日16:18
*/
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#ifndef _T
#ifdef UNICODE
#define _T(str)   L ## str
#else
#define _T(str)     str
#endif  // UNICODE
#endif  // _T
#if ! __STDC_WANT_SECURE_LIB__
#define strcpy_s(DstText, TextLen, SrcText)         strcpy(DstText, SrcText) 
#define strcat_s(DstText, TextLen, SrcText)         strcat(DstText, SrcText) 
#define _itoa_s(Val, Text, TextLen, Radix)          itoa(Val, Text, Radix)
#endif
// 全局变量
HWND MainWindow;              // 主窗口
int  TruthX, TruthY;          // 正确答案坐标
int  ArraySize, Level;        // 颜色块矩阵边长、级别
COLORREF ColorDef, ColorTruth;// 默认颜色与正确答案颜色
// 画矩形
BOOL PolyRect(HDC hdc, LONG left, LONG top, LONG width, LONG hight) {
POINT point[4];

point[0].x = left;
point[0].y = top;
point[1].x = left + width;
point[1].y = top;
point[2].x = left + width;
point[2].y = top + hight;
point[3].x = left;
point[3].y = top + hight;
return Polygon(hdc, point, 4);
}
// 重绘窗口事件
void PaintWindow(void) {
HBRUSH hBrush, hOldBrush;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
int x, y;
double w, h;
ArraySize = ArraySize < 2 ? 2 : ArraySize;
GetClientRect(MainWindow, &rect);
hdc = BeginPaint(MainWindow, &ps);
hBrush = CreateSolidBrush(ColorDef);
hOldBrush = (HBRUSH) SelectObject(hdc, hBrush);
w = (double) (rect.right - rect.left) / ArraySize;
h = (double) (rect.bottom - rect.top) / ArraySize;
for (x = 0; x < ArraySize; ++x) {
for (y = 0; y < ArraySize; ++y) {
PolyRect(hdc, (LONG) w * x, (LONG) h * y, (LONG) w - 1, (LONG) h - 1);
}
}
hBrush = CreateSolidBrush(ColorTruth);
DeleteObject(SelectObject(hdc, hBrush));
PolyRect(hdc, (LONG) w * TruthX, (LONG) h * TruthY, (LONG) w - 1, (LONG) h - 1);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
EndPaint(MainWindow, &ps);
}
// 新游戏
void NewGame(int lev) {
BYTE r, g, b;
char text[1024];
if (lev > 100) {
MessageBox(MainWindow, _T("您已经通关了,在下佩服!"), _T("通关提示"), 0);
}
lev = lev < 1 || lev > 100 ? 1 : lev;
ArraySize = lev / 10 + 2;
Level = lev;
TruthX = rand() % ArraySize;
TruthY = rand() % ArraySize;
r = rand() & 0x7f;
g = rand() & 0x7f;
b = rand() & 0x7f;
ColorDef = RGB(r, g, b);
ColorTruth = RGB(r + 51 - lev / 2, g + 51 - lev / 2, b + 51 - lev / 2);
InvalidateRect(MainWindow, NULL, TRUE);
strcpy_s(text, sizeof(text), "看你有多色 - 第");
_itoa_s(lev, text + strlen(text), 4, 10);
strcat_s(text, sizeof(text), "关");
SetWindowTextA(MainWindow, text);
}
// 点击事件响应
void WindowClick(int x, int y) {
char text[1024];
RECT rect;
double w, h;
GetClientRect(MainWindow, &rect);
w = (double) (rect.right - rect.left) / ArraySize;
h = (double) (rect.bottom - rect.top) / ArraySize;
x = x / (int) w;
y = y / (int) h;
if (x == TruthX && y == TruthY) {
NewGame(Level + 1);
} else {
strcpy_s(text, sizeof(text), "你点错了哦,正确答案:");
_itoa_s(TruthX + 1, text + strlen(text), 3, 10);
strcat_s(text, sizeof(text), ",");
_itoa_s(TruthY + 1, text + strlen(text), 3, 10);
MessageBoxA(MainWindow, text, "重新开始", 0);
NewGame(1);
}
}
// 窗口消息响应
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:      // 窗口销毁
PostQuitMessage(0);
break;
case WM_SIZE:         // 窗口大小被改变
InvalidateRect(hWnd, NULL, TRUE);
break;
case WM_PAINT:        // 重绘窗口
PaintWindow();
break;
case WM_LBUTTONDOWN:  // 鼠标左键按下
WindowClick(LOWORD(lParam), HIWORD(lParam));
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
// 入口函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow) {
WNDCLASSEX wce;
MSG msg;

// 注册窗口类
wce.cbSize = sizeof(wce);
wce.style = 0;
wce.lpfnWndProc = (WNDPROC) WndProc;  // 窗口消息处理函数
wce.cbClsExtra = wce.cbWndExtra = 0;
wce.hInstance = hInstance;
wce.hCursor = LoadCursor(NULL,IDC_ARROW);
wce.hbrBackground = (HBRUSH) COLOR_BTNSHADOW;
wce.hIcon = wce.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
wce.lpszClassName = _T("ShuGuang");
wce.lpszMenuName = NULL;
RegisterClassEx(&wce);

MainWindow = CreateWindowEx(0, _T("ShuGuang"), _T("看你有多色"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, NULL, hInstance, NULL);
ShowWindow(MainWindow, nCmdShow);
UpdateWindow(MainWindow);
srand(GetTickCount());
NewGame(1);
// 消息循环
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

水平有限,可以参考 呵呵

...

14481

求一份c语言“超级玛丽”代码

17184

用c++写一个类似于超级玛丽的小游戏大概需要那些知识?

19910