Halli hallo,
da ich mir vor einer Weile einen Mikrocontroller bestellt habe auf dem ich kleine Spiele spielen möchte (mit einer 9x14 LED-Matrix und 4x4 Keypad-Matrix) habe ich hier Snake gecodet.
Ich wollte es so einfach wie möglich halten damit ich den Code für das Board übernehmen kann.
Feedback ist gerne gesehen!
AutoIt
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
Opt("GUIOnEventMode", 1)
Global $hDLL = DllOpen("user32.dll")
Local $iScale = 15
Local $aDimensions = StringSplit(InputBox("Size", "Field size:", "20x20"), "x", 2)
Local $aControls[$aDimensions[0]][$aDimensions[1]]
Local $aPlayerPosition[2][3] = [[3, 2], [3, 3, "down"]]
Local $iIncX = 0, $iIncY = 1
Local $aBonbon[2]
Local $aOldPosition[2] = [$aPlayerPosition[0][0], $aPlayerPosition[1][1]]
Local $iScore = 0
$bEscape = False
While Not $bEscape
$x = Random(0, $aDimensions[0] - 1, 1)
$y = Random(0, $aDimensions[1] - 1, 1)
$bEscape = True
For $i = 0 To UBound($aPlayerPosition) - 1
If $aPlayerPosition[$i][0] = $x or $aPlayerPosition[$i][1] = $y Then $bEscape = False
Next
WEnd
$aBonbon[0] = $x
$aBonbon[1] = $y
GUICreate("Snake", $aDimensions[0] * $iScale, $aDimensions[1] * $iScale, -1, -1)
For $y = 0 To $aDimensions[1] * $iScale - 15 Step $iScale
For $x = 0 To $aDimensions[0] * $iScale - 15 Step $iScale
$aControls[$x / $iScale][$y / $iScale] = GUICtrlCreateLabel("", $x, $y, $iScale, $iScale)
GUICtrlSetBkColor(-1, 0)
Next
Next
GUICtrlSetBkColor($aControls[$aPlayerPosition[0][0]][$aPlayerPosition[0][1]], 0x00FF00)
GUICtrlSetBkColor($aControls[$aPlayerPosition[1][0]][$aPlayerPosition[1][1]], 0x00FF00)
GUICtrlSetBkColor($aControls[$aBonbon[0]][$aBonbon[1]], 0x00FF00)
GUISetOnEvent(-3, "_Exit")
GUISetState(@SW_SHOW)
While 1
$sButton = _Sleep(200)
$aOldPosition[0] = $aPlayerPosition[0][0]
$aOldPosition[1] = $aPlayerPosition[0][1]
If $sButton = "26" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "down" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "up" Then
$aPlayerPosition[UBound($aPlayerPosition) - 1][2] = "up"
$iIncY = -1
$iIncX = 0
ElseIf $sButton = "25" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "right" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "left" Then
$aPlayerPosition[UBound($aPlayerPosition) - 1][2] = "left"
$iIncX = -1
$iIncY = 0
ElseIf $sButton = "27" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "left" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "right" Then
$aPlayerPosition[UBound($aPlayerPosition) - 1][2] = "right"
$iIncX = 1
$iIncY = 0
ElseIf $sButton = "28" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "up" and $aPlayerPosition[UBound($aPlayerPosition) - 1][2] <> "down" Then
$aPlayerPosition[UBound($aPlayerPosition) - 1][2] = "down"
$iIncY = 1
$iIncX = 0
EndIf
For $i = 0 To UBound($aPlayerPosition) - 1
If $aPlayerPosition[UBound($aPlayerPosition) - 1][0] + $iIncX = $aPlayerPosition[$i][0] and $aPlayerPosition[UBound($aPlayerPosition) - 1][1] + $iIncY = $aPlayerPosition[$i][1] Then _Exit()
Next
If $aPlayerPosition[UBound($aPlayerPosition) - 1][0] + $iIncX = $aBonbon[0] and $aPlayerPosition[UBound($aPlayerPosition) - 1][1] + $iIncY = $aBonbon[1] Then
ReDim $aPlayerPosition[UBound($aPlayerPosition) + 1][3]
$aPlayerPosition[UBound($aPlayerPosition) - 1][0] = $aBonbon[0]
$aPlayerPosition[UBound($aPlayerPosition) - 1][1] = $aBonbon[1]
$aPlayerPosition[UBound($aPlayerPosition) - 1][2] = $aPlayerPosition[UBound($aPlayerPosition) - 2][2]
If UBound($aPlayerPosition) = $aDimensions[0] * $aDimensions[1] Then
MsgBox(64, "Congratulations", "Good job, you beat the game!")
Exit
EndIf
$bEscape = False
While Not $bEscape
$x = Random(0, $aDimensions[0] - 1, 1)
$y = Random(0, $aDimensions[1] - 1, 1)
$bEscape = True
For $i = 0 To UBound($aPlayerPosition) - 1
If $aPlayerPosition[$i][0] = $x or $aPlayerPosition[$i][1] = $y Then $bEscape = False
Next
WEnd
$aBonbon[0] = $x
$aBonbon[1] = $y
GUICtrlSetBkColor($aControls[$aBonbon[0]][$aBonbon[1]], 0x00FF00)
$iScore += 1
Else
For $i = 1 To UBound($aPlayerPosition) - 1
$aPlayerPosition[$i - 1][0] = $aPlayerPosition[$i][0]
$aPlayerPosition[$i - 1][1] = $aPlayerPosition[$i][1]
Next
$aPlayerPosition[UBound($aPlayerPosition) - 1][0] = $aPlayerPosition[UBound($aPlayerPosition) - 2][0] + $iIncX
$aPlayerPosition[UBound($aPlayerPosition) - 1][1] = $aPlayerPosition[UBound($aPlayerPosition) - 2][1] + $iIncY
EndIf
If $aPlayerPosition[UBound($aPlayerPosition) - 1][0] < 0 or $aPlayerPosition[UBound($aPlayerPosition) - 1][0] > $aDimensions[0] - 1 or $aPlayerPosition[UBound($aPlayerPosition) - 1][1] < 0 or $aPlayerPosition[UBound($aPlayerPosition) - 1][1] > $aDimensions[1] - 1 Then _Exit()
For $i = 0 To UBound($aPlayerPosition) - 1
GUICtrlSetBkColor($aControls[$aPlayerPosition[$i][0]][$aPlayerPosition[$i][1]], 0x00FF00)
Next
GUICtrlSetBkColor($aControls[$aOldPosition[0]][$aOldPosition[1]], 0)
WEnd
Func _Sleep($iMs)
Local $hTimer = TimerInit()
Local $sKey = ""
While TimerDiff($hTimer) < $iMs
$sKey = _IsPressed("26", $hDLL) ? "26" : _IsPressed("25", $hDLL) ? "25" : _IsPressed("27", $hDLL) ? "27" : _IsPressed("28", $hDLL) ? "28" : $sKey
WEnd
Return $sKey
EndFunc
Func _Exit()
MsgBox(64, "Oops!", "You failed!" & @CRLF & "Score: " & $iScore)
Exit
EndFunc
Alles anzeigen
Wer will kann es auch gern über den Anhang herunterladen.