#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>

$Width = 300
$Height = 460

$JumpSpeed = 0.15
$JumpHeight = 80 ;Pixel
$MoveSpeed = 0
$Zeitschrit = 0.2

$GUI1 = GUICreate("Doodle Jump - Betha", $Width, $Height)
GUISetState(@SW_SHOW)
$dll=DllOpen("user32.dll")

$pDoodle_Rechts = @ScriptDir&"\Doodle_Rechts e.png"
$pDoodle_Links = @ScriptDir&"\Doodle_Links e.png"
$pPlatte_gruen = @ScriptDir&"\Platform grün.gif"
$pPunkteleiste = @ScriptDir&"\Punkteleiste.gif"
$pBackground = @ScriptDir&"\background.gif"

_GDIPlus_Startup()
$Background = _GDIPlus_ImageLoadFromFile ($pBackground)
$Punkteleiste = _GDIPlus_ImageLoadFromFile ($pPunkteleiste)
$Platform_gruen =_GDIPlus_ImageLoadFromFile ($pPlatte_gruen)
$Doodle_Rechts =_GDIPlus_ImageLoadFromFile ($pDoodle_Rechts)
$Doodle_Links = _GDIPlus_ImageLoadFromFile ($pDoodle_Links)
$Doodle_Height = _GDIPlus_ImageGetHeight($Doodle_Rechts)
$Doodle_Width = _GDIPlus_ImageGetWidth($Doodle_Rechts)
$Plat_Width = _GDIPlus_ImageGetWidth($Platform_gruen)
$AktuellerDoodle = $Doodle_Rechts
Global $gruen_n[9][2] = [[Random(0, $Width - $Plat_Width, 1), 400], [Random(0, $Width - $Plat_Width, 1), 340], [Random(0, $Width - $Plat_Width, 1), 280], _
[Random(0, $Width - $Plat_Width, 1), 220], [Random(0, $Width - $Plat_Width, 1), 160], [Random(0, $Width - $Plat_Width, 1), 100], [Random(0, $Width - $Plat_Width, 1), 40], [Random(0, $Width - $Plat_Width, 1), -20], [Random(0, $Width - $Plat_Width, 1), -80]]
$Graphic = _GDIPlus_GraphicsCreateFromHWND($GUI1)
$Bitmap = _GDIPlus_BitmapCreateFromGraphics($Width, $Height, $Graphic)
$Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)
$die = False
$i = 0
$t = 0
$x = $gruen_n[0][0]
$Jumped = 0
$yplus = 0
$y  = 380 - $Doodle_Height
$ynew = 0
$onplatform = 0
$plat = 0
$inotok = 0
$wasonplat = 0
$ok = 0
$jumponground = 0
$move =0
$score = 0
$scoreplus = 0
$yboden = $gruen_n[0][1]
$Platmovespeed = 0
While 1




	Select
		Case _IsPressed("25",$dll)
			$MoveSpeed -= 2
			$AktuellerDoodle = $Doodle_Links
		Case _IsPressed("27",$dll)
			$MoveSpeed += 2
			$AktuellerDoodle = $Doodle_Rechts
		Case $x + $Doodle_Width < 0
			$x = $Width
		Case $x > $Width
			$x = $Doodle_Width * -1
		Case $MoveSpeed > 30
			$MoveSpeed =30
		Case $MoveSpeed < -30
			$MoveSpeed = -30
	EndSelect


Redraw()

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_GraphicsDispose($Graphic)
            _GDIPlus_ImageDispose($Buffer)
            _WinAPI_DeleteObject($Bitmap)

            Exit
    EndSwitch
WEnd

Func Redraw()

		$i += $JumpSpeed
		$t += $Zeitschrit

;~ 		If $onplatform = 0 Then
;~ 		 $y = $gruen_n[$plat][1]  - $Doodle_Height - 10 - Round (40*$t-(9.81/2)*$t^2)
;~ 		 $wasonplat = 1
;~ 	 Else
;~ 		 $y = $gruen_n[$plat][1]  - $Doodle_Height - 10 - Round (40*$t-(9.81/2)*$t^2)

;~ EndIf


$platnotactive = 0
$platactive = 0

$platold = $plat
For $n = 0 to UBound($gruen_n, 1)-1 Step 1
Select

		Case $y < 220 and $y > 180
			$move = 1
			$Platmovespeed = 0.5
		Case $y < 180
			$Platmovespeed= 4
		Case $y > 180
			$Platmovespeed = 1
		Case $y >= 220
			$move = 0
			$Platmovespeed = 0
Endselect
Select
	Case  ($gruen_n[$n][0] - $Doodle_Width <= $x and $gruen_n[$n][0] + $Plat_Width >= $x) and($y+$Doodle_Height<=$gruen_n[$n][1] and $y+$Doodle_Height+15>=$gruen_n[$n][1])
		$onplatform = 1
		$plat = $n
		$t = 0
		$t = $Zeitschrit
		$yboden = $gruen_n[$n][1]
;~ 		If $n = 1 Then
;~ 			$wasonplat = 1
;~ 		EndIf

;~ 	Case $platold < $n
;~ 		$scoreplus += 500
;~ 	Case $platold = UBound($gruen_n, 1) and $plat = 1
;~ 		$scoreplus += 500




EndSelect
Next



$x += $MoveSpeed*$t/6

If $yboden= $y +30 Then
	$y = $yboden  - $Doodle_Height - 10 - Round (Abs(40*$t-(9.81/2)*$t^2))
	Else
$y = $yboden  - $Doodle_Height - 10 - Round (40*$t-(9.81/2)*$t^2)
EndIf
If $y + $Doodle_Height >= $Height Then
	$die = True
EndIf


If $move = 1 then
	$yboden += $Platmovespeed
		For $n = 0 to UBound($gruen_n, 1)-1
		$gruen_n[$n][1] += $Platmovespeed

		If $gruen_n[$n][1] > $Height Then
			reset($n)
		EndIf
	Next
	Endif
;#cs
If $die = True Then
	;_GDIPlus_Shutdown()
	GUIDelete($GUI1)
	$GUI1 = GUICreate("Doodle Jump - Betha", $Width, $Height)
	$Label = GUICtrlCreateLabel("Start", 48, 168, 92, 41)
	GUISetState(@SW_show)
	
	
	
	
	_GDIPlus_Startup()
$Background = _GDIPlus_ImageLoadFromFile (@ScriptDir&"\background.gif")
$Play = _GDIPlus_ImageLoadFromFile(@Scriptdir & "\Play.gif")
if @error then MsgBox(0,"","")
$Platform_gruen =_GDIPlus_ImageLoadFromFile ($pPlatte_gruen)
$Doodle_Rechts =_GDIPlus_ImageLoadFromFile ($pDoodle_Rechts)
$Doodle_Links = _GDIPlus_ImageLoadFromFile ($pDoodle_Links)
$Doodle_Height = _GDIPlus_ImageGetHeight($Doodle_Rechts)
$Doodle_Width = _GDIPlus_ImageGetWidth($Doodle_Rechts)
$Plat_Width = _GDIPlus_ImageGetWidth($Platform_gruen)
$AktuellerDoodle = $Doodle_Rechts

$Graphic = _GDIPlus_GraphicsCreateFromHWND($GUI1)
$Bitmap = _GDIPlus_BitmapCreateFromGraphics($Width, $Height, $Graphic)
$Buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap)

$die = False
$i = 0
$x = 50
$Jumped = 0
$yplus = 0
$y  = $Height  - $yplus - $Doodle_Height - 40 - Abs(Sin($i))*$JumpHeight
$ynew = 0
$onplatform = 0

While 1


	If $onplatform = 1 Then
		 $y = 400  - $Doodle_Height - 10 - Round (Abs(Sin($i))*$JumpHeight)
		 Else
		$y = $Height - $Doodle_Height - 10 - Round (Abs(Sin($i))*$JumpHeight)
	EndIf
$i += $JumpSpeed
	if 60 - $Doodle_Width <= $x and 60 + $Plat_Width >= $x Then
			if ($y+$Doodle_Height<=400 and $y+$Doodle_Height+6>=400) then
				$onplatform = 1
				$i = 0
			EndIf
		Else
			$onplatform = 0
			$die = True
		EndIf

	_WinAPI_RedrawWindow($GUI1, "", "", $RDW_UPDATENOW + $RDW_FRAME)
    
	
    _GDIPlus_GraphicsDrawImage ($Buffer, $AktuellerDoodle, $x, $y)
	_GDIPlus_GraphicsDrawImage ($Buffer, $Platform_gruen, 50, 400)
    _GDIPlus_GraphicsDrawImageRect($Graphic, $Bitmap, 0, 0, $Width, $Height)
	_GDIPlus_GraphicsDrawImage($Buffer,$Play,48, 168)
	
    Sleep(20)
	_GDIPlus_GraphicsDrawImage ($Buffer, $Background, 0, 0)
	
	$nMsg = GUIGetMsg()
    Switch $nMsg
		
		Case $GUI_EVENT_CLOSE
			ende()

		
		Case $Label
			Run(@ScriptFullPath)
			Exit

	EndSwitch 
WEnd
	

EndIf
#ce


    _GDIPlus_GraphicsDrawImage ($Buffer, $Background, 0, 0)

    _GDIPlus_GraphicsDrawImage ($Buffer, $AktuellerDoodle, $x, $y)


	For $n = 0 to UBound($gruen_n, 1)-1
	_GDIPlus_GraphicsDrawImage ($Buffer, $Platform_gruen, $gruen_n[$n][0], $gruen_n[$n][1])
Next
	_GDIPlus_GraphicsDrawImageRectRectTrans($Buffer, $Punkteleiste, 0, 0, "", "", "", "", "" , "", 2, 50)
    _GDIPlus_GraphicsDrawImageRect($Graphic, $Bitmap, 0, 0, $Width, $Height)
    Sleep(20)

EndFunc

Func reset($n)
	$gruen_n[$n][0] = Random(0, $Width - $Plat_Width, 1)
	$gruen_n[$n][1] = -80
EndFunc

Func _GDIPlus_GraphicsDrawImageRectRectTrans($hGraphics, $hImage, $iSrcX, $iSrcY, $iSrcWidth = "", $iSrcHeight = "", _
    $iDstX = "", $iDstY = "", $iDstWidth = "" , $iDstHeight = "", $iUnit = 2, $nTrans = 1)
    Local $tColorMatrix, $x, $hImgAttrib, $iW = _GDIPlus_ImageGetWidth($hImage), $iH = _GDIPlus_ImageGetHeight($hImage)
    If $iSrcWidth = 0 or $iSrcWidth = "" Then $iSrcWidth = $iW
    If $iSrcHeight = 0 or $iSrcHeight = "" Then $iSrcHeight = $iH
    If $iDstX = "" Then $iDstX = $iSrcX
    If $iDstY = "" Then $iDstY = $iSrcY
    If $iDstWidth = "" Then $iDstWidth = $iSrcWidth
    If $iDstHeight = "" Then $iDstHeight = $iSrcHeight
    If $iUnit = "" Then $iUnit = 2
    ;;create color matrix data
    $tColorMatrix = DllStructCreate("float[5];float[5];float[5];float[5];float[5]")
    ;blending values:
    $x = DllStructSetData($tColorMatrix, 1, 1, 1) * DllStructSetData($tColorMatrix, 2, 1, 2) * DllStructSetData($tColorMatrix, 3, 1, 3) * _
            DllStructSetData($tColorMatrix, 4, $nTrans, 4) * DllStructSetData($tColorMatrix, 5, 1, 5)
    ;;create an image attributes object and update its color matrix
    $hImgAttrib =  DllCall($ghGDIPDll, "int", "GdipCreateImageAttributes", "ptr*", 0)
    $hImgAttrib = $hImgAttrib[1]
     DllCall($ghGDIPDll, "int", "GdipSetImageAttributesColorMatrix", "ptr", $hImgAttrib, "int", 1, _
            "int", 1, "ptr", DllStructGetPtr($tColorMatrix), "ptr", 0, "int", 0)
    ;;draw image into graphic object with alpha blend
    DllCall($ghGDIPDll, "int", "GdipDrawImageRectRectI", "hwnd", $hGraphics, "hwnd", $hImage, "int", $iDstX, "int", _
            $iDstY, "int", $iDstWidth, "int", $iDstHeight, "int", $iSrcX, "int", $iSrcY, "int", $iSrcWidth, "int", _
            $iSrcHeight, "int", $iUnit, "ptr", $hImgAttrib, "int", 0, "int", 0)
    ;;clean up
    DllCall($ghGDIPDll, "int", "GdipDisposeImageAttributes", "ptr", $hImgAttrib)
    Return
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectTrans


Func Jumpup()
	Do
	$i += $JumpSpeed
    $y = $Height  - $Doodle_Height - 40 - Abs(Sin($i))*$JumpHeight
	_GDIPlus_GraphicsDrawImage ($Buffer, $Background, 0, 0)
    _GDIPlus_GraphicsDrawImage ($Buffer, $AktuellerDoodle, $x, $y)
	_GDIPlus_GraphicsDrawImage ($Buffer, $Platform_gruen, 60, 400)
    _GDIPlus_GraphicsDrawImageRect($Graphic, $Bitmap, 0, 0, $Width, $Height)
    Sleep(20)
	Until $y <= 400 +$Doodle_Height
EndFunc

func ende()
	_GDIPlus_GraphicsDispose($Graphic)
    _GDIPlus_ImageDispose($Buffer)
    _WinAPI_DeleteObject($Bitmap)
	_GDIPlus_Shutdown()
	Exit
EndFunc