共有回帖数 0 个
-
俺终于把c++的五子棋程序改写成windowsAPI的了,
模拟的字符模式的程序,没有使用任何绘图功能,
因此,界面全是字符的,
在vc6.0中我一共做了以下几步:
1、
新建一个新的工程,
选择win32 Application,
工程框中输入:5zq
点完成。
2、
在工程菜单-----〉设置---------〉link
在object/lib 模块中加入:winmm.lib,以便支持PlaySound()函数。
3、
新建:C++ Source File,
加入工程:5zq中,
文件框中输入:5zq
点结束
4、
在5zq.cpp中输入下面的内容:
#includewindows.h
#define BUFFER(x,y) *(pBuffer + y * cxBuffer + x )
#define CHESSNULL 0 //没有棋子
#define CHESS1 'O'//一号玩家的棋子
#define CHESS2 'X'//二号玩家的棋子
#define CHESSBOARDCROSS '.'
#define CROSSRU CHESSBOARDCROSS /*右上角点*/
#define CROSSLU CHESSBOARDCROSS /*左上角点*/
#define CROSSLD CHESSBOARDCROSS /*左下角点*/
#define CROSSRD CHESSBOARDCROSS /*右下角点*/
#define CROSSL CHESSBOARDCROSS /*左边*/
#define CROSSR CHESSBOARDCROSS /*右边*/
#define CROSSU CHESSBOARDCROSS /*上边*/
#define CROSSD CHESSBOARDCROSS /*下边*/
#define CROSS CHESSBOARDCROSS /*十字交叉点*/
#define WINTRUE 1
#define WINFALSE 0
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[]=TEXT("5zq");
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 Windowsd NT!"),
szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("5zq Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
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)
{
static DWORD dwCharSet=DEFAULT_CHARSET;
static int cxChar,cyChar,cxClient,cyClient,cxBuffer,cyBuffer,xCaret,yCaret;
static TCHAR * pBuffer=NULL;
static int Order=CHESS1,bOutWhile=WINFALSE;
char *Msg[]=
{
"Player1 key: ",
" UP----w ",
" DOWN--s ",
" LEFT--a ",
" RIGHT-d ",
" DO----space",
" ",
"Player2 key: ",
" UP----up ",
" DOWN--down ",
" LEFT--left ",
" RIGHT-right",
" DO----ENTER",
" ",
"new game: ",
" ESC ",
NULL,
};
HDC hdc;
HBRUSH hBrush;
int x,y,i;
PAINTSTRUCT ps;
TEXTMETRIC tm;
int Direction,WinFlag=WINFALSE;
switch(message)
{
case WM_INPUTLANGCHANGE:
dwCharSet=wParam;
case WM_CREATE:
hdc=GetDC(hwnd);
SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,
dwCharSet,0,0,0,FIXED_PITCH,NULL));
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cyChar=tm.tmHeight;
DeleteObject(SelectObject(hdc,GetStockObject(SYSTEM_FONT)));
ReleaseDC(hwnd,hdc);
cxBuffer=max(1,19);
cyBuffer=max(1,19);
if(pBuffer!=NULL)
free(pBuffer);
pBuffer=(TCHAR *)malloc(cxBuffer *cyBuffer *sizeof(TCHAR));
for(y=0;ycyBuffer;y++)
for(x=0;xcxBuffer;x++)
BUFFER(x,y)=CHESSNULL;
xCaret=0;
yCaret=0;
case WM_SIZE:
if(message==WM_SIZE)
{
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
}
if(hwnd==GetFocus())
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_SETFOCUS:
CreateCaret(hwnd,NULL,cxChar,cyChar);
SetCaretPos(xCaret*cxChar,yCaret*cyChar);
ShowCaret(hwnd);
return 0;
case WM_KILLFOCUS:
HideCaret(hwnd);
DestroyCaret();
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_LEFT:
if(Order==CHESS2)
xCaret=max(xCaret-1,0);
break;
case VK_RIGHT:
if(Order==CHESS2)
xCaret=min(xCaret+1,cxBuffer-1);
break;
case VK_UP:
if(Order==CHESS2)
yCaret=max(yCaret-1,0);
break;
case VK_DOWN:
if(Order==CHESS2)
yCaret=min(yCaret+1,cyBuffer-1);
break;
case VK_DELETE://退棋
break;
}
SetCaretPos(xCaret *cxChar,yCaret*cyChar);
return 0;
case WM_CHAR:
for(i=0;i(int)LOWORD(lParam);i++)
{
switch(wParam)
{
case 'w'://UP
case 'W':
if(Order==CHESS1)
yCaret=max(yCaret-1,0);
break;
case 's'://DOWN
case 'S':
if(Order==CHESS1)
yCaret=min(yCaret+1,cyBuffer-1);
break;
case 'a'://LEFT
case 'A':
if(Order==CHESS1)
xCaret=max(xCaret-1,0);
break;
case 'd'://RIGHT
case 'D':
if(Order==CHESS1)
xCaret=min(xCaret+1,cxBuffer-1);
break;
case ' ':
case 'r'://落子
if(!((Order==CHESS2&&wParam=='r')||(Order==CHESS1&&wParam==' ')))
{
//DoError();
PlaySound(TEXT("DoError.wav"),NULL,SND_FILENAME|SND_ASYNC);
break;
}
if(BUFFER(xCaret,yCaret)==CHESSNULL)
{
BUFFER(xCaret,yCaret)=Order;
HideCaret(hwnd);
hdc=GetDC(hwnd);
if(BUFFER(xCaret,yCaret)==CHESS1)
hBrush=(HBRUSH)CreateSolidBrush(RGB(0,0,255));
else
hBrush=(HBRUSH)CreateSolidBrush(RGB(255,0,0));
SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,
dwCharSet,0,0,0,FIXED_PITCH,NULL));
TextOut(hdc,xCaret * cxChar,yCaret * cyChar,
&BUFFER(xCaret,yCaret),1);
SetTextAlign(hdc,TA_LEFT|TA_TOP);
DeleteObject(SelectObject(hdc,GetStockObject(SYSTEM_FONT)));
ReleaseDC(hwnd,hdc);
DeleteObject(hBrush);
ShowCaret(hwnd);
PlaySound(TEXT("DoOk.wav"),NULL,SND_FILENAME|SND_ASYNC);//DoOk();
for(Direction=0;Direction4&&!WinFlag;Direction++)
{
int i;
int pos_x,pos_y,dpos_x,dpos_y;
const int testnum=5;
int count;
switch(Direction)
{
case 0:/*在水平方向*/
pos_x=xCaret-(testnum-1);
pos_y=yCaret;
dpos_x=1;
dpos_y=0;
break;
case 1:/*在垂直方向*/
pos_x=xCaret;
pos_y=yCaret-(testnum-1);
dpos_x=0;
dpos_y=1;
break;
case 2:/*在左下至右上的斜方向*/
pos_x=xCaret-(testnum-1);
pos_y=yCaret+(testnum-1);
dpos_x=1;
dpos_y=-1;
break;
case 3:/*在左上至右下的斜方向*/
pos_x=xCaret-(testnum-1);
pos_y=yCaret-(testnum-1);
dpos_x=1;
dpos_y=1;
break;
}
count=0;
for(i=0;itestnum*2+1&&!WinFlag;i++)
{
if(pos_x=0&&pos_x=18&&pos_y=0&&pos_y=18)
{
if(BUFFER(pos_x,pos_y)==Order)
{
count++;
if(count=testnum)
WinFlag=WINTRUE;
}
else
{
count=0;
WinFlag = WINFALSE;
}
}
pos_x+=dpos_x;
pos_y+=dpos_y;
}
}
if(WinFlag)
{
PlaySound(TEXT("DoWin.wav"),NULL,SND_FILENAME|SND_ASYNC);//DoWin();
}
else
{
if(Order==CHESS1)
Order=CHESS2;
else
Order=CHESS1;
}
}
else
{
//DoError();
PlaySound(TEXT("DoError.wav"),NULL,SND_FILENAME|SND_ASYNC);
}
break;
case 'x1B':
InvalidateRect(hwnd,NULL,FALSE);
break;
default:
break;
}
}
SetCaretPos(xCaret * cxChar, yCaret * cyChar);
if(WinFlag)
{
MessageBox(NULL,TEXT("Winner"),TEXT("WinInfo"),0);
if(pBuffer!=NULL)
free(pBuffer);
pBuffer=(TCHAR *)malloc(cxBuffer *cyBuffer *sizeof(TCHAR));
for(y=0;ycyBuffer;y++)
for(x=0;xcxBuffer;x++)
BUFFER(x,y)=CHESSNULL;
xCaret=0;
yCaret=0;
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdc,CreateFont(0,0,0,0,0,0,0,0,
dwCharSet,0,0,0,FIXED_PITCH,NULL));
int i,j;
for(i=0;icxBuffer;i++)
for(j=0;jcyBuffer;j++)
{
char k;
int x,y;
x=i;
y=j;
/*交叉点上是一号玩家的棋子*/
if(BUFFER(x,y)==CHESS1)
{
hBrush=(HBRUSH)CreateSolidBrush(RGB(0,0,255));
k=CHESS1;
}
/*交叉点上是二号玩家的棋子*/
else if(BUFFER(x,y)==CHESS2)
{
hBrush=(HBRUSH)CreateSolidBrush(RGB(255,0,0));
k=CHESS2;
}
else
{
hBrush=(HBRUSH)CreateSolidBrush(RGB(0,255,0));
k=CROSS;
}
TextOut(hdc,x * cxChar,y * cyChar,&k,1);
DeleteObject(hBrush);
}
DeleteObject(SelectObject(hdc,GetStockObject(SYSTEM_FONT)));
/*显示操作键说明*/
{
i=0;
while(Msg!=NULL)
{
SetTextAlign(hdc,TA_RIGHT|TA_CENTER);
TextOut(hdc,25 *2 * cxChar,i* cyChar,Msg,13);
SetTextAlign(hdc,TA_LEFT|TA_CENTER);
i++;
}
}
EndPaint(hwnd,&ps);
SetCaretPos(xCaret * cxChar, yCaret * cyChar);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
哦,对了,忘了链上那个C++程序了,
http://post.baidu.com/f?kz=257292053
楼主 2016-01-28 13:11 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知