So hab gestern Abend aus Spaß nen kleines Ping Pong Spiel für zwei Spieler geschrieben. Der Ball fliegt wie gewohnt von der einen zur anderen Seite und beide Spieler müssen versuchen zu verhindern dass er ihre Seite berührt, dies können sie mit einem rechteckigen Schläger welcher den Ball zurückprallen lässt ohne das der Gegner einen Punkte bekommt. Wenn der Ball den Schläger trifft, fliegt er zurück, wobei sich die Flugrichtung (in diesem Fall unabhängig der vorherigen Richtung) je nach Aufprallpunkt auf dem Schläger ändert. Dies ist zwar bei einem rechteckigem Schläger eigentlich nicht der Fall, aber die richtung würde sonst immer gleich bleiben , da ich sowieso alle Berechnungen freischnautze gemacht habe und von daher eh keine Stimmt dachte ich, dass ich das einfach mal einfüge.
http://www.imagebanana.com/view/it3450a4/Beispiel.png
Die Steuerung für Spieler 1 ist:
W= Schläger nach oben bewegen
S= Schläger nach unten bewegen
A= Schläger nach vorne bewegen
D= Schläger nach hinten bewegen
Und für Spieler 2:
Up= Schläger nach oben bewegen
Down= Schläger nach unten bewegen
Left= Schläger nach hinten bewegen
Right= Schläger nach vorne bewegen
P=Pause (Die Pause kann mit P oder mit beliebigen gültigen Bewegungstasten beendet werden)
ESC=Beendet das Spiel
Script
;----------------------
Local $fFullScreen = true
Local $iWidth = 600
Local $iHight = 400
Local $iPunkte = 10
;----------------------
#include<GDIPlus.au3>
#include<Misc.au3>
$u32dll = DllOpen("user32.dll")
_GDIPlus_Startup()
If $fFullScreen = true Then
$hGUI = GUICreate("hGUI",@DesktopWidth,@DesktopHeight,0,0,0x80000000)
Else
$hGUI = GUICreate("hGUI",$iWidth,$iHight)
EndIf
$aSize = WinGetClientSize($hGUI)
$hGrafik = _GDIPlus_GraphicsCreateFromHWND($hGUI)
_GDIPlus_GraphicsSetSmoothingMode($hGrafik,2)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($aSize[0],$aSize[1],$hGrafik)
$hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hContext,2)
$hPen = _GDIPlus_PenCreate(0xFF00FF00,1)
$hSPen = _GDIPlus_PenCreate(0xFF00FFFF,2)
$hBrush = _GDIPlus_BrushCreateSolid(0xFF55FF88)
$hBPen = _GDIPlus_PenCreate(0x99FF5555,1)
$hStringBrush = _GDIPlus_BrushCreateSolid (0x70997766)
$hFormat = _GDIPlus_StringFormatCreate ()
$hFamily = _GDIPlus_FontFamilyCreate ("Arial")
$hFont = _GDIPlus_FontCreate ($hFamily, 15, 2)
$hFont2 = _GDIPlus_FontCreate ($hFamily, 20, 2)
$tLayout = _GDIPlus_RectFCreate (10, 10)
$tLayout2 = _GDIPlus_RectFCreate ($aSize[0]/2-20, $aSize[1]/2-20)
GUISetState(1,$hGUI)
Local $aBall[6] = [50,0,50, (Random(0,1,1)*2-1)*30,Random(5,30,1),Random(-10,10,1)]
Local $aPlayer1[2] = [50,50]
Local $aPlayer2[2] = [50,50]
Local $aPunkte[2] = [0,0]
Local $iTimer = TimerInit()
Local $iTimeStep,$iLastTimer = 0,$iDiff=0,$iPause = 0
Local $iW = $aSize[0]/100
Local $iH = $aSize[1]/100
Local $iBX = 0, $BY = 0
Local $iSpeed = 2
Local $fPause = True
Do
_GDIPlus_GraphicsClear($hContext,0xFF101020)
$iDiff = TimerDiff($iTimer)-$iPause
$iStep = ($iDiff - $iLastTimer)/1000*$iSpeed
$iSpeed += ($iDiff - $iLastTimer)/5000
$iLastTimer = $iDiff
$aBall[0] += $iStep*$aBall[3]
$aBall[1] += $iStep*$aBall[4]
$aBall[2] += $iStep*$aBall[5]
If $aBall[1] < 0 Then
$aBall[1] = 0
$aBall[4] = -$aBall[4]
EndIf
If $aBall[1] > 100 Then
$aBall[1] = 100
$aBall[4] = -$aBall[4]
EndIf
If $aBall[2] < 0 Then
$aBall[2] = 0
$aBall[5] = -$aBall[5]
EndIf
If $aBall[2] > 100 Then
$aBall[2] = 100
$aBall[5] = -$aBall[5]
EndIf
If $aBall[0] < 0 Then
If $aBall[1] > $aPlayer1[0] + 15 or $aBall[1] < $aPlayer1[0] - 15 or $aBall[2] > $aPlayer1[1] + 15 or $aBall[2] < $aPlayer1[1] - 15 Then
$aPunkte[1] +=1
$iSpeed = 2
If $aPunkte[1] >= $iPunkte Then exitloop
Else
$aBall[4] = ( $aBall[1] - $aPlayer1[0] )
$aBall[5] = ( $aBall[2] - $aPlayer1[1] )
EndIf
$aBall[0] = 0
$aBall[3] = -$aBall[3]
EndIf
If $aBall[0] > 100 Then
If $aBall[1] > $aPlayer2[0] + 15 or $aBall[1] < $aPlayer2[0] - 15 or $aBall[2] > $aPlayer2[1] + 15 or $aBall[2] < $aPlayer2[1] - 15 Then
$aPunkte[0] +=1
$iSpeed = 2
If $aPunkte[0] >= $iPunkte Then exitloop
Else
$aBall[4] = ( $aBall[1] - $aPlayer2[0] )
$aBall[5] = ( $aBall[2] - $aPlayer2[1] )
EndIf
$aBall[0] = 100
$aBall[3] = -$aBall[3]
EndIf
$iBX = (50-$aBall[0])/10
$iBY = (50-$aBall[1])/10
$iBZ = (50-$aBall[2])/3+10
$aLOH = Get3dPoint(0+$iBX, 0+$iBY, 0+$iBZ, 100, 100) ; links oben hinten
$aLOV = Get3dPoint(0+$iBX, 0+$iBY, 100+$iBZ, 100, 100) ; links oben vorne
$aLUH = Get3dPoint(0+$iBX, 100+$iBY, 0+$iBZ, 100, 100) ; links unten hinten
$aLUV = Get3dPoint(0+$iBX, 100+$iBY, 100+$iBZ, 100, 100) ; links unten vorne
$aROH = Get3dPoint(100+$iBX, 0+$iBY, 0+$iBZ, 100, 100) ; rechts oben hinten
$aROV = Get3dPoint(100+$iBX, 0+$iBY, 100+$iBZ, 100, 100) ; rechts oben vorne
$aRUH = Get3dPoint(100+$iBX, 100+$iBY, 0+$iBZ, 100, 100) ; rechts unten hinten
$aRUV = Get3dPoint(100+$iBX, 100+$iBY, 100+$iBZ, 100, 100) ; rechts unten vorne
_GDIPlus_GraphicsDrawLine($hContext,$aLOH[0]*$iW,$aLOH[1]*$iH,$aLOV[0]*$iW,$aLOV[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aLOH[0]*$iW,$aLOH[1]*$iH,$aLUH[0]*$iW,$aLUH[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aLOV[0]*$iW,$aLOV[1]*$iH,$aLUV[0]*$iW,$aLUV[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aLUH[0]*$iW,$aLUH[1]*$iH,$aLUV[0]*$iW,$aLUV[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aROH[0]*$iW,$aROH[1]*$iH,$aROV[0]*$iW,$aROV[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aROH[0]*$iW,$aROH[1]*$iH,$aRUH[0]*$iW,$aRUH[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aROV[0]*$iW,$aROV[1]*$iH,$aRUV[0]*$iW,$aRUV[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aRUH[0]*$iW,$aRUH[1]*$iH,$aRUV[0]*$iW,$aRUV[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aLOH[0]*$iW,$aLOH[1]*$iH,$aROH[0]*$iW,$aROH[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aLOV[0]*$iW,$aLOV[1]*$iH,$aROV[0]*$iW,$aROV[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aLUH[0]*$iW,$aLUH[1]*$iH,$aRUH[0]*$iW,$aRUH[1]*$iH,$hPen)
_GDIPlus_GraphicsDrawLine($hContext,$aLUV[0]*$iW,$aLUV[1]*$iH,$aRUV[0]*$iW,$aRUV[1]*$iH,$hPen)
$aLOH = Get3dPoint(0+$iBX, $aPlayer1[0]+$iBY-15, $aPlayer1[1]+$iBZ-15, 100, 100) ; links oben hinten
$aLOV = Get3dPoint(0+$iBX, $aPlayer1[0]+$iBY-15, $aPlayer1[1]+$iBZ+15, 100, 100) ; links oben vorne
$aLUH = Get3dPoint(0+$iBX, $aPlayer1[0]+$iBY+15, $aPlayer1[1]+$iBZ-15, 100, 100) ; links oben hinten
$aLUV = Get3dPoint(0+$iBX, $aPlayer1[0]+$iBY+15, $aPlayer1[1]+$iBZ+15, 100, 100) ; links oben vorne
$aROH = Get3dPoint(100+$iBX, $aPlayer2[0]+$iBY-15, $aPlayer2[1]+$iBZ-15, 100, 100) ; links oben hinten
$aROV = Get3dPoint(100+$iBX, $aPlayer2[0]+$iBY-15, $aPlayer2[1]+$iBZ+15, 100, 100) ; links oben vorne
$aRUH = Get3dPoint(100+$iBX, $aPlayer2[0]+$iBY+15, $aPlayer2[1]+$iBZ-15, 100, 100) ; links oben hinten
$aRUV = Get3dPoint(100+$iBX, $aPlayer2[0]+$iBY+15, $aPlayer2[1]+$iBZ+15, 100, 100) ; links oben vorne
Local $aPoints[5][2] = [[4]]
$aPoints[1][0] = $aLOH[0]*$iW
$aPoints[1][1] = $aLOH[1]*$iH
$aPoints[2][0] = $aLOV[0]*$iW
$aPoints[2][1] = $aLOV[1]*$iH
$aPoints[3][0] = $aLUV[0]*$iW
$aPoints[3][1] = $aLUV[1]*$iH
$aPoints[4][0] = $aLUH[0]*$iW
$aPoints[4][1] = $aLUH[1]*$iH
_GDIPlus_GraphicsDrawPolygon($hContext,$aPoints,$hSPen)
$aPoints[1][0] = $aROH[0]*$iW
$aPoints[1][1] = $aROH[1]*$iH
$aPoints[2][0] = $aROV[0]*$iW
$aPoints[2][1] = $aROV[1]*$iH
$aPoints[3][0] = $aRUV[0]*$iW
$aPoints[3][1] = $aRUV[1]*$iH
$aPoints[4][0] = $aRUH[0]*$iW
$aPoints[4][1] = $aRUH[1]*$iH
_GDIPlus_GraphicsDrawPolygon($hContext,$aPoints,$hSPen)
$aBallPos = Get3dPoint($aBall[0]+$iBX, $aBall[1]+$iBY, $aBall[2]+$iBZ, 100, 100)
$aBallPos2 = Get3dPoint($aBall[0]+$iBX-3, $aBall[1]+$iBY-3, $aBall[2]+$iBZ-3, 100, 100)
$aBallL = Get3dPoint(0+$iBX, $aBall[1]+$iBY, $aBall[2]+$iBZ, 100, 100)
$aBallL2 = Get3dPoint(0+$iBX, $aBall[1]+$iBY-3, $aBall[2]+$iBZ-3, 100, 100)
$aBallR = Get3dPoint(100+$iBX, $aBall[1]+$iBY, $aBall[2]+$iBZ, 100, 100)
$aBallR2 = Get3dPoint(100+$iBX, $aBall[1]+$iBY-3, $aBall[2]+$iBZ-3, 100, 100)
_GDIPlus_GraphicsDrawEllipse($hContext,$aBallL[0]*$iW,$aBallL[1]*$iH,($aBallL[0]-$aBallL2[0])*$iW,($aBallL[1]-$aBallL2[1])*$iH,$hBPen)
_GDIPlus_GraphicsDrawEllipse($hContext,$aBallR[0]*$iW,$aBallR[1]*$iH,($aBallR[0]-$aBallR2[0])*$iW,($aBallR[1]-$aBallR2[1])*$iH,$hBPen)
_GDIPlus_GraphicsFillEllipse($hContext,$aBallPos[0]*$iW,$aBallPos[1]*$iH,($aBallPos[0]-$aBallPos2[0])*$iW,($aBallPos[1]-$aBallPos2[1])*$iH,$hBrush)
_GDIPlus_GraphicsDrawStringEx ($hContext, "Spieler1: "&$aPunkte[0]&@crlf&"Spieler2: " &$aPunkte[1], $hFont, $tLayout, $hFormat, $hStringBrush)
If _IsPressed(26,$u32dll) and $aPlayer2[0] > 15 Then
$aPlayer2[0] -= $iStep*50
If $aPlayer2[0] < 15 Then $aPlayer2[0] = 15
EndIf
If _IsPressed(28,$u32dll) and $aPlayer2[0] < 85 Then
$aPlayer2[0] += $iStep*50
If $aPlayer2[0] > 85 Then $aPlayer2[0] = 85
EndIf
If _IsPressed(25,$u32dll) and $aPlayer2[1] < 85 Then
$aPlayer2[1] += $iStep*50
If $aPlayer2[1] > 85 Then $aPlayer2[1] = 85
EndIf
If _IsPressed(27,$u32dll) and $aPlayer2[1] > 15 Then
$aPlayer2[1] -= $iStep*50
If $aPlayer2[1] < 15 Then $aPlayer2[1] = 15
EndIf
If _IsPressed(57,$u32dll) and $aPlayer1[0] > 15 Then
$aPlayer1[0] -= $iStep*50
If $aPlayer1[0] < 15 Then $aPlayer1[0] = 15
EndIf
If _IsPressed(53,$u32dll) and $aPlayer1[0] < 85 Then
$aPlayer1[0] += $iStep*50
If $aPlayer1[0] > 85 Then $aPlayer1[0] = 85
EndIf
If _IsPressed(44,$u32dll) and $aPlayer1[1] < 85 Then
$aPlayer1[1] += $iStep*50
If $aPlayer1[1] > 85 Then $aPlayer1[1] = 85
EndIf
If _IsPressed(41,$u32dll) and $aPlayer1[1] > 15 Then
$aPlayer1[1] -= $iStep*50
If $aPlayer1[1] < 15 Then $aPlayer1[1] = 15
EndIf
If _IsPressed("1B",$u32dll) Then ExitLoop
_GDIPlus_GraphicsDrawImage($hGrafik,$hBitmap,0,0)
If $fPause or _IsPressed("50",$u32dll) Then
$iPauseTimer = TimerInit()
$fPause = False
While _IsPressed("50",$u32dll)
sleep(50)
Wend
_GDIPlus_GraphicsDrawStringEx ($hContext, "Pause", $hFont2, $tLayout2, $hFormat, $hStringBrush)
_GDIPlus_GraphicsDrawImage($hGrafik,$hBitmap,0,0)
Do
sleep(25)
If _IsPressed("1B",$u32dll) Then exitloop 2
If _IsPressed(57,$u32dll) Then exitloop
If _IsPressed(53,$u32dll) Then exitloop
If _IsPressed(44,$u32dll) Then exitloop
If _IsPressed(41,$u32dll) Then exitloop
If _IsPressed(25,$u32dll) Then exitloop
If _IsPressed(26,$u32dll) Then exitloop
If _IsPressed(27,$u32dll) Then exitloop
If _IsPressed(28,$u32dll) Then exitloop
Until _IsPressed("50",$u32dll)
While _IsPressed("50",$u32dll)
sleep(25)
WEnd
$iPause += TimerDiff($iPauseTimer)
EndIf
Until 0
If $aPunkte[0] >= $iPunkte Then msgbox(0,"","Spieler 1 Gewinnt")
If $aPunkte[1] >= $iPunkte Then msgbox(0,"","Spieler 2 Gewinnt")
_GDIPlus_FontDispose ($hFont)
_GDIPlus_FontDispose ($hFont2)
_GDIPlus_FontFamilyDispose ($hFamily)
_GDIPlus_StringFormatDispose ($hFormat)
_GDIPlus_BrushDispose ($hStringBrush)
_GDIPlus_PenDispose($hBPen)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_PenDispose($hSPen)
_GDIPlus_PenDispose($hPen)
_GDIPlus_GraphicsDispose($hContext)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_GraphicsDispose($hGrafik)
_GDIPlus_ShutDown ()
DllClose($u32dll)
[/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]Func Get3dPoint($iX,$iY,$iZ,$iWidth,$iHight)
Local $iDiff = 100/($iZ+101)
Local $aReturn[2] = [$iWidth/2+($iX-$iWidth/2)*$iDiff,$iHight/2+($iY-$iHight/2)*$iDiff]
Return $aReturn
EndFunc
PS: Wird es mit Chrome jemals möglich sein vernünftig formatierte Beiträge zu verfassen? Ich muss jedesmal den Browser wechseln, hat jemand da mal ne Lösung gefunden?
€: danke Christoph54 klappt super