📄 main.cpp
字号:
#include "allHeader.h"
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam);
BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow);
void drawStartChooseInterface(HDC hDc, int choice);
void drawRestartChooseInterface(HDC hDc, int choice, int score);
void drawGameInterface(HDC hDc, cube Cube,bool status[HIGHT+2][WIDTH+2], int score);
void startGame(HWND hWnd, cube &Cube, bool status[HIGHT+2][WIDTH+2], int &score);
int choice;
int step;
int score;
bool status[HIGHT+2][WIDTH+2];
cube Cube;
int speed;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG Message;
if(!InitWindowsClass(hInstance))
return FALSE;
if(!InitWindows(hInstance, nCmdShow))
return FALSE;
while (GetMessage(&Message, 0, 0, 0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
long WINAPI WndProc(HWND hWnd, UINT iMessage, UINT wParam, LONG lParam)
{
HDC hDc;
PAINTSTRUCT ps;
switch(iMessage)
{
case WM_CREATE:
step = 0;
speed = SPEED;
choice = 0;
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_MENUITEM40010:
MessageBox(hWnd, "下、左、右控制方向,上改变形状,空格直接下落\n\n 制作人:郭鹏鸥、胡钟文、高小龙、李聪", "简介", MB_OK);
return 0;
case IDM_START:
startGame(hWnd, Cube, status, score);
step = 1;
InvalidateRect(hWnd, NULL, 1);
return 0;
case IDM_EXIT:
PostQuitMessage(0);
return 0;
case IDM_L1:
speed = SPEED;
SetTimer(hWnd, 1, speed, NULL);
return 0;
case IDM_L2:
speed = SPEED - 100;
SetTimer(hWnd, 1, speed, NULL);
return 0;
case IDM_L3:
speed = SPEED - 200;
SetTimer(hWnd, 1, speed, NULL);
return 0;
case IDM_L4:
speed = SPEED - 300;
SetTimer(hWnd, 1, speed, NULL);
return 0;
case IDM_L5:
speed = SPEED - 400;
SetTimer(hWnd, 1, speed, NULL);
return 0;
}
case WM_KEYDOWN:
if (step == 0){
switch (wParam)
{
case VK_UP:
case VK_DOWN:
choice++;
choice %= 2;
InvalidateRect(hWnd, NULL, 1);
return 0;
case VK_RETURN:
if (choice == 1){
PostQuitMessage(0);
}
else{
startGame(hWnd, Cube, status, score);
step = 1;
InvalidateRect(hWnd, NULL, 1);
}
return 0;
}
}
else if (step == 1){
switch (wParam)
{
case VK_LEFT:
Cube.moveToLeft(status);
InvalidateRect(hWnd, NULL, 1);
return 0;
case VK_RIGHT:
Cube.moveToRight(status);
InvalidateRect(hWnd, NULL, 1);
return 0;
case VK_SPACE:
KillTimer(hWnd, 1);
SetTimer(hWnd, 1, speed, NULL);
if (!Cube.drop(status, score))
{
KillTimer(hWnd, 1);
step = 2;
}
InvalidateRect(hWnd, NULL, 1);
return 0;
case VK_DOWN:
if (!Cube.down(status, score)){
KillTimer(hWnd, 1);
step = 2;
}
KillTimer(hWnd, 1);
SetTimer(hWnd, 1, speed, NULL);
InvalidateRect(hWnd, NULL, 1);
return 0;
case VK_UP:
Cube.change(status);
InvalidateRect(hWnd, NULL, 1);
return 0;
}
}
else if (step == 2){
switch (wParam)
{
case VK_UP:
case VK_DOWN:
choice++;
choice %= 2;
InvalidateRect(hWnd, NULL, 1);
return 0;
case VK_RETURN:
if (choice == 1){
PostQuitMessage(0);
}
else{
startGame(hWnd, Cube, status, score);
step = 1;
}
return 0;
}
}
case WM_PAINT:
hDc = BeginPaint(hWnd, &ps);
if (step == 0){
drawStartChooseInterface(hDc, choice);
}
else if (step == 1){
drawGameInterface(hDc, Cube, status, score);
}
else if (step == 2){
drawRestartChooseInterface(hDc, choice, score);
}
EndPaint(hWnd,&ps);
return 0;
case WM_TIMER:
if (!Cube.down(status, score)){
KillTimer(hWnd, 1);
step = 2;
}
InvalidateRect(hWnd, NULL, 1);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return (DefWindowProc(hWnd, iMessage, wParam, lParam));
}
}
BOOL InitWindows(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow("窗口类名",
"cube",
WS_OVERLAPPEDWINDOW,//窗口类型
300,//窗口X位置
100,//窗口Y位置
300,//窗口宽度
500,//窗口高度
NULL,
NULL,
hInstance,
NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hInstance)
{
WNDCLASS WndClass;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));//窗口背景刷
WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon = LoadIcon(NULL, "END");
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = "窗口类名";//窗口类名字
WndClass.lpszMenuName = (const char *)IDR_MENU1;
WndClass.style = CS_HREDRAW|CS_VREDRAW;
return RegisterClass(&WndClass);
}
void drawStartChooseInterface(HDC hDc, int choice)
{
HFONT hf;
hf = CreateFont(20, 0,
0, 0,
700,
0, 0, 0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH,
"自定义1"
);
if (choice == 0){
SetTextColor(hDc, RGB(30, 38, 202));
SetBkColor(hDc, RGB(191, 27, 205));
TextOut(hDc, 120, 220, "开始", 4);
SetTextColor(hDc, RGB(255, 255, 255));
SetBkColor(hDc, RGB(0, 0, 0));
TextOut(hDc, 120, 250, "退出", 4);
}
else if (choice == 1){
SetTextColor(hDc, RGB(30, 38, 202));
SetBkColor(hDc, RGB(191, 27, 205));
TextOut(hDc, 120, 250, "退出", 4);
SetTextColor(hDc, RGB(255, 255, 255));
SetBkColor(hDc, RGB(0, 0, 0));
TextOut(hDc, 120, 220, "开始", 4);
}
}
//------------------------------------------------------------------------------
void drawRestartChooseInterface(HDC hDc, int choice, int score)
{
HFONT hf;
char str[20];
hf = CreateFont(20, 0,
0, 0,
700,
0, 0, 0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH,
"自定义1"
);
sprintf(str,"得分: %d", score);
if (choice == 0){
SetTextColor(hDc, RGB(255, 255, 255));
SetBkColor(hDc, RGB(0, 0, 0));
TextOut(hDc, 120, 250, str, strlen(str));
SetTextColor(hDc, RGB(30, 38, 202));
SetBkColor(hDc, RGB(191, 27, 205));
TextOut(hDc, 120, 220, "重新开始", 8);
SetTextColor(hDc, RGB(255, 255, 255));
SetBkColor(hDc, RGB(0, 0, 0));
TextOut(hDc, 120, 250, "退出", 4);
}
else if (choice == 1){
SetTextColor(hDc, RGB(255, 255, 255));
SetBkColor(hDc, RGB(0, 0, 0));
TextOut(hDc, 120, 250, str, strlen(str));
SetTextColor(hDc, RGB(30, 38, 202));
SetBkColor(hDc, RGB(191, 27, 205));
TextOut(hDc, 120, 250, "退出", 4);
SetTextColor(hDc, RGB(255, 255, 255));
SetBkColor(hDc, RGB(0, 0, 0));
TextOut(hDc, 120, 220, "重新开始", 8);
}
}
//------------------------------------------------------------------------------
void drawGameInterface(HDC hDc,cube Cube,bool status[HIGHT+2][WIDTH+2], int score)
{
HPEN hp;
HBRUSH hBrush1, hBrush2;
count i, j;
point* pointPt;
HFONT hf;
char str[20];
hf = CreateFont(20, 0,
0, 0,
700,
0, 0, 0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH,
"自定义1"
);
sprintf(str,"得分: %d", score);
hp = (HPEN)GetStockObject(WHITE_PEN);
SelectObject(hDc, hp);
hBrush1 = (HBRUSH)GetStockObject(BLACK_BRUSH);
SelectObject(hDc, hBrush1);
Rectangle(hDc, 69, 99, 171, 401);
hp = (HPEN)GetStockObject(BLACK_PEN);
SelectObject(hDc, hp);
hBrush1 = CreateSolidBrush(RGB(67, 188, 106));
hBrush2 = CreateSolidBrush(RGB(277, 128, 28));
SelectObject(hDc, hBrush1);
for (i = 1; i <= HIGHT; i++){
for (j = 1; j <= WIDTH; j++){
if (status[i][j] == true)
{
Rectangle(hDc, (j - 1) * 10 + 70, (i - 1) * 10 + 100, j * 10 + 70, i * 10 + 100);
}
}
}
SelectObject(hDc, hBrush2);
pointPt = Cube.getHead();
for (; pointPt != NULL ;){
Rectangle(hDc, (pointPt->x-1)*10+70, (pointPt->y-1)*10+100 , pointPt->x*10+70, pointPt->y*10+100);
pointPt = pointPt->next;
}
pointPt = Cube.getNextHead();
for (; pointPt != NULL ;){
Rectangle(hDc, (pointPt->x-1)*10+200, (pointPt->y-1)*10+120 , pointPt->x*10+200, pointPt->y*10+120);
pointPt = pointPt->next;
}
SetTextColor(hDc, RGB(255, 255, 255));
SetBkColor(hDc, RGB(0, 0, 0));
TextOut(hDc, 200, 250, str, strlen(str));
DeleteObject(hBrush1);
DeleteObject(hBrush2);
DeleteObject(hp);
DeleteObject(hf);
}
void startGame(HWND hWnd, cube &Cube, bool status[HIGHT+2][WIDTH+2], int &score)
{
count i,j;
for (i = 0 ; i < HIGHT +2 ; ++i){
for (j = 0 ; j < WIDTH + 2 ; ++j){
if (i == 0 || i == (HIGHT + 1) || j == 0 || j == (WIDTH + 1) )
{
status[i][j] = true;
}
else status[i][j] = false;
}
}
SetTimer(hWnd, 1, speed, NULL);
Cube.initCube(status);
score = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -