One of the three days to learn shell Tetris: drawing the interface

Source: Internet
Author: User
One of the three days to learn about shell Tetris: drawing the interface-general Linux technology-Linux programming and kernel information. The following is a detailed description. [I = s] This post was last edited by kevin_2009 at, 2009-4-14


No way, the teacher asked me to finish the Tetris programming in shell in three days, so I could only do it,
By the way, I will share with you and work together to complete this interesting game.

The first step is to draw the interface:



First, we need to learn how to use the color in shell.

Let's take a look at the definition of the color in shell:
Code color
---------------------------------------
0 black
1? Color
2? Color
3? Color
4? Color
5 purple? Color
6 green? Color
7 white
-----------------------------------------

Use shell color
  
Next I will use a prompt example to explain the shell color.
  
[Nico @ ebrain: ~] $ PS1 = "\ [\ 033 [0; 32; 40m \ u @ \ h: \ w \ $ \]"
  
In this way, all the prompts are displayed in green. Like this: [nico @ ebrain: ~] $
  
\ 033 declares the start of the escape sequence, and then [begins to define the color. The following 0 defines the default font width. I will introduce other available characters later. Escape Sequence strings must be enclosed by \ [and \] to prevent the text of escape sequences from occupying too much space in shell.
  
Next we will select the foreground color (here is 32, representing green ). 40 of the background color indicates black. If you do not want to change the text after the prompt to green, use \ 033 [0 m to close the escape sequence. \ 033 [0 m is the default color of shell. Both the foreground and background colors are available.
  
Optional colors: Red, green, yellow, blue, magenta, cyan, and white. Their corresponding color codes are: 30 (black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (Foreign red), 36 (blue), 37 (white ).
  
Set the background color using the same color method, but Replace the first digit "3" with "4", for example, 40, 41, 42, 43, 44, 45, 46, and 47.
  
Example:
  
~ /. Bashrc: PS1 = "\ [\ 033 [0; 37; 44m \ u @ \ 033 [0; 32; 43m \ h: \ 033 [0; 33; 41m \ w $ \ 033 [0m \]"
  
This gives us a very colorful prompt:
  
Nico @ ebrain :~ $

  
I believe that after the above learning, you have a better understanding of the color under shell, So let's continue to write about the Russian square.

From the player's point of view, the Tetris game. The game is made up of several basic shapes of bricks, which are lowered to the bottom of the window at a certain speed.
Bricks can be flipped and moved during the fall process. When there is a brick link at the bottom and occupies a whole row, it can be eliminated. So,
Our implementation is divided into two parts: the background processing part and the user interface display part.

Return to the interface design issue. To make the color easier to use, replace the characters in English:

# Color definition
CRed = 1
CGreen = 2
CYellow = 3
CBlue = 4
CFuchsia = 5
CCyan = 6
CWhite = 7
ColorTable = ($ cRed $ cGreen $ cYellow $ cBlue $ cFuchsia $ cCyan $ cWhite)

The next step is to define an array to display the square. We know that there are eight kinds of squares, and each square has several transformation methods:

# Definitions of blocks in section 7
# By rotating, the display style of each square may be several
Box0 = (0 0 0 1 1 0 1)
Box1 = (0 2 1 2 2 3 2 1 0 1 1 1 2 1 3)
Box2 = (0 0 0 1 1 1 1 2 0 1 1 1 1 2 0)
Box3 = (0 1 0 2 1 0 1 1 0 1 0 1 1 1 2 1)
Box4 = (0 1 0 2 1 1 1 1 1 1 1 1 1 2 2 2 2 0 1 1 2 0 2 1 0 1 0 1 1 1 1 1 2)
Box5 = (0 1 1 1 1 2 2 2 1 1 1 1 1 2 2 0 0 0 0 1 1 1 2 1 0 2 1 1 1 1 2)
Box6 = (0 1 1 1 1 2 2 1 1 1 1 1 1 2 2 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 2)
# Put all the definitions of squares in the box variable
Box = ($ {box0 [@] }$ {box1 [@] }$ {box2 [@] }$ {box3 [@] }$ {box4 [@] }$ {box5 [@] }$ {box6 [@]})
# Number of possible styles after rotation of various squares
CountBox = (1 2 2 2 4 4 4)



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.