首页 > 代码库 > au3 贪吃蛇游戏 研究与学习
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,leftLocal $count = 4, $movedir = 1, $turndir = 0Local $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) ;,0x80000000For $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)Nextmyrand()AdlibRegister("movesnake", $speed)GUIRegisterMsg(0x0100, "mykeydown") ;$WM_KEYDOWN = 0x0100GUISetState(@SW_SHOW)MsgBox(0, "贪吃蛇", "开始游戏(需要使用wasd来表示方向)")While True GUIGetMsg()WEndFunc 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 EndIfEndFunc ;==>movesnakeFunc 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) ExitEndFunc ;==>myexitFunc 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 EndSwitchEndFunc ;==>mykeydownFunc 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 0EndFunc ;==>checksameFunc 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 ;==>myrandFunc 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 1EndFunc ;==>eatpoint
au3 贪吃蛇游戏 研究与学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。