绿色风's Blog
专注AutoIT(Au3)
  • 首页
  • 流●年
  • 笔●记
    • 学习随记
    • 源码示例
  • 脚●本
    • UDF(收集)
    • 工作室UDF
    • 工具●教程
    • 教程之GDI
  • 作●品
  • 下●载
  • 情怀ExcelTip
3月102016

还记得小时候玩的16宫格游戏版吗

作者:绿色风   发布:2016-3-10 12:04 Thursday   分类:   阅读:4907次   评论:0条  

test6.gif

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




本文固定链接: http://www.jianyiit.com/post-166.html

blogger
该日志由 绿色风 于2016-3-10 12:04 Thursday发表在 分类下。
版权所有:《绿色风's Blog》 → 《还记得小时候玩的16宫格游戏版吗》;
除特别标注,本博客很多文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:

扫描二维码,在手机上阅读
上一篇::AutoIt 正则表达式参考与学习[序]
下一篇:Windows消息代码

热门文章

相关文章

  • Au3 屏幕取色器
  • 笔记本电池电量显示
  • 跟随鼠标的图型
  • Au3简单的多线程例子
  • LED时间显示
取消回复

发表评论

亲,头像对么?

33 + 15 =

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


    站点统计
    • 运行时间: 20254 天
    • 日志总数: 365 篇
    • 评论数量: 7254 条
    • 微语数量: 6 条
    • 附件总量: 388 件
  • 逝出的青春

  • 打赏"绿色风"



      扫码关注本站公众号 可搜本站内容

  • Autoit V3 脚本交流群

      常驻群1:905774875
      常驻群2:40672266


  • 链接

    • AU3中文论坛
    • Excel资料库
    • 完美者博客
    • 顺网小哥'S Blog
    • 猛牛哥的博客
    • 网吧系统下载
  • 分类

    • 流●年(66)
    • 笔●记(0)
    • 脚●本(0)
    • 作品(21)
    • 学习随记(51)
    • 源码示例(68)
    • UDF(收集)(26)
    • 工作室UDF(30)
    • 工具●教程(62)
    • 教程之GDI(24)
Copyright © 2013 绿色风's Blog. Powered by emlog. Theme by 射雕天龙. 鄂ICP备2021011689号-1 鄂公网安备42102302000078号 sitemap