Advertisement
Guest User

Tetris

a guest
Jan 17th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 11.79 KB | None | 0 0
  1.  
  2.  
  3. #include "au3Irrlicht2.au3"
  4. #Include <Timers.au3>
  5.  
  6.  
  7.  
  8. $title="Tetris Beta v_1.0 by CheapBrain"
  9. $x=@DesktopWidth/2-200
  10. $y=@DesktopHeight/2-200
  11. $gridx=120+$x
  12. $gridy=27+$y
  13.  
  14. _IrrStart($IRR_EDT_SOFTWARE,@DesktopWidth,@DesktopHeight,$IRR_BITS_PER_PIXEL_16,$IRR_FULLSCREEN,$IRR_NO_SHADOWS,$IRR_CAPTURE_EVENTS,$IRR_VERTICAL_SYNC_OFF)
  15. _IrrSetWindowCaption($title)
  16.  
  17. local $bitmapFont = _IrrGetFont ( @ScriptDir&"\font1.png" )
  18. local $logo = _IrrGetTexture(@ScriptDir&"\title.bmp")
  19. local $background = _IrrGetTexture(@ScriptDir&"\ingame.bmp")
  20. local $texture = _IrrGetTexture(@ScriptDir&"\texture.bmp")
  21. local $sprite[4][3], $tileset[10][24]
  22.  
  23. _IrrBeginScene(0, 0, 0)
  24. _Irr2DFontDraw ( $BitmapFont, "Loading...", @DesktopWidth/2-30, @DesktopHeight/2-10, 10, 10 )
  25. _IrrEndScene()
  26.  
  27.  
  28.  
  29.  
  30. HotKeySet ( "{UP}" , "_up" )
  31. HotKeySet ( "{DOWN}" , "_down" )
  32. HotKeySet ( "{RIGHT}" , "_right" )
  33. HotKeySet ( "{LEFT}" , "_left" )
  34. HotKeySet ( "{ENTER}" , "_enter" )
  35. HotKeySet ( "{SPACE}" , "_space" )
  36.  
  37.  
  38. _menu()
  39.  
  40.  
  41.     func _menu()
  42.     Local   $timer=TimerInit()
  43.     local   $count=0
  44.     local   $fps=0
  45.     local   $mouse=0
  46.     local   $mousex=0
  47.     local   $mousey=0
  48.     global  $enter=0
  49.     global  $up=0
  50.     global  $down=0
  51.     global  $right=0
  52.     global  $left=0
  53.     global  $space=0
  54.     global  $key=0
  55.  
  56.     While 1
  57.  
  58.         if not WinActive($title) then _sleep()
  59.  
  60.         _IrrBeginScene(18, 44, 77)
  61.             _IrrDraw2DImage( $logo, $x, $y )
  62.             _Irr2DFontDraw ( $BitmapFont, "   x:"&$mousex&@CRLF&"   y:"&$mousey&@CRLF&"   click:"&$mouse&@CRLF&"   FPS:"&$fps, 0, 0, 10, 10 )
  63.  
  64.         while _IrrMouseEventAvailable()
  65.             $pMouseEvent = _IrrReadMouseEvent()
  66.             $mousex=__getMouseEvt($pMouseEvent, $EVT_MOUSE_IX)-$x
  67.             $mousey=__getMouseEvt($pMouseEvent, $EVT_MOUSE_IY)-$y
  68.  
  69.             if __getMouseEvt($pMouseEvent, $EVT_MOUSE_IACTION)=$IRR_EMIE_LMOUSE_PRESSED_DOWN then
  70.                 $mouse=1
  71.             Else
  72.                 $mouse=0
  73.             endif
  74.  
  75.  
  76.             if $mouse=1 Then
  77.                 if $mousex>139 And $mousex<278 And $mousey>356 and $mousey<400 then
  78.                     _IrrEndScene()
  79.                     _stop()
  80.  
  81.                 ElseIf $mousex>105 And $mousex<293 And $mousey>171 and $mousey<213 then
  82.                     _IrrEndScene()
  83.                     _game()
  84.  
  85.                 ElseIf $mousex>105 And $mousex<293 And $mousey>238 and $mousey<280 then
  86.                     _IrrEndScene()
  87.                     _high()
  88.  
  89.                 ElseIf $mousex>7 And $mousex<38 And $mousey>365 and $mousey<396 then
  90.                     _IrrEndScene()
  91.                     _help()
  92.  
  93.                 ElseIf $mousex>46 And $mousex<76 And $mousey>365 and $mousey<396 then
  94.                     _IrrEndScene()
  95.                     _sett()
  96.  
  97.                 endif
  98.  
  99.             EndIf
  100.  
  101.         wend
  102.  
  103.         _IrrEndScene()
  104.  
  105.         $count+=1
  106.         if $count=20 Then
  107.             $fps=Round(20000/TimerDiff($timer),0)
  108.             $timer=TimerInit()
  109.             $count=0
  110.         EndIf
  111.  
  112.     WEnd
  113.  
  114. endfunc
  115.  
  116. func _game()
  117.  
  118.     local   $binfo="10101010001010110001011100101110000111100010110100111100"
  119.     Local   $block[7][2][4]
  120.     for $dh45=0 to 6
  121.     for $dh50=0 to 3
  122.     for $dh55=0 to 1
  123.         $block[$dh45][$dh55][$dh50]=stringmid($binfo,8*$dh45+$dh50*2+$dh55+1,1)
  124.  
  125.     Next
  126. Next
  127. Next
  128.  
  129.     Local   $sprite[3]
  130.     Local   $ran1=10
  131.     Local   $ran2=10
  132.     for $dh45=0 to 2
  133.         while $ran1=$ran2
  134.         $ran2=Random(0,6,1)
  135.     wend
  136.     $ran1=$ran2
  137.         $sprite[$dh45]=$ran1
  138.  
  139.     Next
  140.  
  141.     Local   $tileset[11][24]
  142.     For $dh45=0 to 10
  143.         for $dh50=0 to 23
  144.             $tileset[$dh45][$dh50]=0
  145.         Next
  146.     Next
  147.  
  148.     Local   $timer=TimerInit()
  149.     Local   $autodown= TimerInit()
  150.     Local   $interr=1000
  151.     Local   $count=0
  152.     Local   $fps=0
  153.     global  $enter=0
  154.     global  $up=0
  155.     global  $down=0
  156.     global  $right=0
  157.     global  $left=0
  158.     global  $space=0
  159.     global  $key=0
  160.     global  $posx=64
  161.     global  $posy=-64
  162.     Local   $spritecoord[4][2]
  163.     Local   $spritecoord2[4][2]
  164.     Local   $rotate=0
  165.     $count45=0
  166. for $dh45=0 to 3
  167.     for $dh50=0 to 1
  168.         if $block[$sprite[0]][$dh50][$dh45]=1 Then
  169.             $spritecoord[$count45][0]=$dh50*16
  170.             $spritecoord[$count45][1]=$dh45*16
  171.             $count45+=1
  172.         EndIf
  173.  
  174.     Next
  175. Next
  176.  
  177.  
  178.     while 1
  179.  
  180.  
  181.         _IrrBeginScene(18, 44, 77)
  182.             _Irr2DFontDraw ( $BitmapFont, "game"&@CRLF&"   FPS:"&$fps, 0, 0, 10, 10 )
  183.             _IrrDraw2DImage($background,$x,$y)
  184.             _IrrDraw2DImageElement($texture, $gridx+$posx+$spritecoord[0][0], $gridy+$posy+$spritecoord[0][1], $sprite[0]*16, 0, $sprite[0]*16+16, 16,$IRR_USE_ALPHA)
  185.             _IrrDraw2DImageElement($texture, $gridx+$posx+$spritecoord[1][0], $gridy+$posy+$spritecoord[1][1], $sprite[0]*16, 0, $sprite[0]*16+16, 16,$IRR_USE_ALPHA)
  186.             _IrrDraw2DImageElement($texture, $gridx+$posx+$spritecoord[2][0], $gridy+$posy+$spritecoord[2][1], $sprite[0]*16, 0, $sprite[0]*16+16, 16,$IRR_USE_ALPHA)
  187.             _IrrDraw2DImageElement($texture, $gridx+$posx+$spritecoord[3][0], $gridy+$posy+$spritecoord[3][1], $sprite[0]*16, 0, $sprite[0]*16+16, 16,$IRR_USE_ALPHA)
  188.  
  189.             For $dh45=1 to 10
  190.  
  191.                 For $dh50=4 to 23
  192.  
  193.                     if $tileset[$dh45][$dh50]<>0 Then
  194.                         _IrrDraw2DImageElement($texture, $gridx+($dh45-1)*16, $gridy+($dh50-4)*16, ($tileset[$dh45][$dh50]-1)*16, 0, ($tileset[$dh45][$dh50]-1)*16+16, 16,$IRR_USE_ALPHA)
  195.  
  196.                     EndIf
  197.  
  198.  
  199.                 Next
  200.             Next
  201.  
  202.  
  203.         If $key=1 Then
  204.             $key=0
  205.             if $enter=1 Then
  206.                 $enter=0
  207.                 _pause()
  208.             ElseIf $left=1 then
  209.  
  210.                 if $posx-16+$spritecoord[0][0]>-1 And $posx-16+$spritecoord[1][0]>-1 And $posx-16+$spritecoord[2][0]>-1 And $posx-16+$spritecoord[3][0]>-1 and $tileset[($posx+$spritecoord[0][0])/16][($posy+$spritecoord[0][1])/16+4]+$tileset[($posx+$spritecoord[1][0])/16][($posy+$spritecoord[1][1])/16+4]+$tileset[($posx+$spritecoord[2][0])/16][($posy+$spritecoord[2][1])/16+4]+$tileset[($posx+$spritecoord[3][0])/16][($posy+$spritecoord[3][1])/16+4]=0 then
  211.                     $posx-=16
  212.  
  213.                 endif
  214.  
  215.                 $left=0
  216.  
  217.             ElseIf $right=1 then
  218.  
  219.                 if $posx+16+$spritecoord[0][0]<160 And $posx+16+$spritecoord[1][0]<160 And $posx+16+$spritecoord[2][0]<160 And $posx+16+$spritecoord[3][0]<160 and $tileset[($posx+$spritecoord[0][0])/16+2][($posy+$spritecoord[0][1])/16+4]+$tileset[($posx+$spritecoord[1][0])/16+2][($posy+$spritecoord[1][1])/16+4]+$tileset[($posx+$spritecoord[2][0])/16+2][($posy+$spritecoord[2][1])/16+4]+$tileset[($posx+$spritecoord[3][0])/16+2][($posy+$spritecoord[3][1])/16+4]=0 then
  220.                     $posx+=16
  221.  
  222.                 endif
  223.  
  224.                 $right=0
  225.             ElseIf $up=1 then
  226.  
  227.                 $up=0
  228.             ElseIf $down=1 then
  229.  
  230.                 if $posy+16+$spritecoord[0][1]<320 and $posy+16+$spritecoord[1][1]<320 and $posy+16+$spritecoord[2][1]<320 and $posy+16+$spritecoord[3][1]<320 and $tileset[($posx+$spritecoord[0][0])/16+1][($posy+$spritecoord[0][1])/16+5]+$tileset[($posx+$spritecoord[1][0])/16+1][($posy+$spritecoord[1][1])/16+5]+$tileset[($posx+$spritecoord[2][0])/16+1][($posy+$spritecoord[2][1])/16+5]+$tileset[($posx+$spritecoord[3][0])/16+1][($posy+$spritecoord[3][1])/16+5]=0 then
  231.                     $posy+=16
  232.  
  233.                 endif
  234.  
  235.                 $down=0
  236.  
  237.         elseif $space=1 Then
  238.             $rotate+=1
  239.  
  240.             if $rotate=4 then $rotate=0
  241.  
  242. $outgrid=0
  243. $tempx=0
  244. $tempy=0
  245. for $dh45=0 to 3
  246.     for $dh50=0 to 1
  247.         if $block[$sprite[0]][$dh50][$dh45]=1 Then
  248.             if $rotate=0 Then
  249.  
  250.             $tempx=$dh50*16
  251.             $tempy=$dh45*16
  252.         elseif $rotate=1 Then
  253.             $tempx=32-$dh45*16
  254.             $tempy=$dh50*16+16
  255.         elseif $rotate= 2 Then
  256.             $tempx=16-$dh50*16
  257.             $tempy=48-$dh45*16
  258.         Elseif $rotate= 3 then
  259.             $tempx=$dh45*16-16
  260.             $tempy=32-$dh50*16
  261.         EndIf
  262.  
  263.             if $posx+$tempx<0 or $posx+$tempx>159 or $posy+$tempy>319  then
  264.                 $outgrid=1
  265.                 $rotate-=1
  266.  
  267.                 if $rotate=-1 then $rotate=3
  268.  
  269.             ElseIf  $tileset[($posx+$tempx)/16+1][($posy+$tempy)/16+4]<>0 Then
  270.                 $outgrid=1
  271.                 $rotate-=1
  272.  
  273.                 if $rotate=-1 then $rotate=3
  274.  
  275.             EndIf
  276.  
  277.  
  278.         EndIf
  279.  
  280.     Next
  281. Next
  282.  
  283. if $outgrid=0 then
  284.             $count45=0
  285. for $dh45=0 to 3
  286.     for $dh50=0 to 1
  287.         if $block[$sprite[0]][$dh50][$dh45]=1 Then
  288.             if $rotate=0 Then
  289.  
  290.             $spritecoord[$count45][0]=$dh50*16
  291.             $spritecoord[$count45][1]=$dh45*16
  292.         elseif $rotate=1 Then
  293.             $spritecoord[$count45][0]=32-$dh45*16
  294.             $spritecoord[$count45][1]=$dh50*16+16
  295.         elseif $rotate= 2 Then
  296.             $spritecoord[$count45][0]=16-$dh50*16
  297.             $spritecoord[$count45][1]=48-$dh45*16
  298.         Elseif $rotate= 3 then
  299.             $spritecoord[$count45][0]=$dh45*16-16
  300.             $spritecoord[$count45][1]=32-$dh50*16
  301.         EndIf
  302.  
  303.             $count45+=1
  304.         EndIf
  305.  
  306.     Next
  307. Next
  308.  
  309. EndIf
  310.  
  311.  
  312.                 $space=0
  313.  
  314.  
  315.             EndIf
  316.  
  317.         EndIf
  318.  
  319.         _IrrEndScene()
  320.  
  321.         $count+=1
  322.         if $count=20 Then
  323.             $fps=Round(20000/TimerDiff($timer),0)
  324.             $timer=TimerInit()
  325.             $count=0
  326.         EndIf
  327.  
  328.         if TimerDiff($autodown)>$interr Then
  329.             $autodown= TimerInit()
  330.  
  331.             if $posy+16+$spritecoord[0][1]<320 and $posy+16+$spritecoord[1][1]<320 and $posy+16+$spritecoord[2][1]<320 and $posy+16+$spritecoord[3][1]<320 and $tileset[($posx+$spritecoord[0][0])/16+1][($posy+$spritecoord[0][1])/16+5]+$tileset[($posx+$spritecoord[1][0])/16+1][($posy+$spritecoord[1][1])/16+5]+$tileset[($posx+$spritecoord[2][0])/16+1][($posy+$spritecoord[2][1])/16+5]+$tileset[($posx+$spritecoord[3][0])/16+1][($posy+$spritecoord[3][1])/16+5]=0 then
  332.                 $posy+=16
  333.  
  334.             Else
  335.  
  336.                 $tileset[($posx+$spritecoord[0][0])/16+1][($posy+$spritecoord[0][1])/16+4]=$sprite[0]+1
  337.                     $tileset[($posx+$spritecoord[1][0])/16+1][($posy+$spritecoord[1][1])/16+4]=$sprite[0]+1
  338.                     $tileset[($posx+$spritecoord[2][0])/16+1][($posy+$spritecoord[2][1])/16+4]=$sprite[0]+1
  339.                     $tileset[($posx+$spritecoord[3][0])/16+1][($posy+$spritecoord[3][1])/16+4]=$sprite[0]+1
  340.                     $tileset[0][($posy+$spritecoord[0][1])/16+4]+=1
  341.                     $tileset[0][($posy+$spritecoord[1][1])/16+4]+=1
  342.                     $tileset[0][($posy+$spritecoord[2][1])/16+4]+=1
  343.                     $tileset[0][($posy+$spritecoord[3][1])/16+4]+=1
  344.                     for $dh55=4 to 23
  345.                     if $tileset[0][$dh55]=10 Then
  346.                         For $dh45=$dh55 to 1 step -1
  347.                             for $dh50=0 to 10
  348.                             $tileset[$dh50][$dh45]=$tileset[$dh50][$dh45-1]
  349.                             next
  350.                         Next
  351.  
  352.                     EndIf
  353.  
  354.                     next
  355.                     if $tileset[0][3]>0 or $tileset[0][2]>0 then _menu()
  356.                     $sprite[0]=$sprite[1]
  357.                     $sprite[1]=$sprite[2]
  358.  
  359.                     while $ran1=$ran2
  360.                         $ran2=Random(0,6,1)
  361.  
  362.                     wend
  363.  
  364.                     $ran1=$ran2
  365.                     $sprite[2]=$ran1
  366.  
  367.                     $count45=0
  368.                     for $dh45=0 to 3
  369.                         for $dh50=0 to 1
  370.                             if $block[$sprite[0]][$dh50][$dh45]=1 Then
  371.                                 $spritecoord[$count45][0]=$dh50*16
  372.                                 $spritecoord[$count45][1]=$dh45*16
  373.                                 $count45+=1
  374.                             EndIf
  375.  
  376.                         Next
  377.                     Next
  378.  
  379.                     $posx=64
  380.                     $posy=-64
  381.                     $rotate=0
  382.  
  383.  
  384.  
  385.  
  386.             EndIf
  387.  
  388.  
  389.         EndIf
  390.  
  391.     wend
  392.  
  393. endfunc
  394.  
  395. func _high()
  396.     while 1
  397.         _IrrBeginScene(18, 44, 77)
  398.             _Irr2DFontDraw ( $BitmapFont, "high", 0, 0, 10, 10 )
  399.  
  400.         _IrrEndScene()
  401.  
  402.     wend
  403.  
  404. endfunc
  405.  
  406.  
  407. func _sett()
  408.     while 1
  409.         _IrrBeginScene(18, 44, 77)
  410.             _Irr2DFontDraw ( $BitmapFont, "sett", 0, 0, 10, 10 )
  411.         _IrrEndScene()
  412.  
  413.     wend
  414.  
  415. endfunc
  416.  
  417.  
  418. func _help()
  419.     while 1
  420.         _IrrBeginScene(18, 44, 77)
  421.             _Irr2DFontDraw ( $BitmapFont, "help", 0, 0, 10, 10 )
  422.  
  423.         _IrrEndScene()
  424.  
  425.     wend
  426.  
  427. endfunc
  428.  
  429. func _stop()
  430.     _IrrStop()
  431.     exit
  432.  
  433. EndFunc
  434.  
  435. func _sleep()
  436.  
  437.     while WinActive($title)<>true
  438.         sleep(500)
  439.  
  440.     wend
  441.  
  442. endfunc
  443.  
  444. func _pause()
  445.  
  446.     $wait1=true
  447.     while $wait1
  448.  
  449.         _IrrBeginScene(18, 44, 77)
  450.             _Irr2DFontDraw ( $BitmapFont, "pause", 0,0, 10, 10 )
  451.             _Irr2DFontDraw ( $BitmapFont, "Back", $x+180,$y+50, 10, 10 )
  452.             _Irr2DFontDraw ( $BitmapFont, "Menu", $x+180,$y+100, 10, 10 )
  453.             _Irr2DFontDraw ( $BitmapFont, "Exit", $x+180,$y+150, 10, 10 )
  454.  
  455.  
  456.         while _IrrMouseEventAvailable()
  457.             $pMouseEvent = _IrrReadMouseEvent()
  458.             $mousex=__getMouseEvt($pMouseEvent, $EVT_MOUSE_IX)-$x
  459.             $mousey=__getMouseEvt($pMouseEvent, $EVT_MOUSE_IY)-$y
  460.  
  461.             if __getMouseEvt($pMouseEvent, $EVT_MOUSE_IACTION)=$IRR_EMIE_LMOUSE_PRESSED_DOWN then
  462.                 $mouse=1
  463.  
  464.             Else
  465.                 $mouse=0
  466.  
  467.             endif
  468.  
  469.  
  470.             if $mouse=1 Then
  471.                 if $mousex>180 And $mousex<280 And $mousey>50 and $mousey<75 then
  472.                     $wait1=false
  473.  
  474.                 ElseIf $mousex>180 And $mousex<280 And $mousey>100 and $mousey<125 then
  475.                     _IrrEndScene()
  476.                     _menu()
  477.  
  478.                 ElseIf $mousex>180 And $mousex<280 And $mousey>150 and $mousey<175 then
  479.                     _IrrEndScene()
  480.                     _stop()
  481.  
  482.                 endif
  483.  
  484.             EndIf
  485.  
  486.         wend
  487.  
  488.         _IrrEndScene()
  489.  
  490.     wend
  491.  
  492. endfunc
  493.  
  494. func _up()
  495.     $key=1
  496.     $up=1
  497.  
  498. endfunc
  499.  
  500. func _down()
  501.     $key=1
  502.     $down=1
  503.  
  504. endfunc
  505.  
  506. func _right()
  507.     $key=1
  508.     $right=1
  509.  
  510. endfunc
  511.  
  512. func _left()
  513.     $key=1
  514.     $left=1
  515.  
  516. endfunc
  517.  
  518. func _space()
  519.     $key=1
  520.     $space=1
  521.  
  522. endfunc
  523.  
  524. func _enter()
  525.     $key=1
  526.     $enter=1
  527.  
  528. endfunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement