2D Spiel

  • [font='Trebuchet MS'][color=#333333]Hallo,
    es wurde mal auf Autoit.de ein Script released (Snake), und ich wollte mir daraus ein 2D Spiel machen, da ich nie in der Lage wäre, so etwas
    aus der Hand zu schreiben habe ich den Code genommen, und Teile raus gelöscht, hat auch soweit alles geklappt, nur wen ich meinen ersten schritt mache, kommt ein Teil dazu, ich habe gestern
    schon den ganzen Abend damit verbracht, den Fehler zu finden, aber auch heute morgen habe ich nichts gefunden, ich hoffe, das jemand
    den Fehler findet...

    Spoiler anzeigen

    #include <WindowsConstants.au3>
    #include <GuiConstants.au3>
    #include <Array.au3>
    #include <file.au3>
    #include <StaticConstants.au3>


    $gui = GUICreate("RPG", 400, 400)
    GUISetBkColor(0x000099)

    GUISetState()

    $dll = DllOpen ("user32.dll")
    ;Global $clientsize = WinGetClientSize($gui)
    ;Global $speedder = 150
    ;Global $food
    $snakelength = 0
    Dim $board[40][40]; tracks the board
    Dim $snake [10][10];[$snakelength + 1][3]; positions and parts of the snake
    ;Dim $food_location[2]; not possible for multiple food locations?


    Global $dx = 0
    Global $dy = 1

    _Start ()

    While 1
    Sleep (140)
    GUICtrlDelete ($Snake [1][0])
    If _IsPressed(25) Then ;= Links
    $dx = -1
    $dy = 0
    move ()
    EndIf
    If _IsPressed(27) Then ;= Rechts
    $dx = 1
    $dy = 0
    move ()
    EndIf
    If _IsPressed(26) Then ;= Hoch
    $dx = 0
    $dy = -1
    move ()
    EndIf
    If _IsPressed(28) Then ;= Runter
    $dx = 0
    $dy = 1
    move ()
    EndIf


    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch

    WEnd
    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
    Func _Start ()
    $snakelength = 1
    ;ReDim $snake;[$snakelength + 1][3]
    Local $x, $y, $a, $b, $pos
    For $x = 0 To 39
    For $y = 0 To 39
    $board[$x][$y] = 1; empty
    Next
    Next
    $a = Int(Random(3, 36))
    $b = Int(Random(3, 36))
    $snake[0][2] = GUICtrlCreateLabel("", $a * 10, $b * 10, 10, 10)
    GUICtrlSetBkColor($snake[0][2], 16777215)
    $pos = ControlGetPos($gui, "", $snake[0][2])
    $snake[0][0] = Int($pos[0] / 10)
    $snake[0][1] = Int($pos[1] / 10)
    EndFunc

    Func move()

    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]

    Select
    Case $newhead[0] < 0 Or $newhead[0] > 39 Or $newhead[1] < 0 Or $newhead[1] > 39
    If $snakelength > 0 Then
    ;
    EndIf


    Case $board[$newhead[0]][$newhead[1]]; OK to go there
    $newhead[2] = GUICtrlCreateLabel("", $newhead[0] * 10, $newhead[1] * 10, 10, 10)
    GUICtrlSetBkColor($newhead[2], 16777215)
    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]
    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]
    Next
    $snake[0][0] = $newhead[0]
    $snake[0][1] = $newhead[1]
    $snake[0][2] = $newhead[2]
    EndSelectEndFunc

    Liebe grüße ^^

  • so vllt?

    Spoiler anzeigen
    [autoit]

    #include <WindowsConstants.au3>
    #include <GuiConstants.au3>
    #include <Array.au3>
    #include <file.au3>
    #include <StaticConstants.au3>


    $gui = GUICreate("RPG", 400, 400)
    GUISetBkColor(0x000099)

    GUISetState()

    $dll = DllOpen ("user32.dll")
    ;Global $clientsize = WinGetClientSize($gui)
    ;Global $speedder = 150
    ;Global $food
    $snakelength = 0
    Dim $board[40][40]; tracks the board
    Dim $snake [10][10];[$snakelength + 1][3]; positions and parts of the snake
    ;Dim $food_location[2]; not possible for multiple food locations?



    Global $dx = 0
    Global $dy = 1

    _Start ()

    While 1
    Sleep (140)
    GUICtrlDelete ($Snake [1][0])
    If _IsPressed(25) Then ;= Links
    $dx = -1
    $dy = 0
    move ()
    EndIf
    If _IsPressed(27) Then ;= Rechts
    $dx = 1
    $dy = 0
    move ()
    EndIf
    If _IsPressed(26) Then ;= Hoch
    $dx = 0
    $dy = -1
    move ()
    EndIf
    If _IsPressed(28) Then ;= Runter
    $dx = 0
    $dy = 1
    move ()
    EndIf


    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch

    WEnd
    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
    Func _Start ()
    $snakelength = 1
    ;ReDim $snake;[$snakelength + 1][3]
    Local $x, $y, $a, $b, $pos
    For $x = 0 To 39
    For $y = 0 To 39
    $board[$x][$y] = 1; empty
    Next
    Next
    $a = Int(Random(3, 36))
    $b = Int(Random(3, 36))
    $snake[0][2] = GUICtrlCreateLabel("", $a * 10, $b * 10, 10, 10)
    GUICtrlSetBkColor($snake[0][2], 16777215)
    $pos = ControlGetPos($gui, "", $snake[0][2])
    $snake[0][0] = Int($pos[0] / 10)
    $snake[0][1] = Int($pos[1] / 10)
    EndFunc

    Func move()

    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]

    Select
    Case $newhead[0] < 0 Or $newhead[0] > 39 Or $newhead[1] < 0 Or $newhead[1] > 39
    If $snakelength > 0 Then
    ;
    EndIf


    Case $board[$newhead[0]][$newhead[1]]; OK to go there
    $newhead[2] = GUICtrlCreateLabel("", $newhead[0] * 10, $newhead[1] * 10, 10, 10)
    GUICtrlSetBkColor($newhead[2], 16777215)
    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]
    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]
    Next
    $snake[0][0] = $newhead[0]
    $snake[0][1] = $newhead[1]
    $snake[0][2] = $newhead[2]
    EndSelect
    EndFunc

    [/autoit]
  • Ne immer noch, da ist dann ein Pixel, und sobald ich laufe, werden es zwei D=
    Trotzdem Danke
    lg

    Einmal editiert, zuletzt von Rise Against (18. April 2010 um 21:21)

  • so ist es am Anfang einer, falls du das wolltest

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------

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

    AutoIt Version: 3.3.6.0
    Author: myName

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

    Script Function:
    Template AutoIt script.

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

    #ce ----------------------------------------------------------------------------

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

    ; Script Start - Add your code below here

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

    #include <WindowsConstants.au3>
    #include <GuiConstants.au3>
    #include <Array.au3>
    #include <file.au3>
    #include <StaticConstants.au3>


    $gui = GUICreate("RPG", 400, 400)
    GUISetBkColor(0x000099)

    GUISetState()

    $dll = DllOpen ("user32.dll")
    ;Global $clientsize = WinGetClientSize($gui)
    ;Global $speedder = 150
    ;Global $food
    $snakelength = 0
    Dim $board[40][40]; tracks the board
    Dim $snake [10][10];[$snakelength + 1][3]; positions and parts of the snake
    ;Dim $food_location[2]; not possible for multiple food locations?



    Global $dx = 0
    Global $dy = 1

    _Start ()

    While 1
    Sleep (140)
    GUICtrlDelete ($Snake [1][0])
    If _IsPressed(25) Then ;= Links
    $dx = -1
    $dy = 0
    move ()
    EndIf
    If _IsPressed(27) Then ;= Rechts
    $dx = 1
    $dy = 0
    move ()
    EndIf
    If _IsPressed(26) Then ;= Hoch
    $dx = 0
    $dy = -1
    move ()
    EndIf
    If _IsPressed(28) Then ;= Runter
    $dx = 0
    $dy = 1
    move ()
    EndIf


    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch

    WEnd
    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
    Func _Start ()
    $snakelength = 0
    ;ReDim $snake;[$snakelength + 1][3]
    Local $x, $y, $a, $b, $pos
    For $x = 0 To 39
    For $y = 0 To 39
    $board[$x][$y] = 1; empty
    Next
    Next
    $a = Int(Random(3, 36))
    $b = Int(Random(3, 36))
    $snake[0][2] = GUICtrlCreateLabel("", $a * 10, $b * 10, 10, 10)
    GUICtrlSetBkColor($snake[0][2], 16777215)
    $pos = ControlGetPos($gui, "", $snake[0][2])
    $snake[0][0] = Int($pos[0] / 10)
    $snake[0][1] = Int($pos[1] / 10)
    EndFunc

    Func move()

    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]

    Select
    Case $newhead[0] < 0 Or $newhead[0] > 39 Or $newhead[1] < 0 Or $newhead[1] > 39
    If $snakelength > 0 Then
    ;
    EndIf


    Case $board[$newhead[0]][$newhead[1]]; OK to go there
    $newhead[2] = GUICtrlCreateLabel("", $newhead[0] * 10, $newhead[1] * 10, 10, 10)
    GUICtrlSetBkColor($newhead[2], 16777215)
    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]
    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]
    Next
    $snake[0][0] = $newhead[0]
    $snake[0][1] = $newhead[1]
    $snake[0][2] = $newhead[2]
    EndSelect
    EndFunc

    [/autoit]