#include <GdiPlus.au3>
#include <Misc.au3>

_GDIPlus_Startup()
Opt("GUIOnEventMode", 1)
$hDLL = DllOpen("user32.dll")

$hGUI = GUICreate("Tunnel", 210, 386)
GUISetOnEvent(-3, "_Exit")
GUISetState(@SW_SHOW)

Local $iX = 105, $iY = 370, $iInc = 0, $iScore = 0, $iFrames = 0
Local $aBorders[386][2]

For $i = 7 To 385 Step 7
	For $x = 1 To 7
		$aBorders[$i - $x][0] = 75
		$aBorders[$i - $x][1] = 135
	Next
Next

AdlibRegister("_FPS", 1000)

$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics(210, 386, $hGraphics)
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

_GDIPlus_GraphicsFillRect($hGraphics, $iX, $iY, 5, 5)

While 1
	$iFrames += 1
	$iScore += 1
	$iInc = Random(-1, 1, 1) * 2

	$aBorder_Tmp = $aBorders
	For $i = 1 To 385 Step 1
		$aBorders[$i][0] = $aBorder_Tmp[$i - 1][0]
		$aBorders[$i][1] = $aBorder_Tmp[$i - 1][1]
	Next

	If $aBorders[0][0] + $iInc <= 1 or $aBorders[0][1] + $iInc >= 209 Then $iInc *= -1

	$aBorders[0][0] = $aBorders[0][0] + $iInc
	$aBorders[0][1] = $aBorders[0][1] + $iInc

	If _IsPressed(25, $hDLL) Then $iX -= 2
	If _IsPressed(26, $hDLL) Then $iY -= 2
	If _IsPressed(27, $hDLL) Then $iX += 2
	If _IsPressed(28, $hDLL) Then $iY += 2

	If $iY <= 14 Then $iY = 14
	If $iY >= 370 Then $iY = 370

	For $i = $iY - 14 To $iY + 14 Step 7
		If $iX < $aBorders[$i][0] or $iX > $aBorders[$i][1] - 5 Then
			MsgBox(48, "Oops!", "You crashed!" & @CRLF & "Score: " & $iScore)
			_Exit()
		EndIf
	Next

	For $i = 0 To 385 Step 7
		_GDIPlus_GraphicsDrawRect($hBuffer, $aBorders[$i][0], $i, 1, 7)
		_GDIPlus_GraphicsDrawRect($hBuffer, $aBorders[$i][1], $i, 1, 7)
	Next

	_GDIPlus_GraphicsFillRect($hBuffer, $iX, $iY, 7, 7)
	_GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 210, 386)
	_GDIPlus_GraphicsClear($hBuffer, 0xFFDCDCDC)
WEnd

Func _FPS()
	WinSetTitle($hGUI, "", $iScore & "@" & $iFrames)
	$iFrames = 0
EndFunc

Func _Exit()
	_GDIPlus_ImageDispose($hBuffer)
	_GDIPlus_BitmapDispose($hBitmap)
	_GDIPlus_GraphicsDispose($hGraphics)
	_GDIPlus_Shutdown()
	AdlibUnRegister("_FPS")
	DllClose($hDLL)
	Exit
EndFunc