Snake...

  • Hey Leute

    wie einige vielleicht schon gemerkt haben versuche ich gerade GDI+ zu lernen.

    Ich wollte mal Snake nachprogrammieren, aber jetzt weiß ich nicht mehr weiter. Wie bekomme ich es hin, dass wenn die Schlange um die Ecke läuft, dass dann die Wurst nen knick bekommt?

    Ja ich hab die SuFu nach Snake abgesucht aber alle GDI+ sachen waren mir einfach viel zu umfangreich und deshalb hab ich nix kapiert -.-

    Spoiler anzeigen
    [autoit]

    #include <GuiConstants.au3>
    #include <GDIPlus.au3>
    #include <Misc.au3>

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

    OnAutoItExitRegister("_EXIT")

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

    $x = 100
    $y = 100
    $score = 1
    $width = 10

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

    $kx = 300
    $ky = 400
    $direction = "down"

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

    $Gui = GUICreate("Snake",500,500,100,100)

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

    _GDIPlus_Startup()
    $brushSchwarz = _GDIPlus_BrushCreateSolid(0xFF000000)
    $brushWeiss = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $brushGruen = _GDIPlus_BrushCreateSolid(0xFF60CC3F)

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

    $graphic = _GDIPlus_GraphicsCreateFromHWND($Gui)
    $bitmap = _GDIPlus_BitmapCreateFromGraphics(500, 500, $graphic)
    $buffer = _GDIPlus_ImageGetGraphicsContext($bitmap)

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

    GUISetState()

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

    Do
    $msg = GUIGetMsg()

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

    Select
    Case _IsPressed(25) ;links
    $direction = "left"
    Case _IsPressed(26) ;oben
    $direction = "up"
    Case _IsPressed(27) ;rechts
    $direction = "right"
    Case _IsPressed(28) ;unten
    $direction = "down"
    EndSelect

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

    If $direction == "left" And $x >= 0 Then $x -= 2
    If $direction == "up" And $y >= 0 Then $y -= 2
    If $direction == "right" And $x <= 500 Then $x += 2
    If $direction == "down" And $y <= 500 Then $y += 2

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

    _GDIPlus_GraphicsFillRect($buffer, 0, 0, 500, 500, $brushWeiss)
    _GDIPlus_GraphicsFillRect($buffer, $x, $y, $width, 10, $brushSchwarz)
    _GDIPlus_GraphicsFillRect($buffer, $kx, $ky, 10, 10, $brushGruen)
    _GDIPlus_GraphicsDrawImageRect($graphic, $bitmap, 0, 0, 500, 500)

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

    Select
    Case $x >= ($kx - 5) And $x <= ($kx + 5) And $y >= ($ky - 5) And $y <= ($ky + 5)
    $score += 1
    $kx = Random(1,490,1)
    $ky = Random(1,490,1)
    $width += 10
    EndSelect

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

    Until $msg = $GUI_EVENT_CLOSE

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

    Func _EXIT()
    _GDIPlus_Shutdown()
    EndFunc

    [/autoit]
  • Um deiner Bitte in name22 treads zu folgen, ich helfe dir gerne :)
    Also, du schreibst die Koordinaten des ersten Quadrats, also des Kopfes in ein Array, das Quadrat dahinter übernimmt dann, bevor das erste Array wieder überschrieben wird, die Koordinaten des Kopfes und der Kopf rückt eins weiter. So machst du es mit jedem Teil des Schlange, also Koordinaten des Kopfes und von dem Ganzen Schwanz.

    Beispiel:

    Array erstellen:

    [autoit]

    Dim $aArray[10][2]

    [/autoit]

    10 1D Indexe für 10 Teile, also Kopf + 9 Schwanzteile. 2 2D Indexe für X Position und Y Position.

    Nun musst du erstmal die Kopfkoordinaten setzen

    [autoit]

    $aArray[0][0] = 0
    $aArray[0][1] = 0

    [/autoit]

    Ich setz sie einfach mal auf 0 und 0.

    Jetzt kommt das aufrücken

    [autoit]

    For $i = Ubound($aArray) -1 to 1 Step -1
    $aArray[$i][0] = $aArray[$i-1][0]
    $aArray[$i][1] = $aArray[$i-1][1]
    Next

    [/autoit]

    Und dann immer wie gewohnt aufzeichnen :)

    [autoit]

    For $i = 0 to Ubound ($aArray) -1
    _gdiplus_graphicsfillrect($hBackbuffer, $aArray[$i][0], $aArray[$i][1], 10, 10)
    Next

    [/autoit]

    Hoffe es konnte dir helfen :)