Advertisement
Alan72104

Autoit tetris (broken)

Jan 6th, 2021
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 14.96 KB | None | 0 0
  1. #include <GDIPlus.au3>
  2. #include <GUIConstantsEx.au3>
  3. #include <AutoItConstants.au3>
  4.  
  5. ; ================================================================================================================== ;
  6. ; __/\\\\\\\\\\\\\\\__/\\\\\\\\\\\\\\\__/\\\\\\\\\\\\\\\____/\\\\\\\\\______/\\\\\\\\\\\_____/\\\\\\\\\\\___________ ;
  7. ; __\///////\\\/////__\/\\\///////////__\///////\\\/////___/\\\///////\\\___\/////\\\///____/\\\/////////\\\________ ;
  8. ; _________\/\\\_______\/\\\___________________\/\\\_______\/\\\_____\/\\\_______\/\\\______\//\\\______\///________ ;
  9. ; __________\/\\\_______\/\\\\\\\\\\\___________\/\\\_______\/\\\\\\\\\\\/________\/\\\_______\////\\\______________ ;
  10. ; ___________\/\\\_______\/\\\///////____________\/\\\_______\/\\\//////\\\________\/\\\__________\////\\\__________ ;
  11. ; ____________\/\\\_______\/\\\___________________\/\\\_______\/\\\____\//\\\_______\/\\\_____________\////\\\______ ;
  12. ; _____________\/\\\_______\/\\\___________________\/\\\_______\/\\\_____\//\\\______\/\\\______/\\\______\//\\\____ ;
  13. ; ______________\/\\\_______\/\\\\\\\\\\\\\\\_______\/\\\_______\/\\\______\//\\\__/\\\\\\\\\\\_\///\\\\\\\\\\\/____ ;
  14. ; _______________\///________\///////////////________\///________\///________\///__\///////////____\///////////_____ ;
  15. ; ================================================================================================================== ;
  16.  
  17. Global $g_bPaused = False
  18. Global Const $debug = True
  19. Global Const $height = 22
  20. Global Const $width = 10
  21. Global Const $scale = 32
  22. Global Const $offsetBoard = [10, 10]
  23. Global Const $offsetNextPiece = [10, 10]
  24. Global Const $offsetScore = []
  25. Global Const $offsetHelp = 0
  26. Global Const $bgColor = 0x309DDB
  27. Global $hGui
  28. Global $hGraphics
  29. Global $hBrush[8]
  30. Global Enum $enumBrushGray, $enumBrushLightBlue, $enumBrushBlue, $enumBrushOrange, $enumBrushYellow, $enumBrushGreen, $enumBrushPurple, $enumBrushRed
  31. Global $hPen[2]
  32. Global Enum $enumPenGuiFrame, $enumPenBoardFrame
  33. Global $board[1]
  34. Global $piece[5] = [False, 0, 0, 0, 0]
  35. Global Enum $enumPieceFalling, $enumPieceType, $enumPieceX, $enumPieceY, $enumPieceRotation
  36. Global Const $pieces = [ _
  37.     [[4,0,0,0],[0,0,0,0],[1,1,1,1],[0,0,0,0],[0,0,0,0]], _  ; 0
  38.     [[3,0,0],  [1,0,0],  [1,1,1],  [0,0,0]], _              ; 1
  39.     [[3,0,0],  [0,0,1],  [1,1,1],  [0,0,0]], _              ; 2
  40.     [[2,0],    [1,1],    [1,1]], _                          ; 3
  41.     [[3,0,0],  [0,1,1],  [1,1,0],  [0,0,0]], _              ; 4
  42.     [[3,0,0],  [0,1,0],  [1,1,1],  [0,0,0]], _              ; 5
  43.     [[3,0,0],  [1,1,0],  [0,1,1],  [0,0,0]]]                ; 6
  44. Global $aPiece[1][1]
  45. Global $aPieceRotatingPH[1][1]
  46. HotKeySet("{F6}", "TogglePause")
  47. HotKeySet("{F7}", "Terminate")
  48. HotKeySet("{UP}", "PieceTryRotate")
  49. HotKeySet("{LEFT}", "PieceTryGoLeft")
  50. HotKeySet("{RIGHT}", "PieceTryGoRight")
  51. HotKeySet("{DOWN}", "PieceTryGoDown")
  52.  
  53. ; ==================================================
  54. ; Various function parts
  55. ; ==================================================
  56.  
  57. Func BrushInit()
  58.     $hBrush[$enumBrushGray] = _GDIPlus_BrushCreateSolid(0xFF303030)       ; 0
  59.     $hBrush[$enumBrushLightBlue] = _GDIPlus_BrushCreateSolid(0xFF00F0F0)  ; 1
  60.     $hBrush[$enumBrushBlue] = _GDIPlus_BrushCreateSolid(0xFF0000F0)       ; 2
  61.     $hBrush[$enumBrushOrange] = _GDIPlus_BrushCreateSolid(0xFFF0A000)     ; 3
  62.     $hBrush[$enumBrushYellow] = _GDIPlus_BrushCreateSolid(0xFFF0F000)     ; 4
  63.     $hBrush[$enumBrushGreen] = _GDIPlus_BrushCreateSolid(0xFF00F000)      ; 5
  64.     $hBrush[$enumBrushPurple] = _GDIPlus_BrushCreateSolid(0xFFA000F0)     ; 6
  65.     $hBrush[$enumBrushRed] = _GDIPlus_BrushCreateSolid(0xFFF00000)        ; 7
  66. EndFunc
  67.  
  68. Func PenInit()
  69.     $hPen[$enumPenGuiFrame] = _GDIPlus_PenCreate(0xFF008080 , 3)  ; Width = 3
  70.     $hPen[$enumPenBoardFrame] = _GDIPlus_PenCreate(0xFF000000, 3)  ; Width = 3
  71. EndFunc
  72.  
  73. Func DrawBoard()
  74.     If $debug Then ConsoleWrite("[BOARD] Drawing board" & @CRLF)
  75.     For $y = 0 To $height - 1
  76.         For $x = 0 To $width - 1
  77.             DrawBlock($x, $y, $board[$y * $width + $x])
  78.             ; _GDIPlus_GraphicsFillRect($hGraphics, $x * $scale, $y * $scale, $scale, $scale, $hBrush[$board[$y * $width + $x]])
  79.         Next
  80.     Next
  81.     If $debug Then ConsoleWrite("[BOARD] Drawing board | SUCCESS" & @CRLF)
  82. EndFunc
  83.  
  84. Func DrawPiece()
  85.     If $debug Then ConsoleWrite("[BOARD] Drawing piece" & @CRLF)
  86.     If $piece[$enumPieceFalling] Then  ; Do draw if piece is falling
  87.         Local $l = $pieces[$piece[$enumPieceType]][0][0]  ; Piece side length
  88.         For $y = 0 To $l - 1
  89.             For $x = 0 To $l - 1
  90.                 If $aPiece[$y][$x] <> 0 Then  ; Do draw only when the block isn't empty
  91.                     DrawBlock($piece[$enumPieceX] + $x, $piece[$enumPieceY] + $y, $piece[$enumPieceType] + 1)
  92.                     If $debug Then ConsoleWrite("[BOARD] Drawing piece | SUCCESS PIECE: $aPiece[" & $y & "][" & $x & "] COLOR: " & $piece[$enumPieceType] + 1 & @CRLF)
  93.                 EndIf
  94.             Next
  95.         Next
  96.     EndIf
  97. EndFunc
  98.  
  99. Func DrawGuiFrame()
  100.     Local $guiSize = WinGetClientSize($hGui)
  101.     _GDIPlus_GraphicsDrawLine($hGraphics, 1, 1, $guiSize[0] - 1, 1, $hPen[$enumPenGuiFrame])
  102.     _GDIPlus_GraphicsDrawLine($hGraphics, 1, 1, 1, $guiSize[0] - 1, $hPen[$enumPenGuiFrame])
  103.     _GDIPlus_GraphicsDrawLine($hGraphics, $guiSize[0] - 1, 1, $guiSize[0] - 1, 1, $hPen[$enumPenGuiFrame])
  104.     _GDIPlus_GraphicsDrawLine($hGraphics, 1, $guiSize[1] - 1, $guiSize[0] - 1, $guiSize[1] - 1, $hPen[$enumPenGuiFrame])
  105. EndFunc
  106.  
  107. Func DrawBoardFrame()
  108.     For $c = 0 To $width
  109.         _GDIPlus_GraphicsDrawLine($hGraphics, $offsetBoard[0] + $c * ($scale + 3) + 1, $offsetBoard[1] + 1, $offsetBoard[0] + $c * ($scale + 3) + 1, $offsetBoard[1] + $height * ($scale + 3) + 1, $hPen[$enumPenBoardFrame])
  110.     Next
  111.     For $r = 0 To $height
  112.         _GDIPlus_GraphicsDrawLine($hGraphics, $offsetBoard[0] + 1, $offsetBoard[1] + $r * ($scale + 3) + 1, $offsetBoard[0] + $width * ($scale + 3) + 1, $offsetBoard[1] + $r * ($scale + 3) + 1, $hPen[$enumPenBoardFrame])
  113.     Next
  114. EndFunc
  115.  
  116. Func DrawBlock($blockX, $blockY, $color)
  117.     _GDIPlus_GraphicsFillRect($hGraphics, $offsetBoard[0] + 3 * ($blockX + 1) + $blockX * $scale, _  ; <== Drawing X pos
  118.         $offsetBoard[1] + 3 * ($blockY + 1) + $blockY * $scale, _  ; <== Drawing Y pos
  119.         $scale, $scale, $hBrush[$color])
  120. EndFunc
  121.  
  122. Func PieceTryRotate()
  123.     If $debug Then ConsoleWrite("[PIECE] Trying to rotate CURRENT: " & $piece[$enumPieceRotation] & @CRLF)
  124.     If $piece[$enumPieceRotation] < 3 Then
  125.         Local $phPieceRotation = $piece[$enumPieceRotation] + 1
  126.         RotatePiece($piece[$enumPieceType], $phPieceRotation, 1)
  127.         If Not CheckCollision($piece[$enumPieceX], $piece[$enumPieceY], 1) Then
  128.             $piece[$enumPieceRotation] += 1
  129.             RotatePiece($piece[$enumPieceType], $phPieceRotation)
  130.             If $debug Then ConsoleWrite("[PIECE] Trying to rotate SUCCESS NEW: " & $piece[$enumPieceRotation] & @CRLF)
  131.         EndIf
  132.     Else
  133.         RotatePiece($piece[$enumPieceType], 0, 1)
  134.         If Not CheckCollision($piece[$enumPieceX], $piece[$enumPieceY], 1) Then
  135.             $piece[$enumPieceRotation] = 0
  136.             RotatePiece($piece[$enumPieceType], 0)
  137.             If $debug Then ConsoleWrite("[PIECE] Trying to rotate SUCCESS NEW: " & $piece[$enumPieceRotation] & @CRLF)
  138.         EndIf
  139.     EndIf
  140. EndFunc
  141.  
  142. Func PieceTryGoLeft()
  143.     If $debug Then ConsoleWrite("[PIECE] Trying to go left" & @CRLF)
  144.     If Not CheckCollision($piece[$enumPieceX] - 1, $piece[$enumPieceY]) Then
  145.         $piece[$enumPieceX] -= 1
  146.         If $debug Then ConsoleWrite("[PIECE] Trying to go left | SUCCESS" & @CRLF)
  147.     EndIf
  148. EndFunc
  149.  
  150. Func PieceTryGoRight()
  151.     If $debug Then ConsoleWrite("[PIECE] Trying to go right" & @CRLF)
  152.     If Not CheckCollision($piece[$enumPieceX] + 1, $piece[$enumPieceY]) Then
  153.         $piece[$enumPieceX] += 1
  154.         If $debug Then ConsoleWrite("[PIECE] Trying to go right | SUCCESS" & @CRLF)
  155.     EndIf
  156. EndFunc
  157.  
  158. Func PieceTryGoDown()
  159.     If $debug Then ConsoleWrite("[PIECE] Trying to go down" & @CRLF)
  160.     If Not CheckCollision($piece[$enumPieceX], $piece[$enumPieceY] + 1) Then
  161.         $piece[$enumPieceY] += 1
  162.         If $debug Then ConsoleWrite("[PIECE] Trying to go down | SUCCESS" & @CRLF)
  163.     Else
  164.         SetPieceDropped()
  165.     EndIf
  166. EndFunc
  167.  
  168. Func GenerateNewPiece($pieceType = -1)
  169.     If $debug Then ConsoleWrite("[MAIN] Generating new piece" & @CRLF)
  170.     $piece[$enumPieceFalling] = True
  171.     If $pieceType = -1 Then $piece[$enumPieceType] = Random(0, 6, 1)
  172.     $piece[$enumPieceX] = 0
  173.     $piece[$enumPieceY] = 0
  174.     $piece[$enumPieceRotation] = 0
  175.     SetPieceArray($piece[$enumPieceType])
  176.     SetPieceArray($piece[$enumPieceType], 1)
  177.     If $debug Then ConsoleWrite("[MAIN] Generated new piece | PIECETYPE: " & $piece[$enumPieceType] & @CRLF)
  178. EndFunc
  179.  
  180. Func SetPieceArray(ByRef $pieceType, $isTryingToRotate = 0)
  181.     Local $l = $pieces[$pieceType][0][0]
  182.     If $isTryingToRotate Then
  183.         ReDim $aPieceRotatingPH[$l][$l]
  184.         For $y = 0 To $l - 1
  185.             For $x = 0 To $l - 1
  186.                 $aPieceRotatingPH[$y][$x] = $pieces[$pieceType][$y + 1][$x]
  187.             Next
  188.         Next
  189.     Else
  190.         ReDim $aPiece[$l][$l]
  191.         For $y = 0 To $l - 1
  192.             For $x = 0 To $l - 1
  193.                 $aPiece[$y][$x] = $pieces[$pieceType][$y + 1][$x]
  194.             Next
  195.         Next
  196.     EndIf
  197. EndFunc
  198.  
  199. Func SetPieceDropped()
  200.     If $debug Then ConsoleWrite("[PIECE] Setting dropped" & @CRLF)
  201.     $piece[$enumPieceFalling] = False
  202.     Local $l = $pieces[$enumPieceType][0][0]
  203.     Local $boardPos = 0
  204.     For $y = 0 To $l - 1
  205.         For $x = 0 To $l - 1
  206.             ConsoleWrite("[PIECE] Setting dropped | CHECKING PIECE: $aPiece["& $y & "][" & $x & "]" & @CRLF)
  207.             If $aPiece[$y][$x] <> 0 Then
  208.                 $boardPos = ($piece[$enumPieceY] + $y) * $width + $piece[$enumPieceX] + $x
  209.                 $board[$boardPos] = $piece[$enumPieceType] + 1
  210.                 If $debug Then ConsoleWrite("[PIECE] Setting dropped | SUCCESS PIECE: $aPiece["& $y & "][" & $x & "] TO BOARD: $board[" & $boardPos & "]" & @CRLF)
  211.             EndIf
  212.         Next
  213.     Next
  214.     If $debug Then ConsoleWrite("[PIECE] Setting dropped | SUCCESS ALL" & @CRLF)
  215.     RemoveFullLines()
  216. EndFunc
  217.  
  218. Func RemoveFullLines()
  219.     If $debug Then ConsoleWrite("[PIECE] Removing full lines" & @CRLF)
  220.     Local $numFullLines = 0
  221.     Local $lineIsFull = True
  222.     For $y = $height - 1 To 0 Step -1
  223.         $lineIsFull = True
  224.         For $x = 0 To $width - 1
  225.             If $board[$y * $width + $x] = 0 Then $lineIsFull = False
  226.             ExitLoop
  227.         Next
  228.         If $lineIsFull Then
  229.             For $x = 0 To $width - 1
  230.                 If $y = 0 Then
  231.                     $board[$y * $width + $x] = 0
  232.                 Else
  233.                     $board[$y * $width + $x] = $board[($y - 1) * $width + $x]
  234.                 EndIf
  235.             Next
  236.             If $debug Then ConsoleWrite("[PIECE] Removing full lines | SUCCESS LINE: " & $y + 1 & @CRLF)
  237.         EndIf
  238.     Next
  239. EndFunc
  240.  
  241. Func RotatePiece(ByRef $pieceType, $rotation, $isTryingToRotate = 0)
  242.     Local $l = $pieces[$pieceType][0][0]
  243.     If $isTryingToRotate Then
  244.         Switch $rotation
  245.             Case 0
  246.                 SetPieceArray($pieceType, 1)
  247.             Case 1  ; Rotate clockwise
  248.                 For $y = 0 To $l - 1
  249.                     For $x = 0 To $l - 1
  250.                         $aPieceRotatingPH[$x][$l - 1 - $y] = $pieces[$pieceType][$y + 1][$x]
  251.                     Next
  252.                 Next
  253.             Case 2  ; Rotate clockwise twice
  254.                 For $y = 0 To $l - 1
  255.                     For $x = 0 To $l - 1
  256.                         $aPieceRotatingPH[$l - 1 - $y][$l - 1 - $x] = $pieces[$pieceType][$y + 1][$x]
  257.                     Next
  258.                 Next
  259.             Case 3  ; Rotate counter clockwise
  260.                 For $y = 0 To $l - 1
  261.                     For $x = 0 To $l - 1
  262.                         $aPieceRotatingPH[$l - 1 - $x][$y] = $pieces[$pieceType][$y + 1][$x]
  263.                     Next
  264.                 Next
  265.         EndSwitch
  266.     Else
  267.         Switch $rotation
  268.             Case 0
  269.                 SetPieceArray($pieceType)
  270.             Case 1  ; Rotate clockwise
  271.                 For $y = 0 To $l - 1
  272.                     For $x = 0 To $l - 1
  273.                         $aPiece[$x][$l - 1 - $y] = $pieces[$pieceType][$y + 1][$x]
  274.                     Next
  275.                 Next
  276.             Case 2  ; Rotate clockwise twice
  277.                 For $y = 0 To $l - 1
  278.                     For $x = 0 To $l - 1
  279.                         $aPiece[$l - 1 - $y][$l - 1 - $x] = $pieces[$pieceType][$y + 1][$x]
  280.                     Next
  281.                 Next
  282.             Case 3  ; Rotate counter clockwise
  283.                 For $y = 0 To $l - 1
  284.                     For $x = 0 To $l - 1
  285.                         $aPiece[$l - 1 - $x][$y] = $pieces[$pieceType][$y + 1][$x]
  286.                     Next
  287.                 Next
  288.         EndSwitch
  289.     EndIf
  290. EndFunc
  291.  
  292. Func CheckCollision($startX, $startY, $isTryingToRotate = 0)
  293.     If $debug Then ConsoleWrite("[PIECE] Checking collision | ISTRYINGTOROTATE: " & $isTryingToRotate & @CRLF)
  294.     Local $l = $pieces[$piece[$enumPieceType]][0][0]
  295.     If $isTryingToRotate Then
  296.         For $y = 0 To $l - 1
  297.             For $x = 0 To $l - 1
  298.                 If $aPieceRotatingPH[$y][$x] <> 0 Then
  299.                     If $startY + $y > $height - 1 Or _
  300.                         $startX + $x < 0 Or _
  301.                         $startX + $x > $width - 1 Or _
  302.                         $board[($startY + $y) * $width + ($startX + $x)] <> 0 _
  303.                     Then
  304.                         If $debug Then ConsoleWrite("[PIECE] Checking collision | SUCCESS ISTRYINGTOROTATE: 1 COLLIDE: 1" & @CRLF)
  305.                         Return 1
  306.                     EndIf
  307.                 EndIf
  308.             Next
  309.         Next
  310.     Else
  311.         For $y = 0 To $l - 1
  312.             For $x = 0 To $l - 1
  313.                 If $aPiece[$y][$x] <> 0 Then
  314.                     If $startY + $y > $height - 1 Or _
  315.                         $startX + $x < 0 Or _
  316.                         $startX + $x > $width - 1 Or _
  317.                         $board[($startY + $y) * $width + ($startX + $x)] <> 0 _
  318.                     Then
  319.                         If $debug Then ConsoleWrite("[PIECE] Checking collision | SUCCESS ISTRYINGTOROTATE: 0 COLLIDE: 1" & @CRLF)
  320.                         Return 1
  321.                     EndIf
  322.                 EndIf
  323.             Next
  324.         Next
  325.     EndIf
  326.     If $debug Then ConsoleWrite("[PIECE] Checking collision | SUCCESS ISTRYINGTOROTATE: " & $isTryingToRotate & " COLLIDE: 0" & @CRLF)
  327.     Return 0
  328. EndFunc
  329.  
  330. Func VarDump()
  331.     Local $cout = ""
  332.     $cout &= "$aPiece=["
  333.     For $i = 0 To UBound($aPiece) - 1
  334.         $cout &= "["
  335.         For $j = 0 To UBound($aPiece, 2) - 1
  336.             $cout &= $aPiece[$i][$j]
  337.             If $j < UBound($aPiece, 2) - 1 Then $cout &= ","
  338.         Next
  339.         $cout &= "]"
  340.         If $i < UBound($aPiece) - 1 Then $cout &= ","
  341.     Next
  342.     $cout &= "]"
  343.     $cout &= @CRLF & "$board=[" & @CRLF
  344.     For $i = 0 To UBound($board) - 1
  345.         $cout &= $board[$i]
  346.         If $i < UBound($board) - 1 Then $cout &= ","
  347.         If Mod($i, $width) = 9 Then $cout &= @CRLF
  348.     Next
  349.     $cout &= "]"
  350.     ConsoleWrite("[MAIN] Array dump | " & $cout & @CRLF)
  351. EndFunc
  352.  
  353. ; ==================================================
  354. ; Main part
  355. ; ==================================================
  356.  
  357. Func Main()
  358.     _GDIPlus_Startup()
  359.     $hGui = GUICreate("Autoit Tetris by Alan72104", 500, 1000, Default, Default, Default)  ; , $WS_EX_TOPMOST)
  360.     GUISetBkColor($bgColor, $hGui)
  361.     GUISetState(@SW_SHOW)
  362.     $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
  363.     BrushInit()
  364.     ReDim $board[$width * $height]  ; Resize the board array
  365.     For $y = 0 To $height - 1
  366.         For $x = 0 To $width - 1
  367.             $board[$y * $width + $x] = 0
  368.         Next
  369.     Next
  370.     DrawGuiFrame()
  371.     DrawBoardFrame()
  372.     GenerateNewPiece()
  373.     Do
  374.         DrawBoard()
  375.         DrawPiece()
  376.         If $piece[$enumPieceFalling] Then
  377.             PieceTryGoDown()
  378.         Else
  379.             GenerateNewPiece()
  380.         EndIf
  381.         ; If $debug Then VarDump()
  382.         Sleep(300)
  383.     Until GUIGetMsg() = $GUI_EVENT_CLOSE
  384.     GdiPlusClose()
  385.     GUIDelete($hGUI)
  386. EndFunc
  387.  
  388. Main()
  389.  
  390. ; ==================================================
  391. ; Pausing parts
  392. ; ==================================================
  393.  
  394. Func GdiPlusClose()
  395.     For $i  = 0 To UBound($hBrush) - 1
  396.         _GDIPlus_BrushDispose($hBrush[$i])
  397.     Next
  398.     For $i  = 0 To UBound($hPen) - 1
  399.         _GDIPlus_PenDispose($hPen[$i])
  400.     Next
  401.     _GDIPlus_GraphicsDispose($hGraphics)
  402.     _GDIPlus_Shutdown()
  403. EndFunc
  404.  
  405. Func Terminate()
  406.     GdiPlusClose()
  407.     GUIDelete($hGUI)
  408.     Exit 0
  409. EndFunc
  410.  
  411. Func TogglePause()
  412.     $g_bPaused = Not $g_bPaused
  413.     While $g_bPaused
  414.         Sleep (500)
  415.         ToolTip ('Script is "Paused"', @desktopWidth / 2, @desktopHeight / 2, Default, Default, $TIP_CENTER)
  416.     WEnd
  417.     ToolTip("")
  418. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement