Ball abprallen lassen

  • Ich wollte ein kleines Spiel machen. Ich müsste dafür es hinbekommen, einen Ball, der sich die ganze Zeit bewegt, an den Rändern abprallen zu lassen.
    Script:

    Spoiler anzeigen
    [autoit]


    #Region includes
    #include <GDIPlus.au3>
    #include <Misc.au3>
    #EndRegion
    #Region Globale Variablen und Einstellungen
    OnAutoItExitRegister("dispose")
    Opt("GUICloseOnESC", 0)
    Global $xl=25, $yl=70, $xt=70, $yt=25, $xr=460, $yr=70, $xb=70, $yb=460, $score=0, $ballpos[2]=[250, 250], $delaybmove=100, $var=5
    Const $vWidth=10, $vHeight=50, $hWidth=50, $hHeight=10, $ly=20, $lx=20, $lwidth=455, $lheight=455
    #EndRegion
    #Region GDI+
    _GDIPlus_Startup()
    $BlockPen=_GDIPlus_PenCreate(0xFFFFFFFF, 4)
    $BlockBrush=_GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    $LinePen=_GDIPlus_PenCreate(0xFFFF0000, 2)
    $sFormat = _GDIPlus_StringFormatCreate()
    $sBrush = _GDIPlus_BrushCreateSolid(0xFFFFFF00)
    $sFamily = _GDIPlus_FontFamilyCreate("Courier New")
    $sFont = _GDIPlus_FontCreate($sFamily, 10)
    $sLayout = _GDIPlus_RectFCreate(5, 5)
    $BallPen=_GDIPlus_PenCreate(0xFFFFFFFF)
    $BallBrush=_GDIPlus_BrushCreateSolid(0xFFFFFFFF)
    #EndRegion
    While 1
    #Region Hauptmenü
    $sGUI = GUICreate("", 342, 143, -1, -1, 0x80000000+0x00800000)
    GUISetFont(20, 800, 2, "Courier New")
    GUISetBkColor(0x000000)
    $start = GUICtrlCreateLabel("Starten", 104, 8, 116, 35)
    GUICtrlSetColor(-1, 0xFFFF00)
    $credits = GUICtrlCreateLabel("Credits", 104, 52, 116, 35)
    GUICtrlSetColor(-1, 0xFFFF00)
    $exit = GUICtrlCreateLabel("Beenden", 104, 97, 116, 35)
    GUICtrlSetColor(-1, 0xFFFF00)
    GUISetState()
    While 1
    Switch GUIGetMsg()
    Case -3, $exit
    Exit
    Case $start
    GUIDelete($sGUI)
    ExitLoop
    Case $credits
    EndSwitch
    WEnd
    #EndRegion
    #Region GameGUI
    $hGUI = GUICreate("Minigame", 500, 500, -1, -1, 0x80000000+0x00800000)
    GUISetBkColor(0x000000)
    $hGraphics=_GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hBitmap=_GDIPlus_BitmapCreateFromGraphics(500, 500, $hGraphics)
    $hBuffer=_GDIPlus_ImageGetGraphicsContext($hBitmap)
    GUISetState()
    drawblocks()
    drawball()
    _GDIPlus_GraphicsDrawRect($hBuffer, $lx, $ly, $lWidth, $lHeight, $LinePen)
    _GDIPlus_GraphicsDrawStringEx($hBuffer, "Score: "&$score, $sFont, $sLayout, $sFormat, $sBrush)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 500, 500)
    AdlibRegister("moveball", $delaybmove)
    While sleep(1)
    Switch GUIGetMsg()
    Case -3
    ExitLoop
    EndSwitch
    If _IsPressed("1B") Then ExitLoop
    If _IsPressed(25) And $xb>25 Then moveblock(4, "l")
    If _IsPressed(27) And $xb<420 Then moveblock(4, "r")
    If _IsPressed(26) And $yr>25 Then moveblock(3, "u")
    If _IsPressed(28) And $yr<420 Then moveblock(3, "d")
    If _IsPressed(41) And $xt>25 Then moveblock(2, "l")
    If _IsPressed(44) And $xt<420 Then moveblock(2, "r")
    If _IsPressed(57) And $yl>25 Then moveblock(1, "u")
    If _IsPressed(53) And $yl<420 Then moveblock(1, "d")
    If NOT WinActive("Minigame") Then
    AdlibUnRegister("moveball")
    Do
    sleep(100)
    Until WinActive("Minigame")
    drawblocks()
    drawball()
    _GDIPlus_GraphicsDrawRect($hBuffer, $lx, $ly, $lWidth, $lHeight, $LinePen)
    _GDIPlus_GraphicsDrawStringEx($hBuffer, "Score: "&$score, $sFont, $sLayout, $sFormat, $sBrush)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 500, 500)
    AdlibRegister("moveball", $delaybmove)
    EndIf
    WEnd
    #EndRegion
    GUIDelete($hGUI)
    WEnd
    #Region Funktionen
    #Region Zeichnen
    Func drawblocks()
    _GDIPlus_GraphicsClear($hBuffer)
    _GDIPlus_GraphicsDrawRect($hBuffer, $xl, $yl, $vwidth, $vheight, $BlockPen)
    _GDIPlus_GraphicsFillRect($hBuffer, $xl, $yl, $vwidth, $vheight, $BlockBrush)
    _GDIPlus_GraphicsDrawRect($hBuffer, $xt, $yt, $hwidth, $hheight, $BlockPen)
    _GDIPlus_GraphicsFillRect($hBuffer, $xt, $yt, $hwidth, $hheight, $BlockBrush)
    _GDIPlus_GraphicsDrawRect($hBuffer, $xr, $yr, $vwidth, $vheight, $BlockPen)
    _GDIPlus_GraphicsFillRect($hBuffer, $xr, $yr, $vwidth, $vheight, $BlockBrush)
    _GDIPlus_GraphicsDrawRect($hBuffer, $xb, $yb, $hwidth, $hheight, $BlockPen)
    _GDIPlus_GraphicsFillRect($hBuffer, $xb, $yb, $hwidth, $hheight, $BlockBrush)
    EndFunc
    Func drawball()
    _GDIPlus_GraphicsDrawEllipse($hBuffer, $ballpos[0], $ballpos[1], 20, 20, $BallPen)
    _GDIPlus_GraphicsFillEllipse($hBuffer, $ballpos[0], $ballpos[1], 20, 20, $BallBrush)
    EndFunc
    #EndRegion
    #Region Bewegungen
    Func moveblock($block, $direction)
    Switch $block
    Case 1
    If $direction="u" Then $yl-=5
    If $direction="d" Then $yl+=5
    Case 2
    If $direction="l" Then $xt-=5
    If $direction="r" Then $xt+=5
    Case 3
    If $direction="u" Then $yr-=5
    If $direction="d" Then $yr+=5
    Case 4
    If $direction="l" Then $xb-=5
    If $direction="r" Then $xb+=5
    EndSwitch
    drawblocks()
    drawball()
    _GDIPlus_GraphicsDrawRect($hBuffer, $lx, $ly, $lWidth, $lHeight, $LinePen)
    _GDIPlus_GraphicsDrawStringEx($hBuffer, "Score: "&$score, $sFont, $sLayout, $sFormat, $sBrush)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 500, 500)
    _GDIPlus_GraphicsClear($hBuffer)
    EndFunc
    Func moveball()
    If $ballpos[1]=430 Or $ballpos[1]=25 Then $var=$var*(-1)
    $ballpos[0]-=$var
    $ballpos[1]-=$var
    drawblocks()
    drawball()
    _GDIPlus_GraphicsDrawRect($hBuffer, $lx, $ly, $lWidth, $lHeight, $LinePen)
    _GDIPlus_GraphicsDrawStringEx($hBuffer, "Score: "&$score, $sFont, $sLayout, $sFormat, $sBrush)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 500, 500)
    _GDIPlus_GraphicsClear($hBuffer)
    EndFunc
    #EndRegion
    Func dispose()
    _GDIPlus_FontDispose($sFont)
    _GDIPlus_FontFamilyDispose($sFamily)
    _GDIPlus_StringFormatDispose($sFormat)
    _GDIPlus_BrushDispose($sBrush)
    _GDIPlus_BrushDispose($BlockBrush)
    _GDIPlus_BrushDispose($BallBrush)
    _GDIPlus_PenDispose($BallPen)
    _GDIPlus_PenDispose($BlockPen)
    _GDIPlus_PenDispose($LinePen)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_Shutdown()
    EndFunc
    #EndRegion

    [/autoit]


    Ich krieg es einfach nicht hin :(

    2 Mal editiert, zuletzt von Ineluki (22. Juli 2010 um 20:12)

  • [autoit]

    $hgui=guicreate("")
    $buttonball=guictrlcreatebutton("",100,10,20,20)
    ;$buttonschlaeger=guictrlcreatebutton("<-n m->",200,300,100,20)
    guisetstate()

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

    $size=wingetclientsize($hgui) ;ränder

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

    $xstep=random(1,5,1) ;zufällige Geschwindigkeit und Winkel
    $ystep=random(1,5,1)
    $x=150 ;startpunkt
    $y=20

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

    Do
    $x+=$xstep
    $y+=$ystep
    if $x<0 or $x>$size[0]-20 then $xstep=-$xstep ;rand berührt
    if $y<0 or $y>$size[1]-20 then $ystep=-$ystep ;rand berührt
    GUICtrlSetPos($buttonball,$x,$y)
    sleep(20)
    until guigetmsg()=-3

    [/autoit]

    viel einfacher gehts nicht....

  • Pong solomode^^ punkte zählen könnt ihr selbst reinbasteln^^

    Spoiler anzeigen
    [autoit]

    #include <Misc.au3>
    #include <StructureConstants.au3>
    $hgui=guicreate("")
    $buttonball=guictrlcreatebutton("",100,10,20,20) ;ball
    $buttonschlaeger=guictrlcreatebutton("<-n m->",200,300,100,20) ;schläger
    guisetstate()

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

    $size=wingetclientsize($hgui) ;ränder

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

    $xstep=random(1,5,1) ;zufällige Geschwindigkeit und Winkel
    $ystep=random(1,5,1)
    $x=150 ;startpunkt
    $y=20

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

    Do
    $x+=$xstep ;schrittweite x
    $y+=$ystep ;schrittweite y
    if $x<0 or $x>$size[0]-20 then $xstep=-$xstep ;rand berührt, richtung umkehren
    if $y<0 or $y>$size[1]-20 then $ystep=-$ystep ;rand berührt, richtung umkehren

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

    $pos=controlgetpos("","",$buttonschlaeger) ;schlägerposition holen
    if _rectcollision($x,$y,$x+20,$y+20,$pos[0],$pos[1],$pos[0]+$pos[2],$pos[1]+$pos[3]) then ;falls ball von schläger berührt
    $xstep=-$xstep ;richtung umkehren
    $ystep=-$ystep ;richtung umkehren
    endif
    if $pos[0]>0 and _ispressed("4E") then GUICtrlSetPos($buttonschlaeger,$pos[0]-2,300) ;n gedrückt nach links
    if $pos[0]+$pos[2]<$size[0] and _ispressed("4D") then GUICtrlSetPos($buttonschlaeger,$pos[0]+2,300);m gedrückt nach rechts

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

    GUICtrlSetPos($buttonball,$x,$y) ;ballposition setzen
    sleep(20)
    until guigetmsg()=-3

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

    Func _RectCollision($Rect1X1, $Rect1Y1, $Rect1X2, $Rect1Y2, $Rect2X1, $Rect2Y1, $Rect2X2, $Rect2Y2)
    ; Prog@ndy
    Local Const $tagRECT = "long;long;long;long"
    Local $1 = DllStructCreate($tagRECT)
    Local $2 = DllStructCreate($tagRECT)
    Local $3 = DllStructCreate($tagRECT)
    DllStructSetData($1, 1, $Rect1X1)
    DllStructSetData($1, 2, $Rect1Y1)
    DllStructSetData($1, 3, $Rect1X2)
    DllStructSetData($1, 4, $Rect1Y2)
    DllStructSetData($2, 1, $Rect2X1)
    DllStructSetData($2, 2, $Rect2Y1)
    DllStructSetData($2, 3, $Rect2X2)
    DllStructSetData($2, 4, $Rect2Y2)
    Local $r = DllCall("user32.dll", "int", "IntersectRect", "ptr", DllStructGetPtr($3), "ptr", DllStructGetPtr($1), "ptr", DllStructGetPtr($2))
    If @error Then Return SetError(1, 0, 0)
    Return $r[0] <> 0
    EndFunc ;==>_RectCollision

    [/autoit]