Advertisement
Guest User

Untitled

a guest
Mar 8th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 5.31 KB | None | 0 0
  1.  
  2. #include <GDIPLus.au3>
  3. #include <Misc.au3>
  4.  
  5. ; GDI+
  6. Global $hGraphic, $g_hBitmap, $g_hGfxCtxt
  7. Global $hPen, $hPenRed, $hBrush, $hBrushFood
  8. Global $cBackground = 0xFFFFFFFF
  9.  
  10.  
  11. ; snake
  12. Global $startLen = 4
  13. Global $numberSnake = 10
  14.  
  15. Global $Snake[$numberSnake]
  16. Global $Direction[$numberSnake]
  17. Global $State[$numberSnake]
  18. Global $Food[$numberSnake][2]
  19.  
  20.  
  21. ; gui
  22. Global $GridSize = 30
  23. Global $GridCount = 30
  24.  
  25. Global $GUI, $GuiW = $GridSize * $GridCount, $GuiH = $GridSize * $GridCount
  26.  
  27. $GUI = GUICreate("", $GuiW, $GuiH)
  28. GUISetState()
  29.  
  30.  
  31. CreateSnake()
  32.  
  33. _Graphic_StartUp()
  34.  
  35. While 1
  36.  
  37.     BotSnake()
  38.     MoveSnake()
  39.     CheckGame()
  40.     Draw()
  41.  
  42.     Switch GUIGetMsg()
  43.         Case -3
  44.             Exit
  45.     EndSwitch
  46.  
  47. WEnd
  48.  
  49. Func BotSnake()
  50.  
  51.     For $i = 0 To $numberSnake - 1
  52.  
  53.         $Position = $Snake[$i]
  54.         $x = $Position[0][0]
  55.         $y = $Position[0][1]
  56.  
  57.         $x_food = $Food[$i][0]
  58.         $y_food = $Food[$i][1]
  59.  
  60.         If Random(0, 1, 1) = 0 Then
  61.  
  62.             If $x <= $x_food Then $iDirection = 2
  63.             If $x > $x_food Then $iDirection = 0
  64.  
  65.         Else
  66.  
  67.             If $y <= $y_food Then $iDirection = 3
  68.             If $y > $y_food Then $iDirection = 1
  69.  
  70.         EndIf
  71.  
  72.         ChangeDirection($i, $iDirection)
  73.  
  74.     Next
  75.  
  76. EndFunc
  77.  
  78. Func ChangeDirection($i, $iDirection)
  79.  
  80.     If $Direction[$i] = 0 And $iDirection = 2 Then Return
  81.     If $Direction[$i] = 1 And $iDirection = 3 Then Return
  82.     If $Direction[$i] = 2 And $iDirection = 0 Then Return
  83.     If $Direction[$i] = 3 And $iDirection = 1 Then Return
  84.  
  85.     $Direction[$i] = $iDirection
  86.  
  87. EndFunc
  88.  
  89. Func CreateSnake()
  90.  
  91.     Local $Position[$startLen][2]
  92.  
  93.     For $i = 0 To $numberSnake - 1
  94.  
  95.         $randomX = Random($startLen - 1, $GridCount, 1)
  96.         $randomY = Random(0, $GridCount, 1)
  97.  
  98.         For $iPos = 0 to $startLen - 1
  99.  
  100.             $Position[$iPos][0] = $randomX - $iPos
  101.             $Position[$iPos][1] = $randomY
  102.  
  103.         Next
  104.  
  105.         $Food[$i][0] = Random(0, $GridCount - 1, 1)
  106.         $Food[$i][1] = Random(0, $GridCount - 1, 1)
  107.  
  108.         $State[$i] = True
  109.         $Direction[$i] = Random(1, 3, 1) ; 0 = left; 1 = up; 2 = right; 3 = down
  110.         $Snake[$i] = $Position
  111.  
  112.     Next
  113. EndFunc
  114.  
  115. Func MoveSnake()
  116.  
  117.     Local $State, $Position
  118.  
  119.     For $i = 0 To $numberSnake - 1
  120.  
  121.         $Position = $Snake[$i]
  122.  
  123.         $x = $Position[0][0]
  124.         $y = $Position[0][1]
  125.  
  126.         __GetDirection($Direction[$i], $x, $y)
  127.  
  128.         ; move snake
  129.  
  130.         For $iPos = UBound($Position) - 1 to 1 step - 1
  131.  
  132.             $Position[$iPos][0] = $Position[$iPos - 1][0]
  133.             $Position[$iPos][1] = $Position[$iPos - 1][1]
  134.         Next
  135.  
  136.         $Position[0][0] = $x
  137.         $Position[0][1] = $y
  138.  
  139.         $Snake[$i] = $Position
  140.  
  141.         $State = CheckSnake($i)
  142.  
  143.         Switch $State
  144.  
  145.             Case -1
  146.                 KillSnake($i)
  147.  
  148.             Case 1
  149.                 EatFood($i)
  150.  
  151.         EndSwitch
  152.     Next
  153. EndFunc
  154.  
  155. Func EatFood($i)
  156.  
  157.     $Position = $Snake[$i]
  158.  
  159.     $len = UBound($Position)
  160.  
  161.     ReDim $Position[$len + 1][2]
  162.  
  163.     ; new tail = tail
  164.     $Position[$len][0] = $Position[$len - 1][0]
  165.     $Position[$len][1] = $Position[$len - 1][1]
  166.  
  167.     ; recreate food
  168.     $Food[$i][0] = Random(0, $GridCount - 1, 1)
  169.     $Food[$i][1] = Random(0, $GridCount - 1, 1)
  170.  
  171.     $Snake[$i] = $Position
  172.  
  173. EndFunc
  174.  
  175. Func KillSnake($i)
  176.  
  177.     $State[$i] = False
  178.  
  179. EndFunc
  180.  
  181. Func CheckGame()
  182.  
  183.     For $i = 0 To $numberSnake - 1
  184.  
  185.         If $State[$i] = True Then Return
  186.     Next
  187.  
  188.     CreateSnake()
  189.  
  190. EndFunc
  191.  
  192. Func CheckSnake($i)
  193.  
  194.     $Position = $Snake[$i]
  195.  
  196.     $x = $Position[0][0]
  197.     $y = $Position[0][1]
  198.  
  199.     If $x < 0 Or $x >= $GridCount Then Return -1
  200.     If $y < 0 Or $y >= $GridCount Then Return -1
  201.  
  202.     For $iPos = 1 To UBound($Position) - 1
  203.  
  204.         If $x = $Position[$iPos][0] And $y = $Position[$iPos][1] Then Return -1
  205.  
  206.     Next
  207.  
  208.     If $x = $Food[$i][0] And $y = $Food[$i][1] Then Return 1
  209.  
  210. EndFunc
  211.  
  212. Func __GetDirection($iDirection, ByRef $x, ByRef $y)
  213.  
  214.     ; get direction
  215.     Switch $iDirection
  216.  
  217.         Case 0 ; left
  218.             $x -= 1
  219.  
  220.         Case 1 ; up
  221.             $y -= 1
  222.  
  223.         Case 2 ; right
  224.             $x += 1
  225.  
  226.         Case 3 ; down
  227.             $y += 1
  228.     EndSwitch
  229.  
  230. EndFunc
  231.  
  232. Func Draw()
  233.  
  234.     _GDIPlus_GraphicsClear($g_hGfxCtxt, $cBackground)
  235.  
  236.     For $i = 0 To $numberSnake - 1
  237.  
  238.         If $State[$i] = False Then ContinueLoop
  239.  
  240.         $Position = $Snake[$i]
  241.  
  242.         For $iPos = 0 To UBound($Position) - 1
  243.  
  244.             $x = $Position[$iPos][0] * $GridSize
  245.             $y = $Position[$iPos][1] * $GridSize
  246.  
  247.             _GDIPlus_GraphicsFillRect($g_hGfxCtxt, $x, $y, $GridSize, $GridSize, $hBrush)
  248.             _GDIPlus_GraphicsDrawRect($g_hGfxCtxt, $x, $y, $GridSize, $GridSize, $hPen)
  249.  
  250.  
  251.         Next
  252.  
  253.         $x_head = $Position[0][0] * $GridSize
  254.         $y_head = $Position[0][1] * $GridSize
  255.         $x_food = $Food[$i][0] * $GridSize
  256.         $y_food = $Food[$i][1] * $GridSize
  257.  
  258.         _GDIPlus_GraphicsFillRect($g_hGfxCtxt, $x_food, $y_food, $GridSize, $GridSize, $hBrushFood)
  259.         _GDIPlus_GraphicsDrawRect($g_hGfxCtxt, $x_food, $y_food, $GridSize, $GridSize, $hPen)
  260.         _GDIPlus_GraphicsDrawLine($g_hGfxCtxt, $x_head + $GridSize / 2, $y_head + $GridSize / 2, $x_food + $GridSize / 2, $y_food + $GridSize / 2, $hPenRed)
  261.  
  262.  
  263.     Next
  264.  
  265.     _GDIPlus_GraphicsDrawImageRect($hGraphic, $g_hBitmap, 0, 0, $GuiW, $GuiH)
  266.  
  267. EndFunc
  268.  
  269. Func _Graphic_StartUp()
  270.  
  271.     _GDIPlus_Startup()
  272.  
  273.     ; install gdi
  274.     $hGraphic = _GDIPlus_GraphicsCreateFromHWND($GUI)
  275.     $g_hBitmap = _GDIPlus_BitmapCreateFromGraphics($GuiW, $GuiH, $hGraphic)
  276.     $g_hGfxCtxt = _GDIPlus_ImageGetGraphicsContext($g_hBitmap)
  277.     _GDIPlus_GraphicsSetSmoothingMode($g_hGfxCtxt, 2)
  278.  
  279.     $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
  280.     $hBrushFood = _GDIPlus_BrushCreateSolid(0xFFFF0000)
  281.     $hPen = _GDIPlus_PenCreate(0xFF000000, 2)
  282.     $hPenRed = _GDIPlus_PenCreate(0xFFFF0000)
  283.  
  284.  
  285. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement