Ball stoppen

  • Hallo zusammen,

    habe eine Gui, in der ein Objekt ist(in meinem Fall ein Ball) und ein Zweites Objekt(Mauer od. Wand). Den Ball kann man mit den Pfeiltasten steuern.
    Jedoch habe ich jetzt das Problem, dass sich der Ball unter die Mauer schiebt und suche nun eine Möglichkeit um das zu verhindern.
    Hab schon vieles probiert, hat aber wenig geklappt.

    Hier mal mein Quellcode, wobei das nur ein Beispiel ist, um es sich besser vorzustellen :

    Spoiler anzeigen
    [autoit]

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

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

    opt("GUIOnEventMode", 1)

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

    GUICreate("Beispiel", 400, 400)
    $ball = GUICtrlCreateIcon("ball17.ico", -1, 10 ,10)
    GUICtrlCreateIcon("wall.ico", -1 ,35, 35)
    GUISetBkColor(000000)
    GUISetState()

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "Ende")

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

    While 1

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

    If _IsPressed(28) Then
    _pfeiltaste_unten()
    EndIf

    If _IsPressed(26) Then
    _pfeiltaste_oben()
    EndIf

    If _IsPressed(25) Then
    _pfeiltaste_links()
    EndIf

    If _IsPressed(27) Then
    _pfeiltaste_rechts()
    EndIf
    Sleep(10)

    WEnd

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

    Func Ende()
    Exit
    EndFunc

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

    func _pfeiltaste_unten()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[1] <> 370 Then
    GUICtrlSetPos($ball, $pos[0], $pos[1] + 2)
    EndIf

    EndFunc

    Func _pfeiltaste_oben()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[1] <> 0 Then
    GUICtrlSetPos($ball, $pos[0], $pos[1] - 2)
    EndIf
    EndFunc

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

    Func _pfeiltaste_links()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[0] <> 0 Then
    GUICtrlSetPos($ball, $pos[0] - 2, $pos[1])
    EndIf

    EndFunc

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

    Func _pfeiltaste_rechts()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[0] <> 370 Then
    GUICtrlSetPos($ball, $pos[0] + 2, $pos[1])
    EndIf
    EndFunc

    [/autoit]

    und die Icons:
    autoit.de/wcf/attachment/5645/

  • Kann man wohl nur dadurch realisieren, das für die Ball koordinaten die Mauerkoordinaten ausgeblendet werden.
    Also wenn rechte Maustaste gedrückt, dann PosX = PosX + 2
    aber nur wenn dort keine Mauer ist (deine Mauer = 35,35 bis 45, 45)
    demnach darf PosX nur kleiner als 35 und größer als 45 sein wenn PosY >35 und <45.
    u.s.w

    RuffuR

  • Ich würde die Wände in einer einzigartigen Farbe machen zb. Pink und dann mit einem Pixel abstand um den ball herum nach dieser Farbe suchen lassen.

  • Die wohl allereinfachste und sicherste Lösung ist ein Array. Mach ein Großes Array, und benutze 2 Zahlen, z.B. 0 und 1.

    0 = Feld
    1 = Mauer

    Dann unterteilst du dein ganzes Spielfeld in Zahlen.

    Code
    00000000000000000000
    00111100000000000000
    00111100000000000000
    00111100000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000

    Dort wo jetzt die 1 ist wird deine Mauer optisch hingezeichnet, aber der Vergleich läuft im Hintegrund ab. Jetzt nimm ne 3. Zahl, die deinen Spieler darstellt, bzw. den Ball. Und den läst du im Array "wandern" und prüfst jedes mal wenn er sich bewegen soll, ob er eine 1 überschreiben würde. Wenn er das tut verweigerst du den Zug. Eigentlich total simpel. Und du kannst jedes mal wenn sich der Spieler bewegt das ganze Spielfeld aus deinem Array heraus neu zeichnen.


    anno2008

  • Erst einmal Danke für euer Lösungen.

    @Ruffur: So etwas in der Art wie du habe ich mir auch schon gedacht, was aber dann komplizierter wird, je mehr Mauern bzw. andere Objekte ich habe.

    anno2008: Finde deinen Ansatz am besten. Werde es probieren und mich dann melden, falls ich eine Lösung hab :D .

    mfg hulle

  • Hallo,
    der Vorteil am Vorschlag von anno2008 ist auch, daß man dieses System auf so gut wie JEDES Spiel anwenden kann. Auch Schachprogramme und viele andere Strategiespiele lassen sich auf diese einfache Art darstellen.
    ciao
    Andy

  • Möchte gerne etwas testen, aber weshalb kommt hier <> 370? Was hat es mit der 370 auf sich?

    [autoit]

    If $pos[1] <> 370 Then
    GUICtrlSetPos($ball, $pos[0], $pos[1] + 2)

    [/autoit]

    mfG
    Charlie

  • HI!

    Hier meine Version.

    LG
    Concara

    Spoiler anzeigen
    [autoit]


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

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

    opt("GUIOnEventMode", 1)
    Dim $awall[12][12]

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

    If FileExists(@ScriptDir & "\testbsp.ini") Then
    For $ii = 0 To 11
    For $i = 0 To 11
    $awall[$i][$ii] = IniRead ( @ScriptDir & "\testbsp.ini", $i, $ii, 4)
    $ballx = IniRead(@ScriptDir & "\testbsp.ini", "ball", 1, "")
    $bally = IniRead(@ScriptDir & "\testbsp.ini", "ball", 2, "")
    $zielx = IniRead(@ScriptDir & "\testbsp.ini", "ziel", 1, "")
    $ziely = IniRead(@ScriptDir & "\testbsp.ini", "ziel", 2, "")
    Next
    Next
    Else
    $awall[1][11] = 1
    $awall[1][10] = 1
    $awall[1][9] = 1
    $awall[1][8] = 1
    $awall[1][7] = 1
    $awall[1][6] = 1
    $awall[2][5] = 1
    $awall[3][4] = 1
    $awall[4][3] = 1
    $awall[5][2] = 1
    $awall[2][6] = 1
    $awall[3][5] = 1
    $awall[4][4] = 1
    $awall[5][3] = 1
    $awall[5][2] = 1
    $awall[5][1] = 1
    $awall[7][2] = 1
    $awall[7][3] = 1
    $awall[7][4] = 1
    $awall[7][5] = 1
    $awall[7][6] = 1
    $awall[7][1] = 1
    $awall[11][11] = 1
    $awall[11][10] = 1
    $awall[11][9] = 1
    $awall[11][8] = 1
    $awall[11][7] = 1
    $awall[11][6] = 1
    $awall[11][5] = 1
    $awall[10][11] = 1
    $awall[10][10] = 1
    $awall[10][9] = 1
    $awall[10][8] = 1
    $awall[10][7] = 1
    $awall[10][6] = 1
    $awall[9][6] = 1
    $awall[8][11] = 1
    $awall[8][10] = 1
    $awall[8][9] = 1
    $awall[8][8] = 1
    $awall[7][11] = 1
    $awall[7][10] = 1
    $awall[7][9] = 1
    $awall[7][8] = 1
    $awall[6][11] = 1
    $awall[6][10] = 1
    $awall[6][9] = 1
    $awall[6][8] = 1
    $awall[5][11] = 1
    $awall[5][10] = 1
    $awall[5][9] = 1
    $awall[5][8] = 1
    $ballx = 0
    $bally = 0
    $zielx = 9
    $ziely = 11
    $awall[9][11] = 3
    For $i = 0 To 11
    For $ii = 0 To 11
    $data = $awall[$i][$ii]
    IniWrite(@ScriptDir & "\testbsp.ini", $i, $ii, $data)
    Next
    Next
    IniWrite(@ScriptDir & "\testbsp.ini", "ball", 1, $ballx)
    IniWrite(@ScriptDir & "\testbsp.ini", "ball", 2, $bally)
    IniWrite(@ScriptDir & "\testbsp.ini", "ziel", 1, $zielx)
    IniWrite(@ScriptDir & "\testbsp.ini", "ziel", 2, $ziely)
    EndIf

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

    GUICreate("Beispiel", 384, 384)
    $ziel = GUICtrlCreateIcon("ziel.ico", -1, $zielx*32, $ziely*32)
    $ball = GUICtrlCreateIcon("ball17.ico", -1, $ballx*32, $bally*32)

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

    For $i = 0 To 11
    For $ii = 0 To 11
    If $awall[$i][$ii] = 1 Then
    GUICtrlCreateIcon("wall.ico", -1 ,$i*32, $ii*32)
    EndIf
    Next
    Next
    GUISetBkColor(000000)
    GUISetState()

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "Ende")

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

    While 1

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

    If _IsPressed(28) Then
    _pfeiltaste_unten()
    EndIf

    If _IsPressed(26) Then
    _pfeiltaste_oben()
    EndIf

    If _IsPressed(25) Then
    _pfeiltaste_links()
    EndIf

    If _IsPressed(27) Then
    _pfeiltaste_rechts()
    EndIf
    Sleep(10)

    WEnd

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

    Func Ende()
    Exit
    EndFunc

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

    Func _crash($x, $y, $zx1, $zy1, $zx2, $zy2)
    Local $x1 = Int(($x+$zx1)/32)
    Local $y1 = Int(($y+$zy1)/32)
    Local $x2 = Int(($x+$zx2)/32)
    Local $y2 = Int(($y+$zy2)/32)
    ;~ ConsoleWrite ($x1 & " " & $y1 & " " & $x2 & " " & $y2 & @CRLF)
    If $awall[Round($x/32)][Round($y/32)] = 3 Then
    MsgBox(0,"Ziel", "Du bist am Ziel angekommen", 10)
    EndIf
    If $awall[$x1][$y1] = 1 Or $awall[$x2][$y2] = 1 Then
    Return 1
    EndIf
    Return 0
    EndFunc

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

    func _pfeiltaste_unten()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[1] <> 352 Then
    $crash = _crash($pos[0], $pos[1] + 1, 0, 31, 31, 31)
    If Not $crash Then
    GUICtrlSetPos($ball, $pos[0], $pos[1] + 2)
    EndIf
    EndIf
    EndFunc

    Func _pfeiltaste_oben()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[1] <> 0 Then
    $crash = _crash($pos[0], $pos[1] - 1, 0, 0, 31, 0)
    If Not $crash Then
    GUICtrlSetPos($ball, $pos[0], $pos[1] - 2)
    EndIf
    EndIf
    EndFunc

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

    Func _pfeiltaste_links()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[0] <> 0 Then
    $crash = _crash($pos[0] - 1, $pos[1], 0, 0, 0, 31)
    If Not $crash Then
    GUICtrlSetPos($ball, $pos[0] - 2, $pos[1])
    EndIf
    EndIf
    EndFunc

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

    Func _pfeiltaste_rechts()
    $pos = ControlGetPos("Beispiel", "", $ball)
    If $pos[0] <> 352 Then
    $crash = _crash($pos[0] + 1, $pos[1], 31, 0, 31, 31)
    If Not $crash Then
    GUICtrlSetPos($ball, $pos[0] + 2, $pos[1])
    EndIf
    EndIf
    EndFunc

    [/autoit]

    If not :?: then ?( else :thumbup:

  • also Concara richtig gud gemacht nur das erstellen der mauern ist etwas blöd . :P
    ich glaube ich werde mich auch ma dran setzen und bissi skripten an dem spiel :thumbup:

  • Muss auch sagen, dass es Concara gut gemacht hat .
    Werd mich auch mal drüber machen und bei etwas verwertbarem mal den Code posten :D