您好,欢迎访问三七文档
界面设计:运行界面:代码如下:OptionExplicitConstSubWidth=400'定义画五子棋表格的每格长度和宽度PrivateP2PlayColorAsInteger'实现黑白棋子的交替进行PrivateMyColorAsInteger'标记黑白双方棋子颜色PrivateIfSucceedAsBoolean'表示是否胜利Constpi=3.14159'定义字符常量pi=3.14159PrivatecenterxAsSinglePrivatecenteryAsSinglePrivateradiusAsSinglePrivateDataArray(14,14)AsInteger'保存棋盘中棋子的位置信息(空子=3黑棋=1白棋=0)PrivatesumtimeAsInteger'记录总时间来判断谁超时PrivateifStarteasyAsBoolean'标记简单难度下计时功能是否可以开启(ifStarteasy=true时每落子一次计时开启一次)PrivateifStartnormalAsBoolean'标记中等难度下计时功能是否可以开启(ifStartnormal=true时每落子一次计时开启一次)PrivateifStarthardAsBoolean'标记困难难度下计时功能是否可以开启(ifStarthard=true时每落子一次计时开启一次)'单击命令按钮退出退出PrivateSubCmdExit_Click()EndEndSubPrivateSubCmdStart_Click()DimiAsIntegerDimmAsIntegerDimnAsInteger'绘制棋盘PicQiPan.ClsPicQiPan.ForeColor=vbBlackFori=1To14PicQiPan.Line(SubWidth,SubWidth*i)-(SubWidth*14,_SubWidth*i)PicQiPan.Line(SubWidth*i,SubWidth)-(SubWidth*i,_SubWidth*14)Nexti'棋盘落点信息初始化Form=0To14Forn=0To14DataArray(m,n)=3NextnNextm'主要标记信息初始化P2PlayColor=0MyColor=0IfSucceed=FalseifStarteasy=FalseifStartnormal=FalseifStarthard=FalseTimer2.Enabled=FalseTimer3.Enabled=FalseTimer4.Enabled=FalseFrmMain.Clssumtime=-1EndSub'简单难度PrivateSubfileeasy_Click()ifStarteasy=Truesumtime=-1MsgBox双方下每步棋的思考时间最多20秒,否则超时清盘EndSub'通过文件退出退出PrivateSubfileexit_Click()EndEndSub'困难难度PrivateSubfilehard_Click()ifStarthard=Truesumtime=-1MsgBox双方下每步棋的思考时间最多5秒,否则超时清盘EndSub'中等难度PrivateSubfilenormal_Click()ifStartnormal=Truesumtime=-1FrmMain.ClsMsgBox双方下每步棋的思考时间最多10秒,否则超时清盘EndSub'通过文件重新开始实现棋盘初始化PrivateSubfilerestart_Click()CallCmdStart_ClickEndSubPrivateSubForm_Load()DimiAsIntegerDimmAsIntegerDimnAsInteger'绘制棋盘PicQiPan.ClsPicQiPan.ForeColor=vbBlackFori=1To14PicQiPan.Line(SubWidth,SubWidth*i)-(SubWidth*14,_SubWidth*i)PicQiPan.Line(SubWidth*i,SubWidth)-(SubWidth*i,_SubWidth*14)Nexti'棋盘落点信息初始化Form=0To14Forn=0To14DataArray(m,n)=3NextnNextmPrint'确定表针位置的基本参量centerx=Pictime.Width/2centery=Pictime.Height/2radius=Pictime.Height/2*0.9Pictime.PSet(centerx,centery)Pictime.Circle(centerx,centery),radiusEndSub'棋子落点判断(出界和重子情况)PrivateSubPicQipan_MouseDown(ButtonAsInteger,ShiftAsInteger,XAsSingle,yAsSingle)Dimx0AsIntegerDimy0AsIntegerDimiAsIntegerDimjAsIntegerIfXSubWidthOrX14.5*SubWidthOrySubWidthOry14.5*SubWidthThenMsgBox超出棋盘界限,请重新下!ExitSubEndIfIfAbs(Int(X/SubWidth)-X/SubWidth)0.5Thenx0=Int(X/SubWidth)Elsex0=Int(X/SubWidth)+1EndIfIfAbs(Int(y/SubWidth)-y/SubWidth)0.5Theny0=Int(y/SubWidth)Elsey0=Int(y/SubWidth)+1EndIfIfDataArray(x0,y0)3Then'当前位置已经有棋子了,MsgBox当前位置已经有棋子了,请重新走!,vbCritical,NOTE!ExitSubEndIfsumtime=-1CallDrawPill(x0,y0)'画棋子CallRemenberCrossData(x0,y0)'记录棋子信息CallWhoWin'判断谁赢'判断是否开启相应难度计时功能IfifStarteasy=TrueThenTimer2.Enabled=TrueEndIfIfifStartnormal=TrueThenTimer3.Enabled=TrueEndIfIfifStarthard=TrueThenTimer4.Enabled=TrueEndIfEndSub'画棋子PrivateSubDrawPill(xx0AsInteger,yy0AsInteger)IfP2PlayColorThenPicQiPan.ForeColor=vbWhiteDoEventsPicQiPan.FillColor=vbWhitePicQiPan.FillStyle=0MyColor=0ElsePicQiPan.ForeColor=vbBlackDoEventsPicQiPan.FillColor=vbBlackPicQiPan.FillStyle=0MyColor=1EndIfP2PlayColor=NotP2PlayColorPicQiPan.Circle(xx0*SubWidth,yy0*SubWidth),SubWidth*0.5EndSub'以下ABC三个事件共同实现下棋的同时听音乐功能'APrivateSubDir1_Change()File1.Path=Dir1.PathEndSub'BPrivateSubDrv_Change()Dir1.Path=Drv.DriveEndSub'CPrivateSubFile1_Click()mp3.URL=File1.Path&\&File1.FileNameEndSub'棋盘皮肤PrivateSubqipanstylefurA_Click()PicQiPan.BackColor=&HC0FFFFCallCmdStart_ClickEndSubPrivateSubqipanstylefurB_Click()PicQiPan.BackColor=&HC0C000CallCmdStart_ClickEndSubPrivateSubqipanstylefurC_Click()PicQiPan.BackColor=&HE0E0E0CallCmdStart_ClickEndSubPrivateSubqipanstylefurD_Click()PicQiPan.BackColor=&H8080FFCallCmdStart_ClickEndSub'添加四种背景音乐PrivateSubstylemusicA_Click()mp3.URL=App.Path&\&music01.mp3EndSubPrivateSubstylemusicB_Click()mp3.URL=App.Path&\&music02.mp3EndSubPrivateSubstylemusicC_Click()mp3.URL=App.Path&\&music03.mp3EndSubPrivateSubstylemusicD_Click()mp3.URL=App.Path&\&music04.mp3EndSub'表针走动Timer1.Enabled=true在属性框中设定PrivateSubTimer1_Timer()DimsAsIntegerDimmAsIntegerDimhAsIntegerDimsngLenSAsSingleDimsngLenMAsSingleDimsngLenHAsSingleDimiAsInteger'调试几次并查询VB常用函数,最后确定应该使用Now而不是Time(不过之前使用Time确实可以)s=Second(Now)m=Minute(Now)h=Hour(Now)+m/60sngLenS=radius*0.8sngLenM=radius*0.6sngLenH=radius*0.4Pictime.ClsPictime.Scale(-centerx,centery)-(centerx,-centery)Pictime.Line(0,0)-(sngLenS*Sin(2*pi*s/60),sngLenS*Cos(2*pi*s/60)),vbGreenPictime.Line(0,0)-(sngLenM*Sin(2*pi*m/60),sngLenM*Cos(2*pi*m/60)),vbGreenIfh12Thenh=h-12EndIfPictime.Line(0,0)-(sngLenH*Sin(2*pi*h/12),sngLenH*Cos(2*pi*h/12)),vbGreenPictime.Circle(0,0),radius*0.9Fori=1To12Pictime.Circle(radius*0.9*0.85*Sin(2*pi*i/12),radius*0.9*0.85*Cos(2*pi*i/12)),radius*0.01,vbGreenNextiEndSub'判断谁赢了PrivateSubWhoWin()DimiAsIntegerDimjAsIntegerForj=1To14Fori=1To14IfDataArray(i,j)=MyColorAndNotIfSucceedThenIf(14-i)=4And(14-j)=4ThenIfDataArray(i+1,j+1)=MyColorThenIfDataArray(i+2,j+2)=MyColorThenIfDataArray(i+3,j+3)=MyColorThenIfDataArray(i+4,j+4)=MyColorThenIfSucceed=TrueExitForExitForEndIfEndIfEndIfEndIfEndIfIfi4And(14-j)=4ThenIfDataArray(i-1,j+1)=MyColorThenIfDat
本文标题:VB五子棋代码
链接地址:https://www.777doc.com/doc-8761640 .html