#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 - Gamma", $Width, $Height)
GUISetState(@SW_SHOW)
Opt("GUIOnEventMode",1)
GUISetOnEvent($GUI_EVENT_CLOSE,"ende")

$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"
$pPlatte_braun1 = @ScriptDir&"\Platform_braun1.gif"
$pPlatte_braun2 = @ScriptDir&"\Platform_braun2.gif"
$pPunkteleiste = @ScriptDir&"\Punkteleiste.gif"
$pBackground = @ScriptDir&"\background.gif"
$pStart = @ScriptDir&"\Start.gif"

_GDIPlus_Startup()
$Background = _GDIPlus_ImageLoadFromFile ($pBackground)
$Start = _GDIPlus_ImageLoadFromFile ($pStart)
$Platte_braun1 = _GDIPlus_ImageLoadFromFile ($pPlatte_braun1)
$Platte_braun2 = _GDIPlus_ImageLoadFromFile ($pPlatte_braun2)
$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]]
Global $special[1][2] = [[Random(0, $Width - $Plat_Width, 1), 370]]

$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
$platten = 0
_Start()
While 1
	if $MoveSpeed<0 then $MoveSpeed+=0.5
	if $MoveSpeed>0 then $MoveSpeed-=0.5
	Select
		Case _IsPressed("25",$dll)
			$MoveSpeed -= 2
			if $MoveSpeed<-8 then $MoveSpeed=-8
;~ 			$x-=5
			$AktuellerDoodle = $Doodle_Links
		Case _IsPressed("27",$dll)
			$MoveSpeed += 2
			if $MoveSpeed>8 then $MoveSpeed=8
;~ 			$x+=5
			$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
If $die = True Then
	_Start()
EndIf

Redraw()

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= 3.5
	Case $y > 180
		$Platmovespeed = 0.5
	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]
		$platten +=1
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 - 10 >= $Height Then
	$die = True
EndIf

If $platten = 10 Then
	Setspecial()
EndIf


If $move = 1 then
	$yboden += $Platmovespeed
		For $n = 0 to UBound($gruen_n, 1)-1
		$gruen_n[$n][1] += $Platmovespeed
			$special[0][1] += $Platmovespeed
		If $gruen_n[$n][1] > $Height Then
			reset($n)
		EndIf
	Next
	Endif


;~ MsgBox(0, "", $MoveSpeed)

;~     _GDIPlus_GraphicsClear($Buffer, 0xFFFFFFFF)
	_WinAPI_RedrawWindow($GUI1, "", "", $RDW_UPDATENOW + $RDW_FRAME)
    _GDIPlus_GraphicsDrawImage ($Buffer, $Background, 0, 0)
    _GDIPlus_GraphicsDrawImage ($Buffer, $AktuellerDoodle, $x, $y)

;~ 		_GDIPlus_GraphicsDrawImage ($Buffer, $Platte_braun2, 50+37, 370)


	For $n = 0 to UBound($gruen_n, 1)-1
	_GDIPlus_GraphicsDrawImage ($Buffer, $Platform_gruen, $gruen_n[$n][0], $gruen_n[$n][1])
Next
;~ _GDIPlus_GraphicsDrawImage ($Buffer, $Platte_braun1, 50, 300)
	_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 Setspecial()
	$special[0][0] = Random(0, $Width - $Plat_Width, 1)
	$special[0][1] = Random(-45, -65, 1)
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 _Start()
;~ 	MsgBox(0,"","")
	While 2
;~ 	$Label = GUICtrlCreateLabel("", 53, 149, 110, 41)
;~ 	GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
AutoItSetOption ("MouseCoordMode", 0)

	Select
		case _IsPressed("01")
			$pos = MouseGetPos()
		Select
			Case $pos[0] > 53 and $pos[0] < 163 and $pos[1] > 149 and $pos[1] < 190
;~
				$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]]
				Global $special[1][2] = [[Random(0, $Width - $Plat_Width, 1), 370]]

				$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]


				$y  = 380 - $Doodle_Height

				$onplatform = 0
				$plat = 0
				$MoveSpeed = 0
				$wasonplat = 0
				$move =0
				$yboden = $gruen_n[0][1]
				$Platmovespeed = 0
				$platten = 0

				ExitLoop
		EndSelect
		EndSelect


		 $y = 400  - $Doodle_Height - 10 - Round (Abs(Sin($i))*$JumpHeight)
		$x = 60
	$i += $JumpSpeed


	_WinAPI_RedrawWindow($GUI1, "", "", $RDW_UPDATENOW + $RDW_FRAME)

	_GDIPlus_GraphicsDrawImage ($Buffer, $Start, 0, 0)
    _GDIPlus_GraphicsDrawImage ($Buffer, $Doodle_Rechts, $x, $y)
	_GDIPlus_GraphicsDrawImage ($Buffer, $Platform_gruen, 50, 400)

    _GDIPlus_GraphicsDrawImageRect($Graphic, $Bitmap, 0, 0, $Width, $Height)
	Sleep(20)
	Wend
EndFunc

Func _SetStandard()

EndFunc

func ende()
	_GDIPlus_GraphicsDispose($Graphic)
    _GDIPlus_ImageDispose($Buffer)
    _WinAPI_DeleteObject($Bitmap)
    Exit
EndFunc
