📄 demo.cpp
字号:
// demo.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
HWND hWnd;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DEMO);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DEMO);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;//(LPCSTR)IDC_DEMO;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
//
//数据
#define ROW 18 //行数
#define COL 24 //列数
#define WIDTH 42 //宽度
#define HEIGHT 42 //高度
#define BLOCKCOUNT 2 //方块类型数量
//方块颜色 1 2 3 4 5
//0代表没有块
//1...5代表不同颜色的块
int data[ROW][COL];
//方块相连的状态
int status[ROW][COL];
//分数
int score=0;
//
int curr_count=0;
//
int index=0;
// i
int mouse_x=-100,mouse_y=-100;
HDC memhdc[6][13];
char filename[6][20]={"fireblock","frogblock","hornblock","lightningblock","quakeblock","windblock"};
//char * filename[6]={"fireblock","frogblock","hornblock","lightningblock","quakeblock","windblock"};
//函数
void LoadPic()
{
HDC hdc = GetDC(0);
for(int i=0;i<6;i++)
{
for(int j=0;j<13;j++)
{
char buf[100]={0};
wsprintf(buf,"bmp\\%s%d.bmp",filename[i],j);
//在内存中创建兼容设备描述表,hdc,要兼容的设备描述表
//返回,新创建的设备描述表
memhdc[i][j] = CreateCompatibleDC(hdc);
//装载图像,可以从文件,也可以从资源中
// 从文件装载, 从资源装载
//第一个参数,实例句柄, NULL 实例 hInst
//资源名 文件名 资源名
//资源类型 IMAGE_BITMAP IMAGE_BITMAP
//资源图像大小 0,0 宽高 0表示默认大小
//从哪里装载资源 LR_LOADFROMFILE 0
HBITMAP bmp = (HBITMAP)LoadImage(NULL,buf,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
//每个设备都有默认的位图,大小1*1
//选入位图资源
SelectObject(memhdc[i][j],bmp);
}
}
ReleaseDC(0,hdc);
}
void Draw(HDC hdc)
{
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
int f=0;
if (status[i][j]==1) f = index;
if (data[i][j]==0)
{
// 目标设备描述表,目标位置小x,y,宽度,高度,源设备描述表
BitBlt(hdc,j*WIDTH,i*HEIGHT,WIDTH,HEIGHT,memhdc[0][12],0,0,SRCCOPY);
}
else
{
BitBlt(hdc,j*WIDTH,i*HEIGHT,WIDTH,HEIGHT,memhdc[data[i][j]-1][f],0,0,SRCCOPY);
}
}
}
char buf[100];
wsprintf(buf,"选中:%d 分数:%d 总成绩:%d",curr_count,(curr_count>1?curr_count*curr_count:0),score);
TextOut(hdc,mouse_x+30,mouse_y+30,buf,strlen(buf));
}
//初始化游戏全部数据
void Init()
{
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
data[i][j] = rand()%BLOCKCOUNT+1;
status[i][j]=0;
}
}
score = 0;
}
void ClearStatus(int status[ROW][COL])
{
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
status[i][j]=0;
}
}
}
void myfind(int r,int c,int type,int status[ROW][COL])
{
status[r][c] = 1;
//left
if (c>0)
{
if ((status[r][c-1]==0)&&(type==data[r][c-1]))
{
myfind(r,c-1,type,status);
}
}
//top
if (r>0)
{
if ((status[r-1][c]==0)&&(type==data[r-1][c]))
{
myfind(r-1,c,type,status);
}
}
//right
if (c<COL-1)
{
if ((status[r][c+1]==0)&&(type==data[r][c+1]))
{
myfind(r,c+1,type,status);
}
}
//down
if (r<ROW-1)
{
if ((status[r+1][c]==0)&&(type==data[r+1][c]))
{
myfind(r+1,c,type,status);
}
}
}
int RCCount(int status[ROW][COL])
{
int count=0;
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
if (status[i][j]>0)
{
count++;
}
}
}
return count;
}
//判断连接性
//参数x,y鼠标位置
//返回值:和xy位置上的块,相同颜色的块的个数
int Link(int x,int y)
{
//计算行列
int r = y / HEIGHT;
int c = x / WIDTH;
if (r>=ROW) return 0;
if (c>=COL) return 0;
if (data[r][c]==0) return 0;
ClearStatus(status);
myfind(r,c,data[r][c],status);
return RCCount(status);
}
int LinkCount(int r,int c)
{
int s[ROW][COL]={0};
if (data[r][c]==0) return 0;
myfind(r,c,data[r][c],s);
return RCCount(s);
}
//删除相同颜色的,相连接的块
int Delete()
{
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
if (status[i][j]>0)
{
data[i][j]=0;
}
}
}
return 0;
}
//判断游戏结束
int GameOver()
{
int flag = 0;
//全部清除
int count = RCCount(data);
if (count==0) flag = 1;
int k=1;
if (flag==0)
{
//全部快没有同色连接
for(int i=0;(i<ROW)&&(k==1);i++)
{
for(int j=0;j<COL;j++)
{
if (data[i][j]!=0)
{
if (LinkCount(i,j)>=2)
{
k = 0;
break;
}
}
}
}
}
if ((k==1)||(flag==1))
{
KillTimer(hWnd,1);
if(MessageBox(hWnd,"游戏结束,是否重新开始游戏?","提示",MB_OKCANCEL)==IDOK)
{
Init();
SetTimer(hWnd,1,100,NULL);
}
else
{
DestroyWindow(hWnd);
}
}
return 0;
}
//当删除一部分块的时候,落下上边的块
int Down()
{
if (RCCount(data)==0)
{
return 0;
}
int count=0;
int i,j;
for(i=ROW-1;i>0;i--)
{
for(j=0;j<COL;j++)
{
if ((data[i][j]==0)&&(data[i-1][j]!=0))
{
count++;
data[i][j]=data[i-1][j];
data[i-1][j]=0;
}
}
}
for(i=0;i<COL;i++)
{
int f = 0;
for(j=0;j<ROW;j++)
{
if (data[j][i]!=0)
{
f = 1;
break;
}
}
//
if (f == 0)
{
for(int x = i+1 ;x<COL;x++)
{
for(int y = 0;y<ROW;y++)
{
data[y][x-1] = data[y][x];
if (x==COL-1)
{
data[y][x]=0;
}
}
}
}
}
return count;
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP,
0, 0,1024, 768, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
SetTimer(hWnd,1,100,NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
int x,y;
int count;
switch (message)
{
case WM_TIMER:
{
hdc = GetDC(hWnd);
int c = Down();
Draw(hdc);
if (c==0)
{
GameOver();
}
index++;
if (index==12)index=0;
ReleaseDC(hWnd,hdc);
}
break;
case WM_MOUSEMOVE:
x = LOWORD(lParam);
y = HIWORD(lParam);
mouse_x = x;
mouse_y = y;
curr_count = Link(x,y);
break;
case WM_LBUTTONDOWN:
x = LOWORD(lParam);
y = HIWORD(lParam);
count = Link(x,y);
if(count>=2)
{
score = score + count * count;
Delete();
Down();
}
break;
case WM_RBUTTONDOWN:
DestroyWindow(hWnd);
break;
case WM_CREATE:
Init();
LoadPic();
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
Draw(hdc);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -