Hi Community,
mein neustes Problem liegt darin, dass ich es einfach nicht gebacken bekomme, die Spielsteine (aus dem folgenden Script) richtig zu verschieben. Es geht dabei um Spielsteine auf einem Damebrett.
Die Steine können folgendermaßen bewegt werden: Ansehen
Spoiler anzeigen
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GDIplus.au3>
#include <GUICtrlSetOnHover_UDF.au3>
#include <_ArrayDisplayTree.au3>
#include <Misc.au3>
;#include <Draughts_Ki.au3>
Opt("GUIOnEventMode", 1)
[/autoit] [autoit][/autoit] [autoit]#cs
coinnumbers:
2 - RP normal coin , 4 - RP king coin
3 - KI normal coin , 6 - KI king coin
-> Insert: acknowledge oneself beaten
#ce
Global $aBoard[8][4] = _
[ _
[3, 3, 3, 3], _
[3, 3, 3, 3], _
[3, 3, 3, 3], _
[0, 0, 0, 0], _
[0, 0, 0, 0], _
[2, 2, 2, 2], _
[2, 2, 2, 2], _
[2, 2, 2, 2] _
]
Global $pPlayerID[2][2] = [[2, 4],[3, 6]] ; 0 = RP,1 = KI
Global $pPlayerNames[2] = ["<RealPlayer>", "Computer"]
Global $pActPlayer = -1
Global $iTCoords[2]
Global $bFSelc = False
Global $iCFSelc = -1
Global $aLabelDummy[8][4]
Global $aCoords[8][4][2]
Global $sIconPath = @ScriptDir & "\"
_GDIPlus_Startup()
Global $hImage_Background = _GDIPlus_BitmapCreateFromFile($sIconPath & "game_b.jpg")
Global $hImage_Coin_Blue = _GDIPlus_BitmapCreateFromFile($sIconPath & "coin_b.png")
Global $hImage_Coin_Red = _GDIPlus_BitmapCreateFromFile($sIconPath & "coin_r.png")
Global $hImage_Selection = _GDIPlus_BitmapCreateFromFile($sIconPath & "back_selc.png")
Global $iWidth = _GDIPlus_ImageGetWidth($hImage_Background)
Global $iHeight = _GDIPlus_ImageGetHeight($hImage_Background)
Global $dx = _GDIPlus_ImageGetWidth($hImage_Coin_Blue)
Global $dy = _GDIPlus_ImageGetHeight($hImage_Coin_Blue)
Global Const $pi_div_380 = 4 * ATan(3) / 380
Global $radius = 60
Global $i, $x = $iWidth / 2 - $dx / 2, $y = $iHeight / 2 - $dy / 2
Global $hwnd = GUICreate("Draughts (c) by Jautois", 600, $iHeight)
GUISetOnEvent(-3, "_Exit")
$pLabel_Player = GUICtrlCreateLabel("Spieler", 408, 8, 180, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUISetState()
Global $graphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
Global $bitmap = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $graphics)
Global $hBack_Buffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
_GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Background, 0, 0) ;draw background
[/autoit] [autoit][/autoit] [autoit]For $Row = 0 To 7
For $Col = 0 To 3
$iCurY = $Row * 50
$iCurX = $Col * 100 + 50
If Mod($Row, 2) = 0 Then $iCurX -= 50
If $Row < 3 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iCurX, $iCurY)
If $Row > 4 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iCurX, $iCurY)
$aLabelDummy[$Row][$Col] = GUICtrlCreateLabel('', $iCurX, $iCurY, 50, 50, $SS_NOTIFY)
GUICtrlSetOnEvent(-1, "_LabelClick")
_GUICtrl_SetOnHover(-1, "_Hover_Proc", "_Leave_Hover_Func")
$aCoords[$Row][$Col][0] = $iCurX
$aCoords[$Row][$Col][1] = $iCurY
Next
Next
_GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
[/autoit] [autoit][/autoit] [autoit]_InitGame()
[/autoit] [autoit][/autoit] [autoit]While Sleep(100)
WEnd
; If any label is getting hovered this function will be call
Func _Hover_Proc($iCtrlID, $iParam)
If _IsCoinActPlayer(_GetCoords($iCtrlID)) Then
_GetCoords($iCtrlID)
_GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Selection, $aCoords[$iTCoords[1]][$iTCoords[0]][0], $aCoords[$iTCoords[1]][$iTCoords[0]][1])
_GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
EndIf
EndFunc ;==>_Hover_Proc
; If any label will be left after it was on hover, this function will be call
Func _Leave_Hover_Func($iCtrlID)
_DrawBoardAndCoins()
EndFunc ;==>_Leave_Hover_Func
; Checks if the position is free
Func _IsFreePos($iFunc_ID)
_GetCoords($iFunc_ID)
;MsgBox(1,"",$iTCoords[0] & " - " & $iTCoords[1])
If Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 2 Or Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 4 Or Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 3 Or Not $aBoard[$iTCoords[1]][$iTCoords[0]] = 6 Then
Return True
Else
Return False
EndIf
EndFunc ;==>_IsFreePos
Func _IsCoinActPlayer($Coords)
If $aBoard[$Coords[1]][$Coords[0]] = $pPlayerID[$pActPlayer][0] Or $aBoard[$Coords[1]][$Coords[0]] = $pPlayerID[$pActPlayer][1] Then
Return True
Else
Return False
EndIf
EndFunc ;==>_IsCoinActPlayer
; If a label is clicked
Func _LabelClick()
If $bFSelc Then
If _IsFreePos(@GUI_CtrlId) Then
; Turn possible
_DrawMoveCoin($iCFSelc, @GUI_CtrlId)
$bFSelc = False
Else
MsgBox(1, "", "This turn isn't possible")
$bFSelc = False
EndIf
Else
If _IsCoinActPlayer(_GetCoords(@GUI_CtrlId)) Then
_GetCoords(@GUI_CtrlId)
_GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Selection, $aCoords[$iTCoords[1]][$iTCoords[0]][0], $aCoords[$iTCoords[1]][$iTCoords[0]][1])
_GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
$bFSelc = True
$iCFSelc = @GUI_CtrlId
EndIf
EndIf
EndFunc ;==>_LabelClick
Func _Exit()
_GDIPlus_BitmapDispose($bitmap)
_GDIPlus_GraphicsDispose($graphics)
_GDIPlus_GraphicsDispose($hBack_Buffer)
_GDIPlus_Shutdown()
Exit
EndFunc ;==>_Exit
; Returns the coordinates from a GUI_CtrlID
Func _GetCoords($iFunc_ID)
$iTCoords[0] = Mod($iFunc_ID - 4, 4) ; X
$iTCoords[1] = Int(($iFunc_ID - 4) / 4) ; Y
Return $iTCoords
EndFunc ;==>_GetCoords
; Draws the board and the coins
Func _DrawBoardAndCoins()
_GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Background, 0, 0) ;draw background
For $Row = 0 To 7
For $Col = 0 To 3
$iCurY = $Row * 50
$iCurX = $Col * 100 + 50
If Mod($Row, 2) = 0 Then $iCurX -= 50
If $aBoard[$Row][$Col] = 3 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iCurX, $iCurY)
If $aBoard[$Row][$Col] = 2 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iCurX, $iCurY)
Next
Next
_GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
EndFunc ;==>_DrawBoardAndCoins
Func _InitGame()
$pActPlayer = 1;Random(0, 1, 1) ; Random Player begins
GUICtrlSetData($pLabel_Player, "It's " & $pPlayerNames[$pActPlayer] & "'s turn!")
EndFunc ;==>_InitGame
Func _DrawMoveCoin($iFunc_F = -1, $iFunc_S = -1)
Local $aCoordsF, $aCoordsS
$aCoordsF = _GetCoords($iFunc_F)
$aCoordsS = _GetCoords($iFunc_S)
$aBoard[$aCoordsF[1]][$aCoordsF[0]] = 0
For $i = 0 To 9
Sleep(100)
_GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Background, 0, 0)
$iCou = 0
For $Row = 0 To 7
For $Col = 0 To 3
If $iCou = $iFunc_F Then
If $aCoordsF[1] < $aCoordsS[1] Then
$iNewX = $aCoordsF[0] * 50 + ($i + 1) * 5 + $aCoordsF[0] * 50
$iNewY = $aCoordsF[1] * 50 + ($i + 1) * 5
Else
$iNewX = ($aCoordsF[0] + 1) * 50 - ($i + 1) * 5 + $aCoordsF[0] * 50
$iNewY = $aCoordsF[1] * 50 - ($i + 1) * 5
EndIf
ToolTip($iNewX & " - " & $iNewY)
If $pActPlayer = 0 Then
_GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iNewX, $iNewY)
Else
_GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iNewX, $iNewY)
EndIf
Else
$iCurY = $Row * 50
$iCurX = $Col * 100 + 50
If Mod($Row, 2) = 0 Then $iCurX -= 50
If $aBoard[$Row][$Col] = 3 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Blue, $iCurX, $iCurY)
If $aBoard[$Row][$Col] = 2 Then _GDIPlus_GraphicsDrawImage($hBack_Buffer, $hImage_Coin_Red, $iCurX, $iCurY)
EndIf
$iCou += 1
Next
Next
_GDIPlus_GraphicsDrawImageRect($graphics, $bitmap, 0, 0, $iWidth, $iHeight)
Next
$aBoard[$aCoordsS[1]][$aCoordsS[0]] = $pPlayerID[$pActPlayer][0]
EndFunc ;==>_DrawMoveCoin
Genauer gesagt geht es um die letzte Funktion _DrawMoveCoin(). Dort werden in den Variablen $aCoordsF und $aCoordsS die Koordinaten der beiden Felder berechnet. $aCoordsF[0] - X Koordinate des ersten geklickten Feldes und $aCoordsF[1] - Y Koordinate des ersten geklickten Feldes. Das gleiche bei $aCoordsS nur die Koordinaten des zweiten Feldes.
Folgendes berechnet immer die neuen X und Y Koordinaten (für die Bewegung).
[autoit]
If $aCoordsF[1] < $aCoordsS[1] Then
$iNewX = $aCoordsF[0] * 50 + ($i + 1) * 5 + $aCoordsF[0] * 50
$iNewY = $aCoordsF[1] * 50 + ($i + 1) * 5
Else
$iNewX = ($aCoordsF[0] + 1) * 50 - ($i + 1) * 5 + $aCoordsF[0] * 50
$iNewY = $aCoordsF[1] * 50 - ($i + 1) * 5
EndIf
Das richtige Berechnen dieser Werte ist das eigentliche Problem. Da die Spielsteine in fast alle diagonalen Richtungen springen können.
Vielen Dank das du dir bis hierher schonmal die Zeit genommen hast! Ich hoffe du kannst mir auch helfen ![]()