Skript optimieren UND Kollision mit GDI+

  • Hallo alle zusammen,
    vorerst mein Skript

    Spoiler anzeigen
    [autoit]

    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_UseUpx=n
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #include <Misc.au3>

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

    $Width = 300
    $Height = 460

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

    $JumpSpeed = 0.07
    $JumpHeight = 250
    $MoveSpeed = 10

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

    $GUI1 = GUICreate("Doodle Jump - Beta", $Width, $Height)
    GUISetState(@SW_SHOW)

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

    $pDoodle_Rechts = @ScriptDir&"\Doodle_Rechts.png"
    $pDoodle_Links = @ScriptDir&"\Doodle_Links.png"
    $pBalken_Gruen = @ScriptDir&"\Balken-Grün.png"

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

    _GDIPlus_Startup()
    $Background = _GDIPlus_ImageLoadFromFile (@ScriptDir&"\background.jpg")

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

    $Doodle_Rechts =_GDIPlus_ImageLoadFromFile ($pDoodle_Rechts)
    $Doodle_Links = _GDIPlus_ImageLoadFromFile ($pDoodle_Links)
    $Balken_Gruen = _GDIPlus_ImageLoadFromFile ($pBalken_Gruen)
    $Doodle_Height = _GDIPlus_ImageGetHeight($Doodle_Rechts)
    $Doodle_Width = _GDIPlus_ImageGetWidth ($Doodle_Rechts)
    $AktuellerDoodle = $Doodle_Rechts

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

    $Graphic = _GDIPlus_GraphicsCreateFromHWND($GUI1)
    $Bitmap = _GDIPlus_BitmapCreateFromGraphics($Width, $Height, $Graphic)
    $Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)

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

    $i = 0
    $x = 50

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

    While 1
    If _IsPressed("25") Then
    If $x >= 10 Then
    $x -= $MoveSpeed
    $AktuellerDoodle = $Doodle_Links
    ElseIf $x <= 10 Then
    $x = 0
    $AktuellerDoodle = $Doodle_Links
    EndIf
    ElseIf _IsPressed("27") Then
    If $x <= $Width-$Doodle_Width-15 Then
    $x += $MoveSpeed
    $AktuellerDoodle = $Doodle_Rechts
    ElseIf $x >= $Width-$Doodle_Width-15 Then
    $x = $Width-$Doodle_Width-14
    $AktuellerDoodle = $Doodle_Rechts
    EndIf
    EndIf

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

    $i += $JumpSpeed
    $y = $Height - $Doodle_Height - 5 - Abs(Sin($i))*$JumpHeight

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

    _GDIPlus_GraphicsDrawImage ($Buffer, $Background, 0, 0)

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

    _GDIPlus_GraphicsDrawImage ($Buffer, $AktuellerDoodle, $x, $y)
    _GDIPlus_GraphicsDrawImage ($Buffer, $Balken_Gruen, 100, 230)
    _GDIPlus_GraphicsDrawImageRect($Graphic, $Bitmap, 0, 0, $Width, $Height)
    Sleep(5)

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

    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    _GDIPlus_GraphicsDispose($Graphic)
    _GDIPlus_ImageDispose($Buffer)
    _WinAPI_DeleteObject($Bitmap)
    _GDIPlus_Shutdown()
    Exit
    EndSwitch
    WEnd

    [/autoit]

    Wenn ihr dies ausführt, dann merk ihr das es ruckelt.
    Das liegt höchstwahrscheinlich an dem Zeichnen der Objekte.

    Wie muss ich das Skript umschreiben, damit der Vorgang des Zeichnens verbesser wird?
    Wenn das Spiel irgendwann fertig ist, dann müssen viele neue Plattformen und Monster etc. gezeichnet werden.
    Das könnte auf die Dauer schwierig werden.
    Der Background kann bestimmt auch irgendwie ein einziges mal gezeichnet werden.
    Dann wäre das schonmal gespart.
    Jedoch wird der "Doodle" jedes mal neu gezeichnet und somit würde es eine Spur aus Doodeln hinterlassen.
    Kann man die Doodle Grafik nach dem Zeichnen nicht wieder löschen und dann erst zeichnen?

    Dann zur zweiten Frage:
    Der Balken wird auch per GDI+ eingezeichnet.
    Kann man eine Kollision damit berechnen?
    Hintergrund:
    Der "Doodle" soll auf die Plattform springen und von dort aus dann weiterhüpfen können.

  • Was mir auf Anhieb auffällt ist, schreib mal lieber im OnEventModus.
    Das GUIGetMsg() bremst deine Schleife stark aus, und verursacht das Ruckeln.

    Spoiler anzeigen
    [autoit]


    #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_UseUpx=n
    #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #include <Misc.au3>

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

    Opt("GUIOnEventMode", 1)

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

    $Width = 300
    $Height = 460

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

    $JumpSpeed = 0.07
    $JumpHeight = 250
    $MoveSpeed = 10

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

    $GUI1 = GUICreate("Doodle Jump - Beta", $Width, $Height)
    GUISetState(@SW_SHOW)

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

    $pDoodle_Rechts = @ScriptDir & "\Doodle_Rechts.png"
    $pDoodle_Links = @ScriptDir & "\Doodle_Links.png"
    $pBalken_Gruen = @ScriptDir & "\Balken-Grün.png"

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

    _GDIPlus_Startup()
    $Background = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\background.jpg")

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

    $Doodle_Rechts = _GDIPlus_ImageLoadFromFile($pDoodle_Rechts)
    $Doodle_Links = _GDIPlus_ImageLoadFromFile($pDoodle_Links)
    $Balken_Gruen = _GDIPlus_ImageLoadFromFile($pBalken_Gruen)
    $Doodle_Height = _GDIPlus_ImageGetHeight($Doodle_Rechts)
    $Doodle_Width = _GDIPlus_ImageGetWidth($Doodle_Rechts)
    $AktuellerDoodle = $Doodle_Rechts

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

    $Graphic = _GDIPlus_GraphicsCreateFromHWND($GUI1)
    $Bitmap = _GDIPlus_BitmapCreateFromGraphics($Width, $Height, $Graphic)
    $Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)

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

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

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

    $i = 0
    $x = 50

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

    While Sleep(5)
    If _IsPressed("25") Then
    If $x >= 10 Then
    $x -= $MoveSpeed
    $AktuellerDoodle = $Doodle_Links
    ElseIf $x <= 10 Then
    $x = 0
    $AktuellerDoodle = $Doodle_Links
    EndIf
    ElseIf _IsPressed("27") Then
    If $x <= $Width - $Doodle_Width - 15 Then
    $x += $MoveSpeed
    $AktuellerDoodle = $Doodle_Rechts
    ElseIf $x >= $Width - $Doodle_Width - 15 Then
    $x = $Width - $Doodle_Width - 14
    $AktuellerDoodle = $Doodle_Rechts
    EndIf
    EndIf

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

    $i += $JumpSpeed
    $y = $Height - $Doodle_Height - 5 - Abs(Sin($i)) * $JumpHeight

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

    _GDIPlus_GraphicsDrawImage($Buffer, $Background, 0, 0)

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

    _GDIPlus_GraphicsDrawImage($Buffer, $AktuellerDoodle, $x, $y)
    _GDIPlus_GraphicsDrawImage($Buffer, $Balken_Gruen, 100, 230)
    _GDIPlus_GraphicsDrawImageRect($Graphic, $Bitmap, 0, 0, $Width, $Height)
    WEnd

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

    Func _Exit()
    _GDIPlus_GraphicsDispose($Graphic)
    _GDIPlus_ImageDispose($Buffer)
    _WinAPI_DeleteObject($Bitmap)
    _GDIPlus_Shutdown()
    Exit
    EndFunc ;==>_Exit

    [/autoit]
  • Schonmal vielen Dank dafür !
    Das hat echt geholfen.

    Jetzt benötige ich nur noch Hilfe mit der Kollision :)