au3 贪吃蛇游戏 研究与学习


贪吃蛇小游戏  部分函数要点

起定时器,$speed为每分钟调用次数   ,不知道

 

有参数的怎么处理
AdlibRegister("movesnake", $speed)

产生随机数
Random(0, $pos[0] / 14 - 1, 1)

疑问:
ReDim $labelarray[$count][3]
不会丢失之前的数据么,查资料是要加上保留属性的  如 ReDim   Preserve   X(10,   10,   15)   

注册事件处理函数
GUIRegisterMsg(0x0100, "mykeydown") ;$WM_KEYDOWN = 0x0100

 

附代码:

Const $speed = 200, $dirfuhao[4][2] = [[0, -1],[1, 0],[0, 1],[-1, 0]] ;up,right,down,left
Local $count = 4, $movedir = 1, $turndir = 0
Local $top = IniRead(@ScriptDir & "\snakecfg.ini", "config", "top", 0)
Local $labelarray[$count][3], $lastnum = $count - 1, $randpoint[3], $pos[2] = [280, 196] ;280/14=20列,196/14=14行

$Form1 = GUICreate("Form1", $pos[0], $pos[1], -1, -1, 0x80000000) ;,0x80000000
For $i = 0 To $lastnum
    $labelarray[$i][0] = $pos[0] / 2 - 14 * $i ;x pos
    $labelarray[$i][1] = $pos[1] / 2 ;y pos
    $labelarray[$i][2] = GUICtrlCreateLabel("□", $labelarray[$i][0], $labelarray[$i][1], 14, 14, 0x0201)
    GUICtrlSetFont(-1, 15)
    GUICtrlSetState(-1, 128)
    GUICtrlSetBkColor(-1, 0x01CC01)
Next

myrand()
AdlibRegister("movesnake", $speed)
GUIRegisterMsg(0x0100, "mykeydown") ;$WM_KEYDOWN = 0x0100
GUISetState(@SW_SHOW)
MsgBox(0, "贪吃蛇", "开始游戏(需要使用wasd来表示方向)")
While True
    GUIGetMsg()
WEnd

Func movesnake()
    If $turndir And Mod($turndir - $movedir, 2) Then
        $movedir = $turndir - 4
        $turndir = 0
    EndIf
    If eatpoint() Then Return
    Switch $movedir
        Case 1 ;turn right
            If $labelarray[Mod($lastnum + 1, $count)][0] + 28 > $pos[0] Or checksame(1) Then myexit()
        Case 2 ;go down
            If $labelarray[Mod($lastnum + 1, $count)][1] + 28 > $pos[1] Or checksame(2) Then myexit()
        Case 3 ;turn left
            If $labelarray[Mod($lastnum + 1, $count)][0] - 14 < 0 Or checksame(3) Then myexit()
        Case 0 ;go up
            If $labelarray[Mod($lastnum + 1, $count)][1] - 14 < 0 Or checksame(0) Then myexit()
    EndSwitch
    $labelarray[$lastnum][0] = $labelarray[Mod($lastnum + 1, $count)][0] + $dirfuhao[$movedir][0] * 14
    $labelarray[$lastnum][1] = $labelarray[Mod($lastnum + 1, $count)][1] + $dirfuhao[$movedir][1] * 14
    GUICtrlSetPos($labelarray[$lastnum][2], $labelarray[$lastnum][0], $labelarray[$lastnum][1])
    If $lastnum Then
        $lastnum -= 1
    Else
        $lastnum = $count - 1
    EndIf
EndFunc   ;==>movesnake

Func myexit()
    AdlibUnRegister("movesnake")
    GUIRegisterMsg(0x0100, "")
    If $count > Number($top) Then
        IniWrite(@ScriptDir & "\snakecfg.ini", "config", "top", $count)
        MsgBox(0, "恭喜", "你创造了新记录:" & $count & "块" & @CR & "你的速度是" & $speed & "ms/step")
    Else
        MsgBox(0, "", "游戏结束")
    EndIf
    GUIDelete($Form1)
    Exit
EndFunc   ;==>myexit

Func mykeydown($hWnd, $iMsg, $wParam, $lParam)
    Switch $wParam
        Case 0x057 ;w up
            $turndir = 4
        Case 0x053 ;s down
            $turndir = 6
        Case 0x041 ;a left
            $turndir = 7
        Case 0x044 ;d right
            $turndir = 5
        Case Else
            Return
    EndSwitch
EndFunc   ;==>mykeydown

Func checksame($dir)
    For $i = 0 To $count - 1
        If $i = $lastnum Or $i = Mod($lastnum + 1, $count) Then ContinueLoop
        If $labelarray[Mod($lastnum + 1, $count)][0] + $dirfuhao[$dir][0] * 14 = $labelarray[$i][0] And $labelarray[Mod($lastnum + 1, $count)][1] + $dirfuhao[$dir][1] * 14 = $labelarray[$i][1] Then Return 1
    Next
    Return 0
EndFunc   ;==>checksame

Func myrand()
    While True
        $randpoint[0] = Random(0, $pos[0] / 14 - 1, 1)
        $randpoint[1] = Random(0, $pos[1] / 14 - 1, 1)
        For $i = 0 To $count - 1
            If $randpoint[0] * 14 = $labelarray[$i][0] And $randpoint[1] * 14 = $labelarray[$i][1] Then ExitLoop
        Next
        If $i = $count Then ExitLoop
    WEnd
    $randpoint[0] *= 14
    $randpoint[1] *= 14
    $randpoint[2] = GUICtrlCreateLabel("□", $randpoint[0], $randpoint[1], 14, 14, 0x0201)
    GUICtrlSetFont(-1, 15)
    GUICtrlSetState(-1, 128)
    GUICtrlSetBkColor(-1, 0x01CC01)
EndFunc   ;==>myrand

Func eatpoint()
    If $labelarray[Mod($lastnum + 1, $count)][0] + $dirfuhao[$movedir][0] * 14 <> $randpoint[0] Or $labelarray[Mod($lastnum + 1, $count)][1] + $dirfuhao[$movedir][1] * 14 <> $randpoint[1] Then Return 0
    $count += 1
    ReDim $labelarray[$count][3]
    If $lastnum + 2 < $count Then
        For $i = $count - 1 To $lastnum + 2 Step -1
            $labelarray[$i][0] = $labelarray[$i - 1][0]
            $labelarray[$i][1] = $labelarray[$i - 1][1]
            $labelarray[$i][2] = $labelarray[$i - 1][2]
        Next
    EndIf
    $labelarray[$lastnum + 1][0] = $randpoint[0]
    $labelarray[$lastnum + 1][1] = $randpoint[1]
    $labelarray[$lastnum + 1][2] = $randpoint[2]
    myrand()
    Return 1
EndFunc   ;==>eatpoint

 

posted @ 2014-11-10 01:05  ZhiHeng123  阅读(323)  评论(0编辑  收藏  举报