我记得很久以前有一部旧手机,上面有一款游戏,我记得它的名字是“贪吃蛇”或“蛇类”,基本上你可以用箭头键改变蛇的方向,蛇不能触碰自己(否则游戏结束),但如果它触碰到地图的边缘,它就会出现在另一边。游戏的目的是让蛇吃食物,但每吃一点食物(每次它吃一些食物,就会在其他地方出现一些,但通常是一次吃一个),蛇就会变长一点,这让游戏变得更难玩。
我相信你们都熟悉这个游戏,所以我想知道(因为我怀念这个游戏,而且只能找到奇怪的 3D 版本)这个游戏在 Terminal 中是否有版本?我希望它会坚持原版,并且可能采用类似 ASCII 的方式?
我正在运行带有 GNOME 3.20 的 Ubuntu GNOME 16.04.1,官方存储库中是否有这样的免费应用程序(我希望它来自那里)?
答案1
答案2
蜈
这款游戏的名字centipede
叫网站不再维护。这是一款无需下载的 bash 游戏,对于那些对 bash 脚本感兴趣的人来说是一项有趣的研究。
选项
您可以通过更改以下变量来更改屏幕尺寸,使其更小且更具挑战性:
LASTCOL=40 # Last col of game area
LASTROW=20 # Last row of game area
查看获取“漂亮角色”的选项:
# Prettier characters but not supported
# by all termtypes/locales
#SNAKECHAR="\0256" # Character to use for snake
#WALLCHAR="\0244" # Character to use for wall
#APPLECHAR="\0362" # Character to use for apples
#
# Normal boring ASCII Chars
SNAKECHAR="@" # Character to use for snake
WALLCHAR="X" # Character to use for wall
APPLECHAR="o" # Character to use for apples
删除前三行代码的注释,并向#
后三行代码添加注释()。
完整的 bash 代码
完整代码如下:
#!/bin/bash
#
# Centipede game
#
# v2.0
#
# Author: [email protected]
#
# Functions
drawborder() {
# Draw top
tput setf 6
tput cup $FIRSTROW $FIRSTCOL
x=$FIRSTCOL
while [ "$x" -le "$LASTCOL" ];
do
printf %b "$WALLCHAR"
x=$(( $x + 1 ));
done
# Draw sides
x=$FIRSTROW
while [ "$x" -le "$LASTROW" ];
do
tput cup $x $FIRSTCOL; printf %b "$WALLCHAR"
tput cup $x $LASTCOL; printf %b "$WALLCHAR"
x=$(( $x + 1 ));
done
# Draw bottom
tput cup $LASTROW $FIRSTCOL
x=$FIRSTCOL
while [ "$x" -le "$LASTCOL" ];
do
printf %b "$WALLCHAR"
x=$(( $x + 1 ));
done
tput setf 9
}
apple() {
# Pick coordinates within the game area
APPLEX=$[( $RANDOM % ( $[ $AREAMAXX - $AREAMINX ] + 1 ) ) + $AREAMINX ]
APPLEY=$[( $RANDOM % ( $[ $AREAMAXY - $AREAMINY ] + 1 ) ) + $AREAMINY ]
}
drawapple() {
# Check we haven't picked an occupied space
LASTEL=$(( ${#LASTPOSX[@]} - 1 ))
x=0
apple
while [ "$x" -le "$LASTEL" ];
do
if [ "$APPLEX" = "${LASTPOSX[$x]}" ] && [ "$APPLEY" = "${LASTPOSY[$x]}" ];
then
# Invalid coords... in use
x=0
apple
else
x=$(( $x + 1 ))
fi
done
tput setf 4
tput cup $APPLEY $APPLEX
printf %b "$APPLECHAR"
tput setf 9
}
growsnake() {
# Pad out the arrays with oldest position 3 times to make snake bigger
LASTPOSX=( ${LASTPOSX[0]} ${LASTPOSX[0]} ${LASTPOSX[0]} ${LASTPOSX[@]} )
LASTPOSY=( ${LASTPOSY[0]} ${LASTPOSY[0]} ${LASTPOSY[0]} ${LASTPOSY[@]} )
RET=1
while [ "$RET" -eq "1" ];
do
apple
RET=$?
done
drawapple
}
move() {
case "$DIRECTION" in
u) POSY=$(( $POSY - 1 ));;
d) POSY=$(( $POSY + 1 ));;
l) POSX=$(( $POSX - 1 ));;
r) POSX=$(( $POSX + 1 ));;
esac
# Collision detection
( sleep $DELAY && kill -ALRM $$ ) &
if [ "$POSX" -le "$FIRSTCOL" ] || [ "$POSX" -ge "$LASTCOL" ] ; then
tput cup $(( $LASTROW + 1 )) 0
stty echo
echo " GAME OVER! You hit a wall!"
gameover
elif [ "$POSY" -le "$FIRSTROW" ] || [ "$POSY" -ge "$LASTROW" ] ; then
tput cup $(( $LASTROW + 1 )) 0
stty echo
echo " GAME OVER! You hit a wall!"
gameover
fi
# Get Last Element of Array ref
LASTEL=$(( ${#LASTPOSX[@]} - 1 ))
#tput cup $ROWS 0
#printf "LASTEL: $LASTEL"
x=1 # set starting element to 1 as pos 0 should be undrawn further down (end of tail)
while [ "$x" -le "$LASTEL" ];
do
if [ "$POSX" = "${LASTPOSX[$x]}" ] && [ "$POSY" = "${LASTPOSY[$x]}" ];
then
tput cup $(( $LASTROW + 1 )) 0
echo " GAME OVER! YOU ATE YOURSELF!"
gameover
fi
x=$(( $x + 1 ))
done
# clear the oldest position on screen
tput cup ${LASTPOSY[0]} ${LASTPOSX[0]}
printf " "
# truncate position history by 1 (get rid of oldest)
LASTPOSX=( `echo "${LASTPOSX[@]}" | cut -d " " -f 2-` $POSX )
LASTPOSY=( `echo "${LASTPOSY[@]}" | cut -d " " -f 2-` $POSY )
tput cup 1 10
#echo "LASTPOSX array ${LASTPOSX[@]} LASTPOSY array ${LASTPOSY[@]}"
tput cup 2 10
echo "SIZE=${#LASTPOSX[@]}"
# update position history (add last to highest val)
LASTPOSX[$LASTEL]=$POSX
LASTPOSY[$LASTEL]=$POSY
# plot new position
tput setf 2
tput cup $POSY $POSX
printf %b "$SNAKECHAR"
tput setf 9
# Check if we hit an apple
if [ "$POSX" -eq "$APPLEX" ] && [ "$POSY" -eq "$APPLEY" ]; then
growsnake
updatescore 10
fi
}
updatescore() {
SCORE=$(( $SCORE + $1 ))
tput cup 2 30
printf "SCORE: $SCORE"
}
randomchar() {
[ $# -eq 0 ] && return 1
n=$(( ($RANDOM % $#) + 1 ))
eval DIRECTION=\${$n}
}
gameover() {
tput cvvis
stty echo
sleep $DELAY
trap exit ALRM
tput cup $ROWS 0
exit
}
###########################END OF FUNCS##########################
# Prettier characters but not supported
# by all termtypes/locales
#SNAKECHAR="\0256" # Character to use for snake
#WALLCHAR="\0244" # Character to use for wall
#APPLECHAR="\0362" # Character to use for apples
#
# Normal boring ASCII Chars
SNAKECHAR="@" # Character to use for snake
WALLCHAR="X" # Character to use for wall
APPLECHAR="o" # Character to use for apples
#
SNAKESIZE=3 # Initial Size of array aka snake
DELAY=0.2 # Timer delay for move function
FIRSTROW=3 # First row of game area
FIRSTCOL=1 # First col of game area
LASTCOL=40 # Last col of game area
LASTROW=20 # Last row of game area
AREAMAXX=$(( $LASTCOL - 1 )) # Furthest right play area X
AREAMINX=$(( $FIRSTCOL + 1 )) # Furthest left play area X
AREAMAXY=$(( $LASTROW - 1 )) # Lowest play area Y
AREAMINY=$(( $FIRSTROW + 1)) # Highest play area Y
ROWS=`tput lines` # Rows in terminal
ORIGINX=$(( $LASTCOL / 2 )) # Start point X - use bc as it will round
ORIGINY=$(( $LASTROW / 2 )) # Start point Y - use bc as it will round
POSX=$ORIGINX # Set POSX to start pos
POSY=$ORIGINY # Set POSY to start pos
# Pad out arrays
ZEROES=`echo |awk '{printf("%0"'"$SNAKESIZE"'"d\n",$1)}' | sed 's/0/0 /g'`
LASTPOSX=( $ZEROES ) # Pad with zeroes to start with
LASTPOSY=( $ZEROES ) # Pad with zeroes to start with
SCORE=0 # Starting score
clear
echo "
Keys:
W - UP
S - DOWN
A - LEFT
D - RIGHT
X - QUIT
If characters do not display properly, consider changing
SNAKECHAR, APPLECHAR and WALLCHAR variables in script.
Characters supported depend upon your terminal setup.
Press Return to continue
"
stty -echo
tput civis
read RTN
tput setb 0
tput bold
clear
drawborder
updatescore 0
# Draw the first apple on the screen
# (has collision detection to ensure we don't draw
# over snake)
drawapple
sleep 1
trap move ALRM
# Pick a random direction to start moving in
DIRECTIONS=( u d l r )
randomchar "${DIRECTIONS[@]}"
sleep 1
move
while :
do
read -s -n 1 key
case "$key" in
w) DIRECTION="u";;
s) DIRECTION="d";;
a) DIRECTION="l";;
d) DIRECTION="r";;
x) tput cup $COLS 0
echo "Quitting..."
tput cvvis
stty echo
tput reset
printf "Bye Bye!\n"
trap exit ALRM
sleep $DELAY
exit 0
;;
esac
done
答案3
有一个名为 的命令行游戏集合bsdgames
。
sudo apt-get install bsdgames
您可以通过输入或 来安装它sudo apt install bsdgames
。
安装成功后,您可以从此列表启动游戏(只需在终端中输入其名称即可)
adventure (6) - an exploration game
sol (6) - a collection of card games which are easy to play with the aid of a mouse.
arithmetic (6) - quiz on simple arithmetic
atc (6) - air traffic controller game
backgammon (6) - the game of backgammon
battlestar (6) - a tropical adventure game
bcd (6) - "reformat input as punch cards, paper tape or morse code"
boggle (6) - word search game
caesar (6) - decrypt caesar ciphers
canfield (6) - the solitaire card game canfield
cfscores (6) - the solitaire card game canfield
chkfont (6) - checks figlet 2.0 and up font files for format errors
countmail (6) - be obnoxious about how much mail you have
cowsay (6) - configurable speaking/thinking cow (and a bit more)
cribbage (6) - the card game cribbage
dab (6) - Dots and Boxes game
espdiff (6) - apply the appropriate transformation to a set of patches
figlet-figlet (6) - display large characters made up of ordinary screen characters
figlist (6) - lists figlet fonts and control files
fortune (6) - print a random, hopefully interesting, adage
gnome-mahjongg (6) - A matching game played with Mahjongg tiles
gnome-mines (6) - The popular logic puzzle minesweeper
gnome-sudoku (6) - puzzle game for the popular Japanese sudoku logic puzzle
go-fish (6) - play "Go Fish"
gomoku (6) - game of 5 in a row
hack (6) - exploring The Dungeons of Doom
hangman (6) - computer version of the game hangman
hunt (6) - a multi-player multi-terminal game
huntd (6) - hunt daemon, back-end for hunt game
intro (6) - introduction to games
lolcat (6) - rainbow coloring for text
mille (6) - play Mille Bornes
monop (6) - Monopoly game
morse (6) - "reformat input as punch cards, paper tape or morse code"
number (6) - convert Arabic numerals to English
phantasia (6) - an interterminal fantasy game
pig (6) - eformatray inputway asway Igpay Atinlay
pom (6) - display the phase of the moon
ppt (6) - "reformat input as punch cards, paper tape or morse code"
primes (6) - generate primes
quiz (6) - random knowledge tests
rain (6) - animated raindrops display
random (6) - random lines from a file or random numbers
robots (6) - fight off villainous robots
rot13 (6) - decrypt caesar ciphers
sail (6) - multi-user wooden ships and iron men
snake (6) - display chase game
snscore (6) - display chase game
teachgammon (6) - learn to play backgammon
tetris-bsd (6) - the game of tetris
trek (6) - trekkie game
wargames (6) - shall we play a game?
worm (6) - Play the growing worm game
worms (6) - animate worms on a display terminal
wtf (6) - translates acronyms for you
wump (6) - hunt the wumpus in an underground cave
这些游戏通常通过按Ctrl+C