Shell Write Tetris game

Source: Internet
Author: User

From #! /Bin/sh is the real source code, which can be directly copied to shell. By the way, you can play it without an image interface.


#! /Bin/shaBox0_0 = (0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0) aBox0_1 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0) aBox0_2 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0- 1) aBox0_3 = (0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) abox3160 = (0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0) aBox1_1 = (0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0) aBox1_2 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0- 1) aBox1_3 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0) aBox2_0 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) aBox2_1 = (0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0) aBox3_0 = (0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0) aBox3_1 = (0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0) aBox4_0 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0-1) aBox4_1 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0) aBox5_0 = (0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0) aBox6_0 = (0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0) aBox6_1 = (0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0) aBox6_2 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0- 1) aBox6_3 = (0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) iSumType = 7 # Total number of block types aStyles = (4 4 2 2 2 1 4) # number of square styles of various types iOrigoX = 10 # display start position xiOrigoY = 5 # display start position yiMapWidth = 12 # square width 10 iMapHeight = 15 # square height 15iBox_X = 0 # square position xiBox_Y = 0 # block position YiScore = 0 # Score iLevel = 0 # level iScoreEachLevle = 50 # scores need to be accumulated for each level of upgrade # Initialize all background squares for (I = 0; I <iMapWidth * iMapHeight; I ++) do aBoxBg [$ I] = 0 done # define the control signal and use 25 ~ 31 iSIG = 0 # iSIG_Rotate = 25 # rotate iSIG_Left = 26 # Move iSIG_Right = 27 # Move iSIG_Down to the Right = 28 # Move iSIG_Down iSIG_FallDown = 29 # exit iSIG_Exit = 30 # exit iSIG_Pause = 31 # suspend Control () {trap "Game_Exit" 2 3 15 $ iSIG_Exit echo-ne "\ 033 [? 25l "SAVEDSTTY = 'stty-G' iDisplayPID = $1 while: do read-s-n 1 LINE iSIG = 0 case $ LINE in" A ") iSIG = $ iSIG_Rotate ;; "B") iSIG = $ iSIG_Down; "D") iSIG = $ iSIG_Left; "C") iSIG = $ iSIG_Right; "") iSIG = $ iSIG_FallDown ;; p | P) iSIG = $ iSIG_Pause; q | Q) Game_Exit; esac [$ iSIG-ne 0] & kill-$ iSIG $ iDisplayPID done} Display () {trap "iSIG = $ iSIG_Rotate" $ iSIG_Rotate trap "iSIG = $ iSIG_Down" $ iSIG_Down Trap "iSIG = $ iSIG_Left" $ iSIG_Left trap "iSIG = $ iSIG_Right" $ iSIG_Right trap "iSIG = $ trap" $ trap "iSIG = $ iSIG_Pause" $ iSIG_Pause local iSIGThis I clear draw_info Create_Box while: do if [$ iSIG-ne $ iSIG_Pause]; then for (I = 0; I <20-iLevel; I ++ )) do iSIG_Display = $ iSIG = 0 case $ iSIG_Display in $ iSIG_Rotate) Move_Rotate; $ iSIG_Down) Move_Down; $ iSIG_Left) Move_Left; $ iSIG _ Right) Move_Right; $ iSIG_FallDown) Move_FallDown; $ iSIG_Pause) iSIG = $ iSIG_Pause break; esac sleep 0.02 done Move_Down fi done} Game_Exit () {echo-ne "\ 033 [21; 1 Hgame over \ n" echo-ne "\ 033 [0m \ 033 [? 25 h "stty $ SAVEDSTTY kill-9 $ iDisplayPID exit 0} Draw_Line () {echo-ne" \ 033 [$1; $ (iOrigoX )) H -- ===================================-- "# iOrigoY} Draw_Row () {local I for (I = iOrigoY; I <iOrigoY + 15; I ++) do echo-ne "\ 033 [$ I; $ 1H |" done} # display the program's main screen Draw_info () {echo-ne "\ 033 [45 m" Draw_Line $ (iOrigoY-1) Draw_Line $ (iOrigoY + iMapHeight) Draw_Row $ (iOrigoX )) # iOrigoY + (3*2)-2 Draw_Row $ (iOrigoX + 2 + 20) # iOrigoY + (3*2) + 10*2 echo-ne "\ 033 [0 m" echo-ne "\ 033 [$ (iOrigoY + 8); $ (iOrigoX + 25 )) h score "echo-ne" \ 033 [$ (iOrigoY + 10); $ (iOrigoX + 30 )) h0 "echo-ne" \ 033 [$ (iOrigoY + 13); $ (iOrigoX + 25 )) h level "echo-ne" \ 033 [$ (iOrigoY + 15); $ (iOrigoX + 30) H0 "} Draw_Box_Clear () # Hide the square {local I for (I = 0; I <$ ($ {# aBoxMove [*]}-1); I ++ )) do [$ {aBoxMove [$ I]}-ne 0] & echo-ne "\ 033 [$ (I/12) + iBox_Y + iOrigoY )); $ (I % 12) * 2) + iOrigoX)) H "done} Draw_Box_New () # Show the square {local I for (I = 0; I <$ ($ {# aBoxMove [*]}-1 )); I ++ )) do [$ {aBoxMove [$ I]}-ne 0] & echo-ne "\ 033 [1 m \ 033 [$ {iCurrentColor} m \ 033 [$ (I /12) + iBox_Y + iOrigoY); $ (I % 12) * 2) + iOrigoX) H [] \ 033 [0 m "done} IsBottom () # Check whether the square falls to the end {local I if [$ ($ iBox_Y + ($ {# aBoxMove [*]}/12)-eq 15]; then return 0 fi iBox_Y =$ (iBox_Y + 1) # Move a row downward for (I = 0; I <$ {# aBoxMove [*]}; I ++) do [$ (iBox_Y * 12 + I )) -lt 0] & continue # exclude situations where iBox_Y is negative when the square is displayed at the beginning. if ["$ {aBoxMove [$ I]}" = "1"-a "$ {aBoxBg [$ (iBox_Y * 12 + I)]} "! = "0"]; then # bottoming iBox_Y = $ (iBox_Y-1) # restore return 0 fi done iBox_Y = $ (iBox_Y-1) return 1} Box_Delete () # Delete rows. calculate the score. {local I j k line aLineFull = () aBoxBgNew = () line = 0 k = 0 # Check the rows that can be eliminated for (I = 0; I <15; I ++) # y do for (j = 0; j <10; j ++ )) do [$ {aBoxBg [$ (I * 12 + 1 + j)]} -eq 0] & break [$ j-eq 9] & aLineFull [$ line] = $ I & (line ++ )) done [$ line-eq 0] & return 0 # Calculate the score and level iScore =$ (iScore + (line * 2)-1) e Cho-ne "\ 033 [15; $ (iOrigoX + 30) H $ {iScore}" if [[$ (iScore/iScoreEachLevle )) -gt $ iLevel & $ iLevel-lt 20] then (iLevel ++) fi echo-ne "\ 033 [20; $ (iOrigoX + 30 )) H $ {iLevel} \ 033 [0 m "# refresh the aBoxBg array for (I = 0; I <15; I ++ )) do while ["$ (I + k-line )) "=" $ {aLineFull [$ k]} "] # Here we process the continuous elimination of multiple rows of do (k ++) done for (j = 0; j <12; j ++) do if [$ I-lt $ line]; then aBoxBgNew [$ (I * 12 + j)] = 0 else aBoxBgNew [$ (I * 12 + j )] =$ {ABoxBg [$ (I + k-line) * 12 + j)]} fi done # refresh the background aBoxBg = ($ {aBoxBgNew [*]}) for (I = 0; I <15; I ++ )) # y do for (j = 0; j <10; j ++) do echo-ne "\ 033 [$ (I + iOrigoY )); $ (j + 1) * 2) + iOrigoX) H "if [$ {aBoxBg [$ (I * 12 + 1 + j)]} -eq 0]; then echo "" else echo-ne "\ 033 [1 m \ 033 [$ {aBoxBg [$ (I * 12 + 1 + j)]} m [] \ 033 [0 m "fi done} Create_Box () # display the new box {aBoxMove = () if [-z" $ iCurrentType "]; then (iCurrentType = RANDOM % $ ISumType) (iCurrentStyle = RANDOM % $ {aStyles [$ iCurrentType]}) (iCurrentColor = 41 + RANDOM % 7 )) else response = $ iNewType iCurrentStyle = $ iNewStyle iCurrentColor = $ iNewColor fi # iCurrentType = 4 # iCurrentStyle = 1 aBoxMove = ('eval' echo $ {aBox '$ icurrenttype' _' $ iCurrentStyle Style '[*]} '') iBox_Y =$ {aBoxMove [$ ($ {# aBoxMove [*]}-1)]} iBox_X = 0 Draw_Box_New Random_Box IsBottom & kill-$ iSIG_Exit $ PPID} Random_Box () # randomly generate blocks and pre-display {local I (iNewType = RANDOM % $ iSumType )) (iNewStyle = RANDOM % $ {aStyles [$ iNewType]}) (iNewColor = 41 + RANDOM % 7) # erase the original display for (I = 0; I <$ {# aBoxMove [*]}; I ++) do if [$ {aBoxMove [$ I]}-eq 1]; then echo-ne "\ 033 [$ ($ I/12 + iOrigoY); $ ($ I % 12) * 2 + iOrigoX + 20 )) H "fi done # pre-display new block aBoxNEW = ('eval' echo $ {aBox '$ iNewType' _ '$ iNewStyle' [*]}'') for (I = 0; I <$ {# aBoxNEW [*] }; I ++) do if [$ {aBoxNEW [$ I]}-eq 1]; then echo-ne "\ 033 [1 m \ 033 [$ {iNewColor} m \ 033 [$ ($ I/12 + iOrigoY )); $ ($ I % 12) * 2 + iOrigoX + 20) H [] \ 033 [0 m "fi done} Move_Left () {local I # determine whether to touch the wall for (I = 0; I <$ ($ {# aBoxMove [*]}/12); I ++ )) do [$ {aBoxMove [$ (I * 12 + 1)]}-eq 1] & return 0 done # determine whether to touch the background for (I = 0; I <$ {# aBoxMove [*]}; I ++) do [$ (iBox_Y * 12 + I )) -le 0] & continue # exclude situations where iBox_Y is negative when the square is displayed at the beginning [$ {aBoxMove [$ I]}- Eq 1-a $ {aBoxBg [$ (iBox_Y * 12 + i-1)]} -ne 0] & return 0 # bottoming done # Clear original display Draw_Box_Clear for (I = 0; I <$ ($ {# aBoxMove [*]}-2); I ++ )) do aBoxMove [$ I] =$ {aBoxMove [$ (I + 1)]} done # re-display the box Draw_Box_New iBox_X =$ (iBox_X-1)} Move_Right () # shift right {local I # determine whether to touch the wall for (I = 0; I <$ ($ {# aBoxMove [*]}/12); I ++ )) do [$ {aBoxMove [$ (I * 12 + 10)]}-eq 1] & return 0 done # determine whether to touch the background for (I = 0; I <$ {# aBoxMove [*]}; I ++) do [$ (iBox _ Y * 12 + I )) -lt 0] & continue # exclude situations where iBox_Y is negative when the square is displayed at the beginning ["$ {aBoxMove [$ I]}" = "1"-a "$ {aBoxBg [$ (iBox_Y * 12 + I + 1)]} "! = "0"] & return 0 # bottoming done # Clear the original display Draw_Box_Clear for (I =$ ($ {# aBoxMove [*]}-2 )); i> 0; I --) do aBoxMove [$ I] =$ {aBoxMove [$ (I-1)]} done # re-display the box Draw_Box_New iBox_X =_x (iBox_X + 1)} Move_Down () # Drop a grid {local I if! IsBottom # If 0 is returned, it indicates then Draw_Box_Clear iBox_Y =$ (iBox_Y + 1) at the bottom )) # draw a new square Draw_Box_New else # Move the current square to the background (set the background of the square to the color number) (Note: 0 is blank) for (I = 0; I <$ {# aBoxMove [*]}; I ++) do if [$ {aBoxMove [$ I]}-eq 1]; then aBoxBg [$ (iBox_Y * 12 + I)] = $ iCurrentColor fi done Box_Delete Create_Box fi} Move_FallDown () # directly to the end {local I Draw_Box_Clear for (I = 0; I <15; I ++) do if! IsBottom then iBox_Y = $ (iBox_Y + 1) else break fi done Draw_Box_New Move_Down} Move_Rotate () # The current style is redirected to the next style, x displacement is determined by the iBox_X position {local I [$ {aStyles [$ iCurrentType]}-eq 1] & return 0 # Only One type is not processed. [$ iBox_Y-lt 0] & return 0 # iBox_Y <0 sRotateStyle not processed =$ (iCurrentStyle + 1) % $ {aStyles [$ iCurrentType]}) aBoxRotate = ('eval' echo $ {aBox '$ icurrenttype' _' $ sRotateStyle '[*]}') # perform left and right shift Processing Based on iBox_X, and assign a new value to aBoxR. Otate/aBoxMove if [$ iBox_X-gt 0]; then # right shift (I =$ ($ {# aBoxRotate [*]}-2 )); i> = iBox_X; I --) # the start position of a single-digit record is added at the end of the original array, so here we need to subtract 2 do aBoxRotate [$ I] =$ {aBoxRotate [$ (i-iBox_X)]} done for (I = 0; I <iBox_X; I ++) do aBoxRotate [$ I] = 0 done else # shift left (I = 0; I <$ ($ {# aBoxRotate [*]} + $ iBox_X-2); I ++ )) do aBoxRotate [$ I] =$ {aBoxRotate [$ (i-iBox_X)]} done for (I =$ ($ {# aBoxRotate [*]} + $ iBox_X-2 )); I <$ ($ {# aBoxRotate [*]}-1); I + +) Do aBoxRotate [$ I] = 0 done fi # determine whether the image can be placed after rotation #1. determine whether to touch the wall (I = 0; I <$ ($ {# aBoxRotate [*]}/12); I ++ )) do [$ {aBoxRotate [$ (I * 12)]} -eq 1] & return 0 [$ {aBoxRotate [$ (I * 12 + 11)]}-eq 1] & return 0 done #2. determine if the base is reached [$ ($ iBox_Y + ($ {# aBoxRotate [*]}/12 ))) -gt 15] & return 0 # Trigger bottom after rotation (I = 0; I <$ {# aBoxRotate [*]}; I ++ )) do # set-xv [$ (iBox_Y * 12 + I)-lt 0] & continue # exclude situations where iBox_Y is negative when the square is displayed at the beginning ["$ {aBoxRo Tate [$ I]} "=" 1 "-a" $ {aBoxBg [$ (iBox_Y * 12 + I)]} "! = "0"] & return 0 # bottoming # set + xv done # It can be rotated to eliminate the original square and display the new square Draw_Box_Clear aBoxMove = () aBoxMove = ($ {aBoxRotate [*]}) iCurrentStyle = $ sRotateStyle aBoxRotate = () Draw_Box_New} # The main program starts from here if ["$1 "! = "Display"] then bash $0 display & Control $! Exitelse Display exitfi

#----------------------------------------------------------------------------


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0610053493-0.jpg "title =" qq 30912194545.jpg "alt =" 194615786.jpg"/>


This article is from the "history_xcy" blog, please be sure to keep this http://historys.blog.51cto.com/7903899/1295243

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.