SHARE
TWEET

Untitled

thedemonsmsc Sep 20th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #include <WinAPI.au3>
  4. #include <misc.au3>
  5. #include <WindowsConstants.au3>
  6. #include <GDIPlus.au3>
  7. #include <Array.au3>
  8. OnAutoItExitRegister("GDI_CleanUp")
  9.  
  10. Global $ObjectsHeight = 155
  11. Global $ObjectsSpace = 180
  12. Global $ObjectsLimit = 585
  13. Global $ObjectsW = 72
  14. Global $ObjectsSpeed = 3
  15. Global $Objects[3][2]
  16.  
  17. Global $BirdW = 57
  18. Global $BirdH = 40
  19. Global $BirdDropSpeed = 6
  20. Global $Birds[100][2], $BirdNumber = 1
  21. Global $BirdsState[$BirdNumber + 1][2]
  22. Global $BirdsFitness[$BirdNumber]
  23.  
  24. Global $Gen = 1
  25.  
  26.     Global $hDC, $hHBitmap, $buffer, $context
  27. Global $hGraphic, $hBrushObject, $hBrushBird, $hPenBlack, $hPenRed, $g_hBitmap, $g_hGfxCtxt
  28.  
  29. Const $cBlack           = 0xFF000000
  30. Const $cRed             = 0x44FF0000
  31. Const $cObject          = 0x5500BB00
  32. Const $cBird            = 0x55FF0000
  33. Const $cBackground      = 0xFFFFFFFF
  34.  
  35. Global $Gui
  36. Global $GuiW = 470, $GuiH = 700
  37.  
  38. $Gui = GUICreate("FlappyBird lololol", $GuiW, $GuiH)
  39. GUISetState()
  40.  
  41. GDI_StartUp()
  42.  
  43. CreateFirstObjects()
  44. CreateBird()
  45. $timeHold = TimerInit()
  46. $gameSpeed = 10
  47. Global $holdSpace =False
  48.  
  49. while True
  50.     If $holdSpace And _IsPressed(20) = False Then
  51.         $holdSpace = False
  52.     ElseIf $holdSpace = False And _IsPressed(20) Then
  53.         $holdSpace = True
  54.         $BirdsState[1][0] = 1
  55.     EndIf
  56.  
  57.     If TimerDiff($timeHold) > $gameSpeed Then
  58.         MoveObjects()
  59.         MoveBirds()
  60.         $timeHold = TimerInit()
  61.     EndIf
  62.     DrawObjects()
  63.     CheckAlive()
  64.     Switch GUIGetMsg()
  65.         Case -3
  66.             Exit
  67.     EndSwitch
  68. WEnd
  69.  
  70. Func MoveBirds()
  71.     Local $isNext = False
  72.     For $i = 1 to $Birds[0][0]
  73.         If $Birds[$i][0] < -3 - $BirdW Then ContinueLoop
  74.         If $BirdsState[$i][0] = - 1 Then ContinueLoop
  75.         For $o = 0 to 2
  76.             If $Birds[$i][0] + $BirdW >= $Objects[$o][0] And $Birds[$i][0] <= $Objects[$o][0] + $ObjectsW And ($Birds[$i][1] < $Objects[$o][1] Or ($Birds[$i][1] + $BirdH) > ($Objects[$o][1] + $ObjectsHeight)) Then
  77.                 $BirdsState[$i][0] = -1
  78.  
  79.                 ; ====================================================  TÍNH FITNESS  ========================================================
  80.                 If $Birds[$i][1] < $Objects[$o][1] Then
  81.                     $BirdsFitness[$i - 1] += ($Objects[$o][1] - $Birds[$i][1]) / $Objects[$o][1]
  82.                 ElseIf $birds[$i][1] > $Objects[$o][1] + $ObjectsHeight Then
  83.                     $BirdsFitness[$i - 1] += ($Birds[$i][1] - ($Objects[$o][1] + $ObjectsHeight)) / ($ObjectsLimit - $Objects[$o][1] + $ObjectsHeight)
  84.                 Else
  85.                     $BirdsFitness += 100
  86.                 EndIf
  87.  
  88.                 ;Set fitness
  89.                 ; ============================================================================================================
  90.  
  91.                 $isNext = True
  92.                 ExitLoop
  93.             EndIf
  94.         Next
  95.         If $isNext = True then
  96.             $isNext = False
  97.             ContinueLoop
  98.         EndIf
  99.         If $BirdsState[$i][0] > 0 Then
  100.             Switch $BirdsState[$i][0]
  101.                 Case 1
  102.                     $Birds[$i][1] -= $BirdDropSpeed*1.5
  103.                     $BirdsState[$i][1] += 1
  104.                     if $BirdsState[$i][1] >= 6 Then
  105.                         $BirdsState[$i][1] = 0
  106.                         $BirdsState[$i][0] += 1
  107.                     EndIf
  108.                     If $Birds[$i][1] <= 0 Then $Birds[$i][1] = 0
  109.                 Case 2
  110.                     $Birds[$i][1] -= $BirdDropSpeed
  111.                     $BirdsState[$i][1] += 1
  112.                     if $BirdsState[$i][1] >= 3 Then
  113.                         $BirdsState[$i][1] = 0
  114.                         $BirdsState[$i][0] += 1
  115.                     EndIf
  116.                     If $Birds[$i][1] <= 0 Then $Birds[$i][1] = 0
  117.                 Case 3
  118.                     $Birds[$i][1] -= $BirdDropSpeed / 2
  119.                     $BirdsState[$i][1] += 1
  120.                     if $BirdsState[$i][1] >= 3 Then
  121.                         $BirdsState[$i][1] = 0
  122.                         $BirdsState[$i][0] += 1
  123.                     EndIf
  124.                     If $Birds[$i][1] <= 0 Then $Birds[$i][1] = 0
  125.                 Case 4
  126.                     $BirdsState[$i][1] += 1
  127.                     if $BirdsState[$i][1] >= 2 Then
  128.                         $BirdsState[$i][1] = 0
  129.                         $BirdsState[$i][0] += 1
  130.                     EndIf
  131.                 Case 5
  132.                     $Birds[$i][1] += $BirdDropSpeed/2
  133.                     $BirdsState[$i][1] += 1
  134.                     if $BirdsState[$i][1] >= 3 Then
  135.                         $BirdsState[$i][1] = 0
  136.                         $BirdsState[$i][0] += 1
  137.                     EndIf
  138.                 Case 6
  139.                     $Birds[$i][1] += $BirdDropSpeed
  140.                     $BirdsState[$i][1] += 1
  141.                     if $BirdsState[$i][1] >= 6 Then
  142.                         $BirdsState[$i][1] = 0
  143.                         $BirdsState[$i][0] = 0
  144.                     EndIf
  145.             EndSwitch
  146.         Else
  147.             If $Birds[$i][1] + $BirdH + $BirdDropSpeed >= $ObjectsLimit Then
  148.                 $Birds[$i][1] = $ObjectsLimit - $BirdH
  149.             Else
  150.                 $Birds[$i][1] += $BirdDropSpeed * 1.5
  151.             EndIf
  152.         EndIf
  153.     Next
  154. EndFunc
  155.  
  156. Func MoveObjects()
  157.     For $i = 0 to 2
  158.         $Objects[$i][0] -= $ObjectsSpeed
  159.         If $Objects[$i][0] <= - $ObjectsW Then
  160.             $Objects[$i][0] = $Objects[(($i - 1 < 0) ? 2 : $i - 1)][0] + $ObjectsSpace + $ObjectsW
  161.             $Objects[$i][1] = Random(100, $ObjectsLimit - $ObjectsHeight - 100, 1)
  162.         EndIf
  163.     Next
  164.     For $i = 1 to $Birds[0][0]
  165.         If $BirdsState[$i][0] = -1 Then
  166.             $Birds[$i][0] -= $ObjectsSpeed
  167.         Else
  168.             $BirdsFitness[$i - 1] += $ObjectsSpeed
  169.         EndIf
  170.     Next
  171.  
  172. EndFunc
  173.  
  174. Func CreateFirstObjects()
  175.     For $i = 0 to 2
  176.         $Objects[$i][0] = 600 + $i * ($ObjectsSpace + $ObjectsW)
  177.         $Objects[$i][1] = Random(100, $ObjectsLimit - $ObjectsHeight - 100, 1)
  178.     Next
  179. EndFunc
  180.  
  181. Func CreateBird()
  182.     Local $centerX = ($GuiW - $BirdW)/2
  183.     Local $centerY = ($ObjectsLimit - $BirdH)/2
  184.     $Birds[0][0] = $BirdNumber
  185.     For $i = 1 to $Birds[0][0]
  186.         $Birds[$i][0] = $centerX
  187.         $Birds[$i][1] = $centerY
  188.         $BirdsState[$i][0] = 6
  189.         $BirdsState[$i][1] = 0
  190.         $BirdsFitness[$i - 1] = 0
  191.     Next
  192. EndFunc
  193.  
  194. Func DrawObjects()
  195.     _GDIPlus_GraphicsClear($context, $cBackground)
  196.     For $i = 0 to 2
  197.         _GDIPlus_GraphicsFillRect($context, $Objects[$i][0], 0, $ObjectsW, $Objects[$i][1], $hBrushObject)
  198.         _GDIPlus_GraphicsFillRect($context, $Objects[$i][0], $Objects[$i][1] + $ObjectsHeight, $ObjectsW, $ObjectsLimit - $Objects[$i][1] - $ObjectsHeight, $hBrushObject)
  199.  
  200.         _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] + 20, 0, $ObjectsW - 30, $Objects[$i][1], $hBrushObject)
  201.         _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] + 20, $Objects[$i][1] + $ObjectsHeight, $ObjectsW - 30, $ObjectsLimit - $Objects[$i][1] - $ObjectsHeight, $hBrushObject)
  202.  
  203.         _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] - 5, $Objects[$i][1] - 15, $ObjectsW + 10, 15, $hBrushObject)
  204.         _GDIPlus_GraphicsFillRect($context, $Objects[$i][0] - 5, $Objects[$i][1] + $ObjectsHeight, $ObjectsW + 10, 15, $hBrushObject)
  205.  
  206.         _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0], -2, $ObjectsW, $Objects[$i][1] + 2 - 15, $hPenBlack)
  207.         _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0], $Objects[$i][1] + $ObjectsHeight + 15, $ObjectsW, $ObjectsLimit - $Objects[$i][1] - $ObjectsHeight - 15, $hPenBlack)
  208.  
  209.         _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0] - 5, $Objects[$i][1] - 15, $ObjectsW + 10, 15, $hPenBlack)
  210.         _GDIPlus_GraphicsDrawRect($context, $Objects[$i][0] - 5, $Objects[$i][1] + $ObjectsHeight, $ObjectsW + 10, 15, $hPenBlack)
  211.     Next
  212.     For $i = 1 to $Birds[0][0]
  213.         _GDIPlus_GraphicsFillEllipse($context, $Birds[$i][0], $Birds[$i][1], $BirdW, $BirdH, $hBrushBird)
  214.  
  215.         _GDIPlus_GraphicsDrawEllipse($context, $Birds[$i][0], $Birds[$i][1], $BirdW, $BirdH, $hPenBlack)
  216.     Next
  217.     _GDIPlus_GraphicsDrawLine($context, 0, $ObjectsLimit, $GuiW, $ObjectsLimit, $hPenBlack)
  218.  
  219.     _GDIPlus_GraphicsDrawString($context, "GEN: " & $Gen, 30, $ObjectsLimit + 10)
  220. ;~  _GDIPlus_GraphicsDrawImageRect($hGraphic, $g_hBitmap, 0, 0, $GuiW, $GuiH)
  221.  
  222.     _WinAPI_BitBlt($hDC, 0, 0, $GuiW, $GuiH, $buffer, 0, 0, $SRCCOPY)
  223. EndFunc
  224.  
  225. Func CheckAlive()
  226.     For $i = 1 to $Birds[0][0]
  227.         If $BirdsState[$i][0] <> - 1 then Return
  228.     Next
  229.  
  230.     CreateFirstObjects()
  231.     CreateBird()
  232.     $Gen += 1
  233. EndFunc
  234.  
  235.  
  236. Func GDI_StartUp()
  237.     _GDIPlus_Startup()
  238.  
  239.     $hDC = _WinAPI_GetDC($GUI)
  240.     $hHBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $GuiW, $GuiH)
  241.  
  242.     $buffer = _WinAPI_CreateCompatibleDC($hDC)
  243.     _WinAPI_SelectObject($buffer, $hHBitmap)
  244.  
  245.     $context = _GDIPlus_GraphicsCreateFromHDC($buffer)
  246.     _GDIPlus_GraphicsSetSmoothingMode($context, $GDIP_SMOOTHINGMODE_HIGHQUALITY)
  247.     _GDIPlus_GraphicsSetPixelOffsetMode($context, $GDIP_PIXELOFFSETMODE_HIGHQUALITY)
  248.  
  249.  
  250.  
  251.     $hBrushObject = _GDIPlus_BrushCreateSolid($cObject)
  252.     $hBrushBird = _GDIPlus_BrushCreateSolid($cBird)
  253.     $hPenBlack = _GDIPlus_PenCreate($cBlack)
  254.     $hPenRed = _GDIPlus_PenCreate($cRed)
  255. EndFunc
  256. Func GDI_CleanUp()
  257.     _GDIPlus_BrushDispose($hBrushObject)
  258.     _GDIPlus_BrushDispose($hBrushBird)
  259.     _GDIPlus_PenDispose($hPenBlack)
  260.     _GDIPlus_PenDispose($hPenRed)
  261.     _GDIPlus_GraphicsDispose($hGraphic)
  262.     _GDIPlus_BitmapDispose($g_hBitmap)
  263.     _GDIPlus_ImageDispose($g_hGfxCtxt)
  264.     _GDIPlus_Shutdown()
  265. EndFunc
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top