Quote:
Originally Posted by WiiZocker
Ok, es ist schomn gelöst, also das ursprüngliche, aber da ich nicht noch nen Thread posten will, frag ich hier nochmal^^
Hier mal Zittiert was auf der 1sten Seite geschrieben steht.
|
Das ist einen ganz simple Collisionsfunktion:
PHP Code:
Func Collision($x1,$y1,$width1,$high1,$x2,$y2,$width2,$high2)
$hpos1=$y1+$high1
$wpos1=$x1+$width1
$hpos2=$y2+$high2
$wpos2=$x2+$width2
if (($hpos1>=$y2 and $y1<$y2) or ($hpos2>=$y1 and $y1>$y2)) Then
if (($wpos1>=$x2 and $x1<$x2) or ($wpos2>=$x1 and $x1>$x2)) then
return true
else
return false
endif
else
return false
endif
EndFunc
Und so kannst du sie verwenden:
PHP Code:
;Neues Fenster öffnet sich:
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
HotKeySet("{Left}","SpielerNachLinks")
HotKeySet("{Right}","SpielerNachRechts")
#region Constants
; Diese Werte kann man aus dem Bild direkt auslesen
; Sagen wir also dein Bild Ball.bmp ist 22 Pixel hoch und 30 breit
; Dann musst du auch dementsprechend das machen:
; $Width_Ball=30
; $High_Ball=22
Const $Width_Spieler=164
Const $Width_Ball=32
Const $Width_Gegner=64
Const $High_Spieler=128
Const $High_Ball=32
Const $High_Gegner=128
#endregion Constants
$X_BallPos = 206
$Y_BallPos = 308
$X_Spieler = 160
$Y_Spieler = 592
$X_Gegner = 160
$Y_Gegner = 16
#Region ### START Koda GUI section ### Form=C:\Users\Jan\Desktop\Pong\Daten\Spielfeld GUI.kxf
$PongGui = GUICreate("Pong By WiiZocker", 436, 638, 257, 164)
$Ball = GUICtrlCreatePic("Daten\Ball.bmp", $X_BallPos, $Y_BallPos, 22, 22, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$Spieler = GUICtrlCreatePic("Daten\Spieler.bmp", $X_Spieler, $Y_Spieler, 121, 23, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$Gegner = GUICtrlCreatePic("Daten\Gegner.bmp", $X_Gegner, $Y_Gegner, 121, 23, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
$Spielfeld = GUICtrlCreatePic("Daten\Spielfeld.bmp", 0, 0, 435, 637, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
if(Collision($X_BallPos,$Y_BallPos,$Width_Ball,$High_Ball,$X_Spieler,$Y_Spieler,$Width_Spieler,$High_Spieler)) Then
MsgBox(0,,"Ball berührt","Ball hat Spieler berührt.")
EndIf
if(Collision($X_BallPos,$Y_BallPos,$Width_Ball,$High_Ball,$X_Gegner,$Y_Gegner,$Width_Gegner,$High_Gegner)) Then
MsgBox(0,,"Ball berührt","Ball hat Gegner berührt.")
EndIf
WEnd
Func SpielerNachLinks()
$X_Spieler = $X_Spieler - 7
Aktuell()
EndFunc
Func SpielerNachRechts()
$X_Spieler = $X_Spieler + 7
Aktuell()
EndFunc
Func Aktuell()
GUICtrlSetPos($Ball,$X_BallPos,$Y_BallPos)
GUICtrlSetPos($Spieler,$X_Spieler,$Y_Spieler)
GUICtrlSetPos($Gegner,$X_Gegner,$Y_Gegner)
EndFunc
Func Collision($x1,$y1,$width1,$high1,$x2,$y2,$width2,$high2)
$hpos1=$y1+$high1
$wpos1=$x1+$width1
$hpos2=$y2+$high2
$wpos2=$x2+$width2
if (($hpos1>=$y2 and $y1<$y2) or ($hpos2>=$y1 and $y1>$y2)) Then
if (($wpos1>=$x2 and $x1<$x2) or ($wpos2>=$x1 and $x1>$x2)) then
return true
else
return false
endif
else
return false
endif
EndFunc
P.S.
Welches Objekt bei der Collisions-Abfrage zuerst kommt ist egal, du kannst also zuerst die Werte vom Ball eintragen und dann die Werte vom Spieler oder andersrum, das spielt keine Rolle.