Shell寫的貪吃蛇遊戲(轉)
Shell寫的貪吃蛇遊戲(轉)[@more@]忙一下午,終於出來了 在我的redhat8.0上執行透過。遊戲鍵:上:w, i下:s, k左:a, j右:d, l退出:q[code:1:327d008a38]#!/bin/bashfunction DetectInput{while [[ 1 ]]doc=""read -n 1 cecho -ne "
"if [[ $c == "a" || $c == "j" || $c == "A" || $c == "J" ]]thenkill -36 $pidShowSnakeelif [[ $c == "w" || $c == "i" || $c == "W" || $c == "I" ]]thenkill -37 $pidShowSnakeelif [[ $c == "d" || $c == "l" || $c == "D" || $c == "L" ]]thenkill -38 $pidShowSnakeelif [[ $c == "s" || $c == "k" || $c == "S" || $c == "K" ]]thenkill -39 $pidShowSnakeelif [[ $c == "q" || $c == "Q" ]]thenecho Quit.kill -40 $pidShowSnakeexitfidone}function AllExit{kill -40 $pidShowSnakeexit}if [[ $1 != "--showsnake" ]]theneval $0" --showsnake "$$"&"trap "exit" TERMtrap "AllExit" INTpidShowSnake=$!DetectInputexitelsepidCtlSnake=$2fiecho subiSpeed=500 #time interval in millisecondiDirection=0 #1-left, 2-up, 3-right, 4-downiWidth=15iHeight=15iX=(6 7 8)iY=(7 7 7)iMap=()iS=0 #Start pointer in iX(( iTotalBox = iWidth * iHeight ))for (( i = 0; i < iTotalBox; i++ ))do(( iMap[$i] = 0 ))donefunction RandomHeader{(( iNew = RANDOM % ( iTotalBox - ${#iX[@]} ) ))for (( iNewP = 0, i = 0; iNewP < iTotalBox && i < iNew; iNewP++))doif (( ${iMap[$iNewP]} != 1 )); then (( i++ )); fidonewhile (( ${iMap[$iNewP]} == 1 )); do (( iNewP++ )); done(( iNewX = iNewP % iWidth ))(( iNewY = (iNewP - iNewX) / iWidth ))echo -ne "33[1m33[35m"(( pX = 2 * iNewX + iLeft + 1 ))(( pY = iNewY + iTop + 1 ))echo -ne "33["$pY";"$pX"H[]"echo -ne "33["$iCursor";1H"echo -ne "33[0m"}function InitDraw{clear(( iTop = 1 ))(( iBottom = iTop + iHeight + 1 ))(( iLeft = 1 ))(( iRight = iLeft + iWidth + iWidth + 1 ))(( iCursor = iBottom + 1 ))echo -ne "33[1m33[32m"for (( i = iLeft + 1; i < iWidth + iWidth + iLeft + 1; i++ ))doecho -ne "33["$iTop";"$i"H="echo -ne "33["$iBottom";"$i"H="donefor (( i = iTop; i < iHeight + iTop + 2; i++ ))doecho -ne "33["$i";"$iLeft"H|"echo -ne "33["$i";"$iRight"H|"doneecho -ne "33["$iCursor";1H"echo -ne "33[0m"echo -ne "33[1m33[33m"for (( i = 0; i < ${#iX[@]}; i++ ))do(( pX = 2 * ${iX[$i]} + iLeft + 1 ))(( pY = ${iY[$i]} + iTop + 1 ))(( pM = ${iY[$i]} * iWidth + ${iX[$i]} ))(( iMap[$pM] = 1 ))echo -ne "33["$pY";"$pX"H[]"#echo ${iX[$i]}doneecho -ne "33["$iCursor";1H"echo -ne "33[0m"RandomHeader}function ShiftSnake{(( iLastP = iS - 1 ))if (( iLastP < 0 )); then ((iLastP = ${#iX[@]} - 1 )); fiif (( iDir == 1 )) #leftthen(( iHX = ${iX[$iLastP]} - 1 ))(( iHY = ${iY[$iLastP]} ))elif (( iDir == 2 )) #upthen(( iHX = ${iX[$iLastP]} ))(( iHY = ${iY[$iLastP]} - 1 ))elif (( iDir == 3 )) #rightthen(( iHX = ${iX[$iLastP]} + 1 ))(( iHY = ${iY[$iLastP]} ))elif (( iDir == 4 )) #downthen(( iHX = ${iX[$iLastP]} ))(( iHY = ${iY[$iLastP]} + 1 ))fibOver=0if (( iHX < 0 || iHY < 0 || iHX >= iWidth || iHY >= iHeight )); then bOver=1;fiif (( bOver == 0 )); thenif (( ${iMap[iHY * iWidth + iHX]} == 1 )); then bOver=1; fifiif (( bOver == 1 ))thenkill $pidCtlSnake(( iBottom = iBottom + 1 ))echo -e "33["$iBottom";0HGame over! (Score: "${#iX[@]}"00)33[0m"exit 0;fi#check if catch the new boxif (( iHX == iNewX && iHY == iNewY ))thenfor (( i = ${#iX[@]}; i > iS; i-- ))do(( iX[$i] = ${iX[$i - 1]} ))(( iY[$i] = ${iY[$i - 1]} ))done(( iX[$iS] = iHX ))(( iY[$iS] = iHY ))(( iNextP = iS + 1 ))if (( iNextP >= ${#iX[@]} )); then iNextP=0; fiecho -ne "a"RandomHeaderelse(( iNextP = iS + 1 ))if (( iNextP >= ${#iX[@]} )); then iNextP=0; fi#clear snake tailer(( pX = 2 * ${iX[$iS]} + iLeft + 1 ))(( pY = ${iY[$iS]} + iTop + 1 ))(( pM = ${iY[$iS]} * iWidth + ${iX[$iS]} ))(( iMap[$pM] = 0 ))echo -ne "33["$pY";"$pX"H "(( iX[$iS] = iHX ))(( iY[$iS] = iHY ))fi#draw snake headerecho -ne "33[1m33[33m"(( pX = 2 * iHX + iLeft + 1 ))(( pY = iHY + iTop + 1 ))(( pM = iHY * iWidth + iHX ))(( iMap[$pM] = 1 ))echo -ne "33["$pY";"$pX"H[]"echo -ne "33["$iCursor";1H"echo -ne "33[0m"(( iS = iNextP ))}trap "if (( iDir != 3 && iDir != 0 )); then iDirection=1; fi" 36trap "if (( iDir != 4 )); then iDirection=2; fi" 37trap "if (( iDir != 1 )); then iDirection=3; fi" 38trap "if (( iDir != 2 )); then iDirection=4; fi" 39trap "exit" 40InitDraw(( iNanoSec = iSpeed * 1000000 ))iTime="1"`date +"%S%N"`while [[ 1 ]]dousleep 100000iTimeNew="1"`date +"%S%N"`#avoid iTimeNew < iTime when new minute comesif (( iTimeNew < iTime ))then(( iTime = iTime - 60000000000 ))fi#detect the time intervalif (( iTimeNew - iTime < iNanoSec )); then continue; fiiTime=$iTimeNewiDir=$iDirectionif (( iDir != 0 ))thenShiftSnakefidone
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-944849/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python:遊戲:貪吃蛇Python遊戲
- 貪吃蛇源程式 (轉)
- 手把手教你寫Android 貪吃蛇 遊戲Android遊戲
- jQuery 實現貪吃蛇遊戲jQuery遊戲
- 貪吃蛇jsJS
- 04 貪吃蛇
- 閒得無聊寫的一個貪吃蛇小遊戲~遊戲
- [譯] RxJS 遊戲之貪吃蛇JS遊戲
- 初試javascript :貪吃蛇啊 (轉)JavaScript
- js貪吃蛇遊戲程式碼例項JS遊戲
- canvas貪吃蛇遊戲程式碼例項Canvas遊戲
- 貪吃蛇的演算法分析(1) (轉)演算法
- 貪吃蛇的演算法分析(3) (轉)演算法
- 貪吃蛇的演算法分析(4) (轉)演算法
- 貪吃蛇的演算法分析(2) (轉)演算法
- Java實現貪吃蛇Java
- 貪吃蛇c原始碼原始碼
- javascript貪吃蛇小遊戲程式碼例項JavaScript遊戲
- html畫布製作貪吃蛇小遊戲HTML遊戲
- H5遊戲開發:貪吃蛇H5遊戲開發
- 用 Python 寫個貪吃蛇,保姆級教程!Python
- 如何用Python寫一個貪吃蛇AIPythonAI
- canvas實現的貪吃蛇遊戲程式碼例項Canvas遊戲
- 基於51微控制器的貪吃蛇遊戲遊戲
- 開發Windows貪吃蛇遊戲——(一)前期準備Windows遊戲
- C語言小遊戲------貪吃蛇----小白專用C語言遊戲
- Ubuntu下C語言實現貪吃蛇遊戲UbuntuC語言遊戲
- Python3 貪吃蛇Python
- python實現貪吃蛇Python
- C#貪吃蛇(WPF版)C#
- GUI 基於Swing製作貪吃蛇小遊戲GUI遊戲
- 【Python】 Python小遊戲-貪吃蛇大冒險Python遊戲
- 一個貪吃蛇小遊戲(17行程式碼)遊戲行程
- 一步步教你怎麼用python寫貪吃蛇遊戲Python遊戲
- OpenGL實現貪吃蛇程式碼
- C語言貪吃蛇原始碼C語言原始碼
- [原生JS][程式導向]貪吃蛇JS
- JavaScript-開發一個簡單的貪吃蛇小遊戲JavaScript遊戲