3月102016
还记得小时候玩的16宫格游戏版吗
16宫格游戏版,游戏源码是不是很赞呢!!!
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <Timers.au3>
#include <Date.au3>
;Code By ScriptFans
;From [url]www.autoitx.com[/url]
;==========================界面设置,关联事件函数=================
Opt("GUIOnEventMode",1)
$hGUI=GUICreate("16宫",240,303)
GUISetOnEvent($GUI_EVENT_CLOSE,"Quit")
$idBegin=GUICtrlCreateButton("开始",0,240,120,40)
GUICtrlSetOnEvent(-1,"Begin")
$idBegin=GUICtrlCreateButton("排行榜",120,240,120,40)
GUICtrlSetOnEvent(-1,"GetRanking")
Global $n=0,$aButton[15][3],$aBlankCoor[2]=[180,180],$sOriginCoor
For $i=0 To 3
For $j=0 To 3
If $i=3 And $j=3 Then ExitLoop
$iX=$j*60
$iY=$i*60
$aButton[$n][0]=GUICtrlCreateButton($n+1,$iX,$iY,60,60)
GUICtrlSetBkColor(-1,0x00AA80+$i*0xC0)
GUICtrlSetFont(-1,15,800)
GUICtrlSetState(-1,$GUI_DISABLE)
$aButton[$n][1]=$iX
$aButton[$n][2]=$iY
$sOriginCoor&=$iX&$iY
$n+=1
Next
Next
$n=0 ;临时变量用完要重置
For $i=0 To UBound($aButton)-1
GUICtrlSetOnEvent($aButton[$i][0],"ClickMove")
Next
Global $aParts[2] = [30,200],$g_hStatus
$g_hStatus = _GUICtrlStatusBar_Create($hGUI, $aParts)
_GUICtrlStatusBar_SetText($g_hStatus, "计时",0)
;创建timer,通过设置$bTimer的值来控制计时
Global $iCount=0,$iSec=0,$iSec_Point=0,$idTimer,$bTimer=False
$idTimer=_Timer_SetTimer($hGUI, 100, "_UpdateStatusBarClock")
GUISetState(@SW_SHOW)
While 1
Sleep(1000)
WEnd
;=============================函数=====================
Func Begin()
For $i=0 To UBound($aButton)-1
GUICtrlSetState($aButton[$i][0],$GUI_ENABLE)
Next
For $x=1 To 60
Local $incORdec=1,$iX,$iY,$aSideCoor[0][3],$sFill="",$iRanIndex,$iStepX,$iStepY,$iLastID
;查找空白周边可移动的button
For $i=0 To 1
For $j=0 To 1
$incORdec=-$incORdec
$iX=$aBlankCoor[0]+$i*$incORdec*60
$iY=$aBlankCoor[1]+(1-$i)*$incORdec*60
For $k=0 To UBound($aButton)-1
If $aButton[$k][1]&$aButton[$k][2]==$iX&$iY Then $sFill&=$aButton[$k][0]&"|"&$iX&"|"&$iY&@CRLF
Next
Next
Next
$sFill=StringLeft($sFill,StringLen($sFill)-2)
;将可移动的button信息装入数组
_ArrayAdd($aSideCoor,$sFill)
Do
$iRanIndex=Random(0,UBound($aSideCoor)-1,1)
Until Not ($aSideCoor[$iRanIndex][0]=$iLastID)
;记录上一个移动的button,避免重复移动
$iLastID=$aSideCoor[$iRanIndex][0]
Move($aSideCoor[$iRanIndex][0],$aSideCoor[$iRanIndex][1],$aSideCoor[$iRanIndex][2],$aBlankCoor[0],$aBlankCoor[1])
Local $iIndex=_ArraySearch($aButton,$aSideCoor[$iRanIndex][0])
;交换移动方块和空白方块的坐标
$aButton[$iIndex][1]=$aBlankCoor[0]
$aButton[$iIndex][2]=$aBlankCoor[1]
$aBlankCoor[0]=$aSideCoor[$iRanIndex][1]
$aBlankCoor[1]=$aSideCoor[$iRanIndex][2]
Sleep(50)
Next
;开始计时
$bTimer=True
EndFunc
Func ClickMove()
Local $iIndex
$iIndex=_ArraySearch($aButton,@GUI_CtrlId,0,0,0,0)
Local $incORdec=1,$iX,$iY,$iStepX,$iStepY
For $i=0 To 1
For $j=0 To 1
;判断点击的button是否可移动
$incORdec=-$incORdec
$iX=$aButton[$iIndex][1]+$i*$incORdec*60
$iY=$aButton[$iIndex][2]+(1-$i)*$incORdec*60
If $iX&$iY==$aBlankCoor[0]&$aBlankCoor[1] Then
Move(@GUI_CtrlId,$aButton[$iIndex][1],$aButton[$iIndex][2],$iX,$iY)
$aBlankCoor[0]=$aButton[$iIndex][1]
$aBlankCoor[1]=$aButton[$iIndex][2]
$aButton[$iIndex][1]=$iX
$aButton[$iIndex][2]=$iY
;判断是否还原
$sCurrentCoor=""
For $l=0 To UBound($aButton)-1
$sCurrentCoor&=$aButton[$l][1]&$aButton[$l][2]
Next
If $sCurrentCoor==$sOriginCoor Then
;停止计时
$bTimer=False
For $k=0 To UBound($aButton)-1
GUICtrlSetState($aButton[$k][0],$GUI_DISABLE)
Next
;记录成绩
Local $sPlayerName,$hFile,$tCur,$sTime
$sPlayerName=InputBox("提示","输入你的名字","","",100,130)
$hFile=FileOpen("Record",1)
$tCur = _Date_Time_GetLocalTime()
$sTime=_Date_Time_SystemTimeToDateTimeStr($tCur)
FileWrite($hFile,$sPlayerName&"|"&$iSec&"."&$iSec_Point&"|"&"秒"&"|"&$sTime&@CRLF)
FileClose($hFile)
EndIf
ExitLoop
EndIf
Next
Next
EndFunc
Func Move($idButton,$iStartX,$iStartY,$iEndX,$iEndY)
Local $iStepX,$iStepY
$iStepX=($iEndX-$iStartX)/60
$iStepY=($iEndY-$iStartY)/60
For $i=0 To 60
GUICtrlSetPos($idButton,$iStartX+$iStepX*$i,$iStartY+$iStepY*$i)
Next
EndFunc
Func _UpdateStatusBarClock($hWnd, $iMsg, $iIDTimer, $iTime)
#forceref $hWnd, $iMsg, $iIDTimer, $iTime
If $bTimer Then
$iCount+=1
$iSec=Int($iCount/10)
$iSec_Point=Mod($iCount,10)
_GUICtrlStatusBar_SetText($g_hStatus, $iSec&":"&$iSec_Point,1)
Else
$iCount=0
EndIf
EndFunc
Func GetRanking()
Local $hFile,$aDisplay[0][4],$sRecord
$hFile=FileOpen("ReCord",0)
$sRecord=FileRead($hFile)
$sRecord=StringLeft($sRecord,StringLen($sRecord)-2)
FileClose($hFile)
_ArrayAdd($aDisplay,$sRecord)
For $i=0 To UBound($aDisplay)-1
$aDisplay[$i][1]=Number($aDisplay[$i][1])
Next
_ArraySort($aDisplay,0,0,0,1)
_ArrayDisplay($aDisplay,"排名","10",32+64+4)
EndFunc
Func Quit()
;必须先停止对StatusBar的修改程序才能完全退出
$bTimer=False
_Timer_KillAllTimers($hGUI)
GUIDelete($hGUI)
Exit
EndFunc
扫描二维码,在手机上阅读
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!