⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tetris.bash

📁 gtk_server的源代码
💻 BASH
📖 第 1 页 / 共 3 页
字号:
#!/bin/bash## A small tetris game wih the HUG abstraction layer (HUG-al)## (c) Peter van Eerten, august 2008 - GPL.## The 'tetris.mid' sound file originally was called 'Korobeiniki.mid' and was# downloaded from Wikimedia. Here it is used for demonstrational purposes only.## This is intended to be a demonstration program, and probably contains bugs.## It is *not* allowed to sell this program, or to use it in any commercial way# without written permission of the author.## However, it is allowed to use this program for educational purposes without# written permission of the author.## If you like Tetris, obtain a copy from http://www.tetris.com/.##--------------------------------------------------------------------- Embed HUG macros# Pipe filename must be unique for your applicationPIPE="/tmp/tetris.bash.\$$"# Find GTK-server configfile firstif [[ -f gtk-server.cfg ]]; then    CFG=gtk-server.cfgelif [[ -f /etc/gtk-server.cfg ]]; then    CFG=/etc/gtk-server.cfgelif [[ -f /usr/local/etc/gtk-server.cfg ]]; then    CFG=/usr/local/etc/gtk-server.cfgelse    echo "No GTK-server configfile found! Please install GTK-server..."    exit 1fi# Now create global functionnames from HUG macrosif [[ ! -f $HOME/.hug4bash || $CFG -nt $HOME/.hug4bash ]]; then    echo "#!/bin/bash" > $HOME/.hug4bash    echo "gtk-server -fifo=$PIPE &" >> $HOME/.hug4bash    echo "while [ ! -p $PIPE ]; do continue; done" >> $HOME/.hug4bash    while read LINE    do	if [[ $LINE = MACRO* ]]; then	    printf "function ${LINE#* }\n" >> $HOME/.hug4bash	    printf "{\necho ${LINE#* } \$@ > $PIPE" >> $HOME/.hug4bash	    printf "\nread GTK < $PIPE\n}\n" >> $HOME/.hug4bash	fi    done < $CFG    printf "\nfunction gtk()\n{\necho \$1 > $PIPE; read GTK < $PIPE;\n}\n" >> $HOME/.gtk4bashfi# Declare global variablesdeclare GTK NULL="NULL"unset CFG PIPE LINE#----------------------------------------------------------------------------------------# Assignment functionfunction define() { $2 $3 $4 $5 $6 $7; eval $1="\"$GTK\""; }#----------------------------------------------------------------------------------------## The '0000' piece - code 1##----------------------------------------------------------------------------------------function Define_or_Wipe_One{# Define these as localtypeset -i POS X Y# Assign the argumentsX=$1; Y=$2# Calculate position on board arraylet POS=$X/20+$Y/2# Determine position, then wipe or drawif [[ $3 -eq 0 || $3 -eq 2 ]]then    let BOARD[$POS-1]=$4    let BOARD[$POS]=$4    let BOARD[$POS+1]=$4    let BOARD[$POS+2]=$4elif [[ $3 -eq 1 || $3 -eq 3 ]]then    let BOARD[$POS-10]=$4    let BOARD[$POS]=$4    let BOARD[$POS+10]=$4    let BOARD[$POS+20]=$4fi}function Action_One{# Define these as localtypeset -i POS STATUS=1# Wipe the piece from arrayDefine_or_Wipe_One $CURX $CURY $CUR_ROTATION 0# Check board boundariesif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWX -lt 20 ]]then    NEWX=20fiif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWX -gt 140 ]]then    NEWX=140fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWX -gt 180 ]]then    NEWX=180fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWY -lt 20 ]]then    NEWY=20fiif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWY -gt 480 ]]then    NEWY=480; STATUS=0fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWY -gt 440 ]]then    NEWY=440; STATUS=0; CURY=$NEWY; CUR_ROTATION=$NEW_ROTATIONfi# Calculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Check if move to left or right was legalif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && ${BOARD[$POS-1]} -ne 0 && $NEWX -lt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && ${BOARD[$POS+2]} -ne 0 && $NEWX -gt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && (${BOARD[$POS-10]} -ne 0 || ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0) && $NEWX -lt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && (${BOARD[$POS-10]} -ne 0 || ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0) && $NEWX -gt $CURX ]]then    NEWX=$CURXfi# Recalculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Now check if all new positions are availableif [[ $NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2 ]]then    if [[ ${BOARD[$POS-1]} -ne 0 || ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+2]} -ne 0 ]]    then	STATUS=0    fielif [[ $NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3 ]]then    if [[ ${BOARD[$POS-10]} -ne 0 || ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+20]} -ne 0 ]]    then	STATUS=0    fifi# If all positions are available, draw piece '1' on new coordnates in array, set returnvalueif [[ $STATUS -eq 1 ]]then    Define_or_Wipe_One $NEWX $NEWY $NEW_ROTATION 1    PIECE=1else    # If not, draw piece '1' on old coordinates in array, set returnvalue    Define_or_Wipe_One $CURX $CURY $CUR_ROTATION 1    if [[ $CUR_ROTATION -eq $NEW_ROTATION ]]    then	PIECE=0    else	NEW_ROTATION=$CUR_ROTATION    fifi}#----------------------------------------------------------------------------------------## The  'OO' piece - code 2#	'OO'#----------------------------------------------------------------------------------------## $1 = X, $2 = Y, $3 = ROTATION, $4 = 0/3#function Define_or_Wipe_Two{# Define these as localtypeset -i POS X Y# Assign the argumentsX=$1; Y=$2# Calculate position on board arraylet POS=$X/20+$Y/2# Determine position, then wipe or drawif [[ $3 -eq 0 || $3 -eq 2 ]]then    let BOARD[$POS]=$4    let BOARD[$POS+1]=$4    let BOARD[$POS+11]=$4    let BOARD[$POS+12]=$4elif [[ $3 -eq 1 || $3 -eq 3 ]]then    let BOARD[$POS]=$4    let BOARD[$POS+9]=$4    let BOARD[$POS+10]=$4    let BOARD[$POS+19]=$4fi}function Action_Two{# Define these as localtypeset -i POS STATUS=1# Wipe the piece from arrayDefine_or_Wipe_Two $CURX $CURY $CUR_ROTATION 0# Check board boundariesif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWX -gt 140 ]]then    NEWX=140fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWX -gt 180 ]]then    NEWX=180fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWX -lt 20 ]]then    NEWX=20fiif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWY -gt 460 ]]then    NEWY=460; STATUS=0fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWY -gt 440 ]]then    NEWY=440; STATUS=0; CURY=$NEWY; CUR_ROTATION=$NEW_ROTATIONfi# Calculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Check if move to left or right was legalif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+11]} -ne 0) && $NEWX -lt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && (${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+12]} -ne 0) && $NEWX -gt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+9]} -ne 0 || ${BOARD[$POS+19]} -ne 0) && $NEWX -lt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+19]} -ne 0) && $NEWX -gt $CURX ]]then    NEWX=$CURXfi# Recalculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Now check if all new positions are availableif [[ $NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2 ]]then    if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+11]} -ne 0 || ${BOARD[$POS+12]} -ne 0 ]]    then	STATUS=0    fielif [[ $NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3 ]]then    if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+9]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+19]} -ne 0 ]]    then	STATUS=0    fifi# If all positions are available, draw piece '2' on new coordnates in array, set returnvalueif [[ $STATUS -eq 1 ]]then    Define_or_Wipe_Two $NEWX $NEWY $NEW_ROTATION 2    PIECE=2else    # If not, draw piece '2' on old coordinates in array, set returnvalue    Define_or_Wipe_Two $CURX $CURY $CUR_ROTATION 2    if [[ $CUR_ROTATION -eq $NEW_ROTATION ]]    then	PIECE=0    else	NEW_ROTATION=$CUR_ROTATION    fifi}#----------------------------------------------------------------------------------------## The  'OO' piece - code 3#     'OO'##----------------------------------------------------------------------------------------## $1 = X, $2 = Y, $3 = ROTATION, $4 = 0/3#function Define_or_Wipe_Three{# Define these as localtypeset -i POS X Y# Assign the argumentsX=$1; Y=$2# Calculate position on board arraylet POS=$X/20+$Y/2# Determine position, then wipe or drawif [[ $3 -eq 0 || $3 -eq 2 ]]then    let BOARD[$POS]=$4    let BOARD[$POS+1]=$4    let BOARD[$POS-8]=$4    let BOARD[$POS-9]=$4elif [[ $3 -eq 1 || $3 -eq 3 ]]then    let BOARD[$POS]=$4    let BOARD[$POS+10]=$4    let BOARD[$POS+11]=$4    let BOARD[$POS+21]=$4fi}function Action_Three{# Define these as localtypeset -i POS STATUS=1# Wipe the piece from arrayDefine_or_Wipe_Three $CURX $CURY $CUR_ROTATION 0# Check board boundariesif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWX -gt 140 ]]then    NEWX=140fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWX -gt 160 ]]then    NEWX=160fiif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && $NEWY -gt 480 ]]then    NEWY=480; STATUS=0fiif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && $NEWY -gt 440 ]]then    NEWY=440; STATUS=0; CURY=$NEWY; CUR_ROTATION=$NEW_ROTATIONfi# Calculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Check if move to left or right was legalif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS-9]} -ne 0) && $NEWX -lt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2) && (${BOARD[$POS-8]} -ne 0 || ${BOARD[$POS+1]} -ne 0) && $NEWX -gt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+21]} -ne 0) && $NEWX -lt $CURX ]]then    NEWX=$CURXelif [[ ($NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3) && (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+11]} -ne 0 || ${BOARD[$POS+21]} -ne 0) && $NEWX -gt $CURX ]]then    NEWX=$CURXfi# Recalculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Now check if all new positions are availableif [[ $NEW_ROTATION -eq 0 || $NEW_ROTATION -eq 2 ]]then    if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS-8]} -ne 0 || ${BOARD[$POS-9]} -ne 0 ]]    then	STATUS=0    fielif [[ $NEW_ROTATION -eq 1 || $NEW_ROTATION -eq 3 ]]then    if [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+11]} -ne 0 || ${BOARD[$POS+21]} -ne 0 ]]    then	STATUS=0    fifi# If all positions are available, draw piece '3' on new coordnates in array, set returnvalueif [[ $STATUS -eq 1 ]]then    Define_or_Wipe_Three $NEWX $NEWY $NEW_ROTATION 3    PIECE=3else    # If not, draw piece '3' on old coordinates in array, set returnvalue    Define_or_Wipe_Three $CURX $CURY $CUR_ROTATION 3    if [[ $CUR_ROTATION -eq $NEW_ROTATION ]]    then	PIECE=0    else	NEW_ROTATION=$CUR_ROTATION    fifi}#----------------------------------------------------------------------------------------## The  'OO' piece - code 4#      'OO'##----------------------------------------------------------------------------------------## $1 = X, $2 = Y, $3 = ROTATION, $4 = 0/3#function Define_or_Wipe_Four{# Define these as localtypeset -i POS X Y# Assign the argumentsX=$1; Y=$2# Calculate position on board arraylet POS=$X/20+$Y/2# Wipe or drawlet BOARD[$POS]=$4let BOARD[$POS+1]=$4let BOARD[$POS+10]=$4let BOARD[$POS+11]=$4}function Action_Four{# Define these as localtypeset -i POS STATUS=1# Wipe the piece from arrayDefine_or_Wipe_Four $CURX $CURY $CUR_ROTATION 0# Check board boundariesif [[ $NEWX -gt 160 ]]then    NEWX=160fiif [[ $NEWY -gt 460 ]]then    NEWY=460; STATUS=0fi# Calculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Check if move to left or right was legalif [[ (${BOARD[$POS]} -ne 0 || ${BOARD[$POS+10]} -ne 0) && $NEWX -lt $CURX ]]then    NEWX=$CURXelif [[ (${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+11]} -ne 0) && $NEWX -gt $CURX ]]then    NEWX=$CURXfi# Recalculate new position on board arraylet POS=$NEWX/20+$NEWY/2# Now check if all new positions are availableif [[ ${BOARD[$POS]} -ne 0 || ${BOARD[$POS+1]} -ne 0 || ${BOARD[$POS+10]} -ne 0 || ${BOARD[$POS+11]} -ne 0 ]]then    STATUS=0fi# If all positions are available, draw piece '4' on new coordnates in array, set returnvalueif [[ $STATUS -eq 1 ]]then    Define_or_Wipe_Four $NEWX $NEWY $NEW_ROTATION 4    PIECE=4else    # If not, draw piece '4' on old coordinates in array, set returnvalue    Define_or_Wipe_Four $CURX $CURY $CUR_ROTATION 4    if [[ $CUR_ROTATION -eq $NEW_ROTATION ]]    then	PIECE=0    else	NEW_ROTATION=$CUR_ROTATION    fifi}#----------------------------------------------------------------------------------------#

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -