Ich habe hier mal ein Snake gemacht angelehnt an ein Snake beim Wettbewerb (ich glaube 2006) bei dem mir etwas nicht gefallen hat und ich habe auch ein paar Sachen hinzugefügt.
Kommentar zum Dateianhang:
Snake_p.au3 ist mit Bildern.
Snake.au3 ist ohne Bilder.
Code Snake.au3:
Spoiler anzeigen
;left arrow 25
;up arrow 26
;right arrow 27
;down arrow 28
;space bar 20
#include <GuiConstants.au3>
#include <Array.au3>
#include <file.au3>
Opt("GuiOnEventMode", 1)
[/autoit] [autoit][/autoit] [autoit]if fileexists("Highscore.dat") then
$file = FileOpen("Highscore.dat", 0)
$rett=""
while @error<>-1
$rett = $rett & FileReadLine($file) & @CRLF
wend
fileclose($file)
MsgBox(0, "Highscore", $rett)
$ertt = MsgBox(260, "Highscore", "Möchtest du die Highscore löschen?")
if $ertt=6 then
filedelete("Highscore.dat")
EndIf
endif
MsgBox(0, "Directions", "Benutze die Pfeiltasten um das Spiel zu starten, um die Schlange aufzuwachen. Benutze [SPACE] um zu pausieren.")
$gui = GUICreate("Snake", 400, 420)
$MenuItem1 = GUICtrlCreateMenu("Schwierigkeitsgrad")
$MenuItem3 = GUICtrlCreateMenuItem("Leicht", $MenuItem1)
$MenuItem4 = GUICtrlCreateMenuItem("Mittel", $MenuItem1)
$MenuItem5 = GUICtrlCreateMenuItem("Schwer", $MenuItem1)
$men = GUICtrlCreateMenuItem("Standard", $MenuItem1)
$MenuItem2 = GUICtrlCreateMenu("Map")
$MenuItem6 = GUICtrlCreateMenuItem("Ohne Wände", $MenuItem2)
$MenuItem7 = GUICtrlCreateMenuItem("Mit Rand", $MenuItem2)
$MenuItem8 = GUICtrlCreateMenuItem("Wände", $MenuItem2)
$MenuItem9 = GUICtrlCreateMenuItem("Zufall", $MenuItem2)
$help = GUICtrlCreateMenu("Hilfe")
$about = GUICtrlCreateMenuItem("About", $help)
guiseticon("fire.ico")
GUISetBkColor(0x005500)
GUISetState()
GUISetOnEvent($gui_event_close, "_Exit")
GUIctrlSetOnEvent($MenuItem3,"seteasy")
GUIctrlSetOnEvent($MenuItem4,"setmid")
GUIctrlSetOnEvent($MenuItem5,"setdiff")
GUIctrlSetOnEvent($men,"setstan")
GUIctrlSetOnEvent($MenuItem6,"mapno")
GUIctrlSetOnEvent($MenuItem7,"maprand")
GUIctrlSetOnEvent($MenuItem8,"mapwand")
GUIctrlSetOnEvent($MenuItem9,"mapzufall")
GUIctrlSetOnEvent($about,"ab")
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]Global $dx = 0
Global $dy = -1
Global $clientsize = WinGetClientSize($gui)
Global $speeddef = 250
Global $speedder = 250
Global $high=50
Global $food
Global $snakelength = 1
Global $walls = 160
Dim $board[40][40]; tracks the board
Dim $snake[$snakelength + 1][3]; positions and parts of the snake
Dim $food_location[2]; not possible for multiple food locations?
Dim $wall[$walls]
Dim $wall_location[$walls][2]
Dim $map[40][40]
$op = DllOpen("user32.dll")
mapzufall()
;
AdlibEnable("my",2500)
HotKeySet("{esc}","_Exit")
;
While 1
Sleep($speedder)
If _IsPressed(25) Then
if $dx<>1 Then
$dx = -1
$dy = 0
endif
EndIf
If _IsPressed(27) Then
if $dx<>-1 Then
$dx = 1
$dy = 0
EndIf
EndIf
If _IsPressed(26) Then
if $dy<>1 Then
$dx = 0
$dy = -1
EndIf
EndIf
If _IsPressed(28) Then
if $dy<>-1 Then
$dx = 0
$dy = 1
EndIf
EndIf
move()
If _IsPressed(20) Then
_sleep()
EndIf
WEnd
Func _start()
$snakelength = 1
ReDim $snake[$snakelength + 1][3]
Local $x, $y, $a, $b, $pos
$a = Int(Random(3, 36))
$b = Int(Random(3, 36))
$snake[0][2] = GUICtrlCreateLabel("", $a * 10, $b * 10, 10, 10)
$snake[1][2] = GUICtrlCreateLabel("", $a * 10, ($b+1) * 10, 10, 10)
GUICtrlSetBkColor($snake[0][2], "0xFFFF00")
GUICtrlSetBkColor($snake[1][2], "0xFFFFFF")
$pos = ControlGetPos($gui, "", $snake[0][2])
$snake[0][0] = Int($pos[0] / 10)
$snake[0][1] = Int($pos[1] / 10)
$pos = ControlGetPos($gui, "", $snake[1][2])
$snake[1][0] = Int($pos[0] / 10)
$snake[1][1] = Int($pos[1] / 10)
;MsgBox (0, "Diagnostic", $snake[0][0] & ", " & $snake[0][1])
$board[$a][$b] = 0; snake simplifies the "can I go there?" logic
$board[$a][$b+1] = 0
Do
$a = Int(Random(0, 39))
$b = Int(Random(0, 39))
Until $board[$a][$b] = 1; empty
$food = GUICtrlCreateLabel("", $a * 10, $b * 10, 10, 10)
$pos2 = ControlGetPos($gui, "", $food)
$food_location[0] = $pos2[0] / 10
$food_location[1] = $pos2[1] / 10
GUICtrlSetBkColor($food, "0xFF0000")
$board[$a][$b] = 2; food
EndFunc ;==>_start
Func move()
[/autoit] [autoit][/autoit] [autoit]Local $newhead[3]
$newhead[0] = $snake[0][0] + $dx
$newhead[1] = $snake[0][1] + $dy
Local $oldtail[3]
$oldtail[0] = $snake[$snakelength][0]
$oldtail[1] = $snake[$snakelength][1]
$oldtail[2] = $snake[$snakelength][2]
if $newhead[0] < 0 then $newhead[0]=39
if $newhead[0] > 39 then $newhead[0]=0
if $newhead[1] < 0 then $newhead[1]=39
if $newhead[1] > 39 then $newhead[1]=0
Select
Case $board[$newhead[0]][$newhead[1]]=1 or $board[$newhead[0]][$newhead[1]]=2; OK to go there
$newhead[2] = GUICtrlCreateLabel("", $newhead[0] * 10, $newhead[1] * 10, 10, 10)
GUICtrlSetBkColor($newhead[2], "0xFFFF00")
If $board[$newhead[0]][$newhead[1]] = 2 Then; Ate Food
; Move food
Do
$a = Int(Random(0, 39))
$b = Int(Random(0, 39))
Until $board[$a][$b] = 1; empty
$board[$a][$b] = 2; food
GUICtrlSetPos($food, $a * 10, $b * 10)
$snakelength = $snakelength + 1
ReDim $snake[$snakelength + 1][3]
ElseIf $board[$newhead[0]][$newhead[1]] = 5 Then;wall
If $snakelength > 1 Then
MsgBox(0, "Your snake ate the wall.", "VERLOREN! Deine Schlange war "& ($snakelength + 1) & " punkte lang")
$input = InputBox("Highscrore", "Dein Name")
FileWrite("Highscore.dat", $input & " hat "& ($snakelength + 1) & " Punkte erreicht!" & @CRLF)
Else
MsgBox(0, "Your snake ate the wall.", "You should play with your snake more.")
EndIf
_cleanup()
Else
GUICtrlDelete($oldtail[2])
$board[$oldtail[0]][$oldtail[1]] = 1
EndIf
$board[$newhead[0]][$newhead[1]] = 0
For $i = $snakelength To 1 Step - 1; Array shift
$snake[$i][0] = $snake[$i - 1][0]
$snake[$i][1] = $snake[$i - 1][1]
$snake[$i][2] = $snake[$i - 1][2]
GUICtrlSetBkColor($snake[$i][2],"0xFFFFFF")
Next
$snake[0][0] = $newhead[0]
$snake[0][1] = $newhead[1]
$snake[0][2] = $newhead[2]
Case Else
If $snakelength > 1 Then
MsgBox(0, "Your snake ate the wall.", "VERLOREN! Deine Schlange war "& ($snakelength + 1) & " punkte lang")
$input = InputBox("Highscrore", "Dein Name")
FileWrite("Highscore.dat", $input & " hat "& ($snakelength + 1) & " Punkte erreicht!" & @CRLF)
Else
MsgBox(0, "Your snake ate the wall.", "You should play with your snake more.")
EndIf
_cleanup()
EndSelect
EndFunc ;==>move
Func _cleanup()
For $x=0 to $walls-1
GUICtrlDelete($wall[$x])
Next
$zahl=0
For $x = 0 To 39
For $y = 0 To 39
$board[$x][$y] = $map[$x][$y]
if $map[$x][$y]=5 then
if $zahl+1>$walls then _Exit()
$wall[$zahl] = GUICtrlCreateLabel("", $x * 10, $y * 10, 10, 10)
$pos3 = ControlGetPos($gui, "", $wall[$zahl])
$wall_location[$zahl][0] = $pos3[0] / 10
$wall_location[$zahl][1] = $pos3[1] / 10
GUICtrlSetBkColor($wall[$zahl], "0x00FF00")
$zahl=$zahl+1
endif
Next
Next
GUICtrlDelete($food)
[/autoit] [autoit][/autoit] [autoit]For $i = 0 To $snakelength
GUICtrlDelete($snake[$i][2])
Next
$speedder=$speeddef
$dx=0
$dy=-1
_start() ; This is going to choke after 300 something calls
_sleep()
EndFunc ;==>_cleanup
Func _Exit()
DllClose($op)
AdlibDisable()
Exit
EndFunc ;==>_Exit
Func _sleep()
AdlibDisable()
Do
Do
Sleep(100)
Until _IsPressed(25) Or _IsPressed(26) Or _IsPressed(27) Or _IsPressed(28)
If _IsPressed(25) Then
if $dx<>1 Then
$dx = -1
$dy = 0
endif
EndIf
If _IsPressed(27) Then
if $dx<>-1 Then
$dx = 1
$dy = 0
EndIf
EndIf
If _IsPressed(26) Then
if $dy<>1 Then
$dx = 0
$dy = -1
EndIf
EndIf
If _IsPressed(28) Then
if $dy<>-1 Then
$dx = 0
$dy = 1
EndIf
EndIf
Until _IsPressed(25) Or _IsPressed(26) Or _IsPressed(27) Or _IsPressed(28)
AdlibEnable("my",2500)
EndFunc ;==>_sleep
Func _IsPressed($hexkey)
Local $ar, $brv
$hexkey = '0x' & $hexkey
$ar = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexkey)
If $ar[0] <> 0 Then
$brv = 1
Else
$brv = 0
EndIf
Return $brv
EndFunc ;==>_IsPressed
;
Func my()
if $speedder>$high then $speedder=$speedder-5
EndFunc
Func seteasy()
$speeddef=250
$speedder=250
$high=150
_cleanup()
EndFunc
Func setmid()
$speeddef=150
$speedder=150
$high=100
_cleanup()
EndFunc
Func setdiff()
$speeddef=100
$speedder=100
$high=50
_cleanup()
EndFunc
Func setstan()
$speeddef=250
$speedder=250
$high=50
_cleanup()
EndFunc
Func mapno()
for $x=0 to 39
for $y=0 to 39
$map[$x][$y]=1
Next
Next
_cleanup()
EndFunc
Func maprand()
for $x=0 to 39
for $y=0 to 39
$map[$x][$y]=1
Next
Next
for $x=0 to 39
$map[$x][0]=5
next
for $y=0 to 39
$map[0][$y]=5
next
for $x=0 to 39
$map[$x][39]=5
next
for $y=0 to 39
$map[39][$y]=5
next
_cleanup()
EndFunc
Func mapwand()
for $x=0 to 39
for $y=0 to 39
$map[$x][$y]=1
Next
Next
for $x=7 to 14
$map[$x][14]=5
next
for $y=7 to 14
$map[14][$y]=5
next
for $x=7 to 14
$map[$x][27]=5
next
for $y=7 to 14
$map[27][$y]=5
next
for $x=23 to 30
$map[$x][10]=5
next
for $y=23 to 30
$map[10][$y]=5
next
for $x=23 to 30
$map[$x][23]=5
next
for $y=23 to 30
$map[23][$y]=5
next
_cleanup()
EndFunc
Func mapzufall()
for $x=0 to 39
for $y=0 to 39
$map[$x][$y]=1
Next
Next
for $x=0 to 39
Do
$a = Int(Random(0, 39))
$b = Int(Random(0, 39))
Until $map[$a][$b] = 1; empty
$map[$a][$b] = 5; wall
next
_cleanup()
EndFunc
Func ab()
msgbox(0,"About","Version: 2.0")
EndFunc
EDIT:
Das Icon hab ich leider verloren, aber das macht ja zum Glück keinen Fehler