您的位置:首页 > 编程语言 > C语言/C++

一步一步实现扫雷游戏(C语言实现)(三)

2011-03-31 11:29 746 查看
使用WIN32API连接窗口

此项目相关博文链接

一步一步实现扫雷游戏(C语言实现)(一)

一步一步实现扫雷游戏(C语言实现)(二)

一步一步实现扫雷游戏(C语言实现)(三)

一步一步实现扫雷游戏(C语言实现)(四)

  首先,我弄个主窗口出来,没有用到MFC,直接调用API函数实现,先看看代码吧:

///////////////////////////////////////
//
//主函数:扫雷
//
///////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "DrawMap.h"//见下文
#include "def.h"

int m =10, n =10;
int map[MAX_X][MAX_Y];
int Global_x[MAX_X], Global_y[MAX_Y];

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
WINGDIAPI BOOL WINAPI MoveToEx(HDC hdc, int x, int y, LPPOINT lppt);//移动当前画笔的位置
WINGDIAPI BOOL WINAPI LineTo(HDC hdc, int x, int y);//用来画直线的函数
WINGDIAPI HPEN WINAPI CreatePen(int iStyle, int cWidth, COLORREF color);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
init();//初始化数组(地雷分布
int x_position, y_position, x_size, y_size;
set_position_size(&x_position, &y_position, &x_size, &y_size);

static TCHAR szAppName[] = TEXT ("MainWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra =0 ;
wndclass.cbWndExtra =0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return0 ;
}

hwnd = CreateWindow (szAppName, // window class name
TEXT ("扫雷游戏——The ClearMines Game"), // window caption
WS_OVERLAPPED | \
WS_CAPTION | \
WS_SYSMENU | \
WS_MINIMIZEBOX, // window style
x_position, // initial x position
y_position, // initial y position
x_size, // initial x size
y_size, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
paint_map(hwnd); //自定义的画图函数
return0;

case WM_LBUTTONDOWN: //单击鼠标
int x=LOWORD(lParam);//x,y为鼠标当前的位置坐标
int y=HIWORD(lParam);
left_key(hwnd, x, y);//自定义的响应鼠标左键的函数
break;

case WM_DESTROY:
PostQuitMessage (0) ;
return0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

主窗口图:

View Code

//NumMines.cpp

#include "NumMines.h"

#include <windows.h>

#include <stdio.h>
#include <string.h>
#include "time.h"
#include "stdlib.h"

/*******************************************************************
初始化地雷分布位置和个数
函数功能:根据设置的地雷个数和分布地图(map,数组)给出分布好了地雷的数组
函数原型:void set_mines( int num_mines)
参数:(in)—— int num_mines
*********************************************************************/
void set_mines(int num_mines)
{
int num =0;
int i,j;

while (num <= num_mines)
{
srand(time(0));
//rand()%n 取(0,n-1)的随机数
i = rand() % m;
j = rand() % n;
//如果出现相同的情况呢?,没事,再循环几次,直到有了足够的地雷为止
if (i<0|| i>m || j<0|| j>n || map[i][j] == MINES)
{
continue;
}
map[i][j] = MINES;
num++;//判断地雷个数
}
}

/****************************************************************************
返回周围地雷个数的函数
函数原型: int round_num_mines(int i,int j);
参 数: int i, int j为当前的坐标
返回值类型: int 返回该坐标处周围的地雷数
返回值情况:(1)返回1-8代表周围有1-8个地雷;
(2)返回0代表周围没有地雷;
(3)返回*代表此坐标时地雷;
******************************************************************************/
int round_num_mines(int i,int j)
{
if (map[i][j] == MINES)
{
return MINES;
}

int dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
int k =0, num_mines =0;

for (k =0; k <8; k++)
{
if (map[i+dir[k][0]][j+dir[k][1]] =='*')
{
num_mines++;
}
}
return num_mines;
}

BOOL init(void)
{
memset(map, 0, sizeof(map));
int i, j;
int flag =0;

set_mines((m*n)/10);//55个地雷

for (i=0; i<m; i++)
{
for (j=0; j<=n; j++)
{
map[i][j] = round_num_mines(i, j);
flag =1;
}
}
if (1== flag)
{
return TRUE;
}
return FALSE;
}

//DEF.H

#ifndef _DEF_
#define _DEF_

#define MAX_X 100 //行坐标最大值
#define MAX_Y 100 //纵坐标最大值
#define MINES -1 //地雷
#define DEF_M //默认行坐标
#define DEF_N //默认列坐标

#endif /* _DEF_ */

希望朋友们知道的帮忙解答解答,谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: