Snake

  • hab mal zu Testzwecken das hier geschrieben:

    [autoit]


    #include <GUIConstants.au3>
    #include <Misc.au3>

    [/autoit][autoit][/autoit][autoit]

    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Snake", 600, 600, 193, 125)
    $Label1 = GUICtrlCreateLabel("", 0, 0, 64, 24)
    GUICtrlSetBkColor(-1, 0xFF0000)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit][autoit][/autoit][autoit]

    Global $1 = 0 , $2 = 0
    Func _up()
    While 1
    GUICtrlSetPos ($Label1,$1,$2)
    $2 = 2 +2
    Sleep (20)
    WEnd
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    Func _move()
    While 1
    If _IsPressed ("28") Then
    _up()
    EndIf
    GUICtrlSetPos ($Label1,$1,$2)
    $1 = $1 +2
    Sleep (50)
    WEnd
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    While 1
    If _IsPressed ("20") Then
    _move()
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [/autoit]

    Wieso setzt er aber das Label jetzt nur einmal nach unten?

  • Danke!
    Habe das Script jetzt mal ein wenig erweitert.

    Spoiler anzeigen
    [autoit]

    #include
    #include

    [/autoit]

    #region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Snake", 600, 600, 193, 125)
    $Label1 = GUICtrlCreateLabel("", 0, 0, 64, 24)
    GUICtrlSetBkColor(-1, 0xFF0000)
    GUISetState(@SW_SHOW)
    #endregion ### START Koda GUI section ### Form=

    Global $1 = 0, $2 = 0
    Func _back()
    While 1
    If _IsPressed("27") Then
    _forward()
    EndIf
    If _IsPressed("26") Then
    _up()
    EndIf
    If _IsPressed("28") Then
    _down()
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $1 = $1 - 2
    Sleep(50)
    WEnd
    EndFunc ;==>_back

    Func _down()
    While 1
    If _IsPressed("27") Then
    _forward()
    EndIf
    If _IsPressed("26") Then
    _up()
    EndIf
    If _IsPressed("25") Then
    _back()
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $2 = $2 + 2
    Sleep(50)
    WEnd
    EndFunc ;==>_down

    Func _up()
    While 1
    If _IsPressed("28") Then
    _down()
    EndIf
    If _IsPressed("25") Then
    _back()
    EndIf
    If _IsPressed("27") Then
    _forward()
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $2 = $2 - 2
    Sleep(50)
    WEnd
    EndFunc ;==>_up

    Func _forward()
    While 1
    If _IsPressed("28") Then
    _down()
    EndIf
    If _IsPressed("26") Then
    _up()
    EndIf
    If _IsPressed("25") Then
    _forward()
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $1 = $1 + 2
    Sleep(50)
    WEnd
    EndFunc ;==>_forward

    While 1
    If _IsPressed("20") Then
    _forward()
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [autoit][/autoit]

    Klappt alles wunderbar bis auf das wenn ich grade die "Pfeil Nach Rechts Taste" gedrückt habe und dann die "Pfeil Nach Links Taste" drücke dann stopt das Label nur kurz.
    Ein Bug im Script oder ein Bug von Autoit?


  • Hi

    Ich geb dir einen Tip:
    verwende den OnEventMode und erstelle 4 Buttons (Links, Rechts, Auf, Ab) welche die Snake steuern.
    Mit GuiSetAccelerators kannst du nun den Buttons Tasten zuordnen

    [autoit]

    $LeftID=GUICtrlCreateButton("",0,-90)
    GUICtrlSetOnEvent(-1,"_Left")
    GUICtrlSetState(-1,$GUI_HIDE)
    $RightID=GUICtrlCreateButton("",0,-70)
    GUICtrlSetOnEvent(-1,"_Right")
    GUICtrlSetState(-1,$GUI_HIDE)
    $DownID=GUICtrlCreateButton("",0,-50)
    GUICtrlSetOnEvent(-1,"_Down")
    GUICtrlSetState(-1,$GUI_HIDE)
    $UpID=GUICtrlCreateButton("",0,-30)
    GUICtrlSetOnEvent(-1,"_Up")
    GUICtrlSetState(-1,$GUI_HIDE)
    Dim $AccelKeys[4][2]=[["{Left}", $LeftID], ["{Right}", $RightID], ["{Down}", $DownID], ["{Up}", $UpID]]
    GUISetAccelerators($AccelKeys)

    [/autoit]

    Finde ich einfacher zu handeln als _IsPressed!

    lgE

  • Stimmt , danke.
    Aber benutze jetzt meine Lösung :).
    Aber dsa Problem besteht immernoch :(.

    [autoit]


    #include <GUIConstants.au3>
    #include <Misc.au3>

    [/autoit][autoit][/autoit][autoit]

    #region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Snake", 300, 300,@DesktopHeight / 2 - 50, 0,$WS_POPUP)
    GUISetBkColor(0x808080)
    $Label1 = GUICtrlCreateLabel("", 0, 0, 25, 24)
    GUICtrlSetBkColor(-1, 0xFFFFFF)
    GUISetState(@SW_SHOW)
    #endregion ### START Koda GUI section ### Form=
    AdlibEnable ("_tooltip",10000000)
    _tooltip()
    Global $1 = 0, $2 = 0, $pause = 40
    Func _tooltip()
    ToolTip ("1. Drücke die Leertaste zum Starten." & @CRLF & "2. Steuer mit den Pfeiltasten." & @CRLF & "3.Geschwindigkeit regelst du folgendermaßen:" & @CRLF & "Geschw. erhöhen - Numpad Plus" & @CRLF & "Geschw. veringern - Numpad Minus",0,0,"Steuerung")
    EndFunc

    [/autoit][autoit][/autoit][autoit]

    Func _back()
    While 1
    If _IsPressed("27") Then
    _forward()
    EndIf
    If _IsPressed("26") Then
    _up()
    EndIf
    If _IsPressed("28") Then
    _down()
    EndIf
    If _IsPressed("6B") Then
    $pause = $pause - 10
    EndIf
    If _IsPressed("6D") Then
    $pause = $pause + 10
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $1 = $1 - 2
    Sleep($pause)
    WEnd
    EndFunc ;==>_back

    [/autoit][autoit][/autoit][autoit]

    Func _down()
    While 1
    If _IsPressed("27") Then
    _forward()
    EndIf
    If _IsPressed("26") Then
    _up()
    EndIf
    If _IsPressed("25") Then
    _back()
    EndIf
    If _IsPressed("6B") Then
    $pause = $pause - 10
    EndIf
    If _IsPressed("6D") Then
    $pause = $pause + 10
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $2 = $2 + 2
    Sleep($pause)
    WEnd
    EndFunc ;==>_down

    [/autoit][autoit][/autoit][autoit]

    Func _up()
    While 1
    If _IsPressed("28") Then
    _down()
    EndIf
    If _IsPressed("25") Then
    _back()
    EndIf
    If _IsPressed("27") Then
    _forward()
    EndIf
    If _IsPressed("6B") Then
    $pause = $pause - 10
    EndIf
    If _IsPressed("6D") Then
    $pause = $pause + 10
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $2 = $2 - 2
    Sleep($pause)
    WEnd
    EndFunc ;==>_up

    [/autoit][autoit][/autoit][autoit]

    Func _forward()
    While 1
    If _IsPressed("28") Then
    _down()
    EndIf
    If _IsPressed("26") Then
    _up()
    EndIf
    If _IsPressed("25") Then
    _forward()
    EndIf
    If _IsPressed("6B") Then
    $pause = $pause - 10
    EndIf
    If _IsPressed("6D") Then
    $pause = $pause + 10
    EndIf
    GUICtrlSetPos($Label1, $1, $2)
    $1 = $1 + 2
    Sleep($pause)
    WEnd
    EndFunc ;==>_forward

    [/autoit][autoit][/autoit][autoit]

    While 1
    If WinActive ("Snake") And _IsPressed("20") Then
    _forward()
    EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [/autoit]

    Was könnte ich tuen?
    Falls jemand den oberen Post nicht gelesen hat : Die "Pfeil Taste Links" geht nicht wenn man vorher die "Pfeil Taste Rechts" gedruckt hatte.

    • Offizieller Beitrag

    Wenn das Script lange genug läuft, stürzt es ab.
    Fehlermeldung:
    ==> Recursion level has been exceeded - AutoIt will quit to prevent stack overflow.:
    Das zum Thema Funktionen als Goto Ersatz.

  • also ich habe vor einigen Monaten das Ganze mal Array-basierend gebastelt mit "KI"

    die Steuerung lief auch über ispressed + button

    Auszug:

    Spoiler anzeigen
    [autoit]

    $dll = DllOpen("user32.dll")

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()

    ;#cs
    If _IsPressed("26", $dll) Then ; Pfeil oben
    _Up()
    EndIf

    If _IsPressed("28", $dll) Then ; Pfeil unten
    _Down()
    EndIf

    If _IsPressed("25", $dll) Then ; Pfeil links
    _Left()
    EndIf

    If _IsPressed("27", $dll) Then ; Pfeil rechts
    _Right()
    EndIf

    [/autoit]
    Zitat

    Laughing Man

    "I thought, what I'd do was, I'd pretend I was one of those deaf-mutes"