Shell遊戲 貪吃蛇 牛

2021-06-02 19:31:23 字數 3002 閱讀 7684

找個時間把這個注釋下,可以藉機好好學習shell的程式設計了。在此做個記錄

#!/bin/sh

## 區域內部的可活動的範圍。此處可修改相應的值

width=50   # 偶數

height=15

padx=10

pady=5

speed="2"  # 1-9,值越小,速度越快

## 蛇起始位置

((snakex=padx+width/2))

((snakey=pady+height/2))

((scorex=padx+width/2-10))

((scorey=pady-3))

score=0

snakehead="\033[41m@\033[0m"

snakebody="\033[41m \033[0m"

p1="\033[31mo\033[0m"

p2="\033[32mo\033[0m"

p3="\033[33mo\033[0m"

p4="\033[34mo\033[0m"

p5="\033[35mo\033[0m"

p6="\033[36mo\033[0m"

p7="\033[37mo\033[0m"

ps=(" " $p1 $p2 $p3 $p4 $p5 $p6 $p7)

pnum=$

clear

function drawbox()h\033[42m\033[0m"

echo -ne "\033[$ybig;$h\033[42m\033[0m"

done

for((i=ysmall;i<=ybig;i++))   

doecho -ne "\033[$i;$h\033[42m\033[0m"

echo -ne "\033[$i;$h\033[42m\033[0m"

done

echo

echo

}function cordtokey()

function values()

function gameover()h \033[32mgame over!\033[0m\n\n"

kill  $ppid

movexyexit

}function newp()

if [[ $v =~ "\|" ]];then

((p=random%((pnum-1))+1))

echo -ne "\033[$y;$h$"

values[`cordtokey $x $y`]="$p"

break

fidone

}function moving()h$snakehead"         

values[`cordtokey $x $y`]="snake"         

snake $x $y                             

echo -ne "\033[$oldy;$h$snakebody"    

if [[ $ != 1 ]];then                   

for((i=0;i

doif [ "$" != "" ];then

((j=i+1))

sx=$

sy=$

snakevalue[$i]=""

snakevalue[$j]=""

echo -ne "\033[$sy;$h "         

values[`cordtokey $sx $sy`]="$sx|$sy" 

break

fidone

else

((score+=v))

echo -ne "\033[$scorey;$h \033[32m score: $score \033[0m"

newpfi}

function snake()]=$1

snakevalue[$]=$2

}function movexy()

[[ $x -gt $maxx || "$v" == "snake" ]] && gameover

;;29)((x--))

v=$[[ $x -lt $padx || "$v" == "snake" ]] && gameover

;;27)((maxy=height+pady-1))

((y++))

v=$[[ $y -gt $maxy || "$v" == "snake" ]] && gameover

;;26)((y--))

v=$[[ $y -lt $pady || "$v" == "snake" ]] && gameover

;;esac

moving $x $y $oldx $oldy $v

sleep .$speed

done

}function init()h$snakehead"

values[`cordtokey $x $y`]="snake"

echo -ne "\033[$scorey;$h \033[32m score: $score \033[0m"

newp

newp

newp

}function movexyexit();0h"

echo

exit

}function movesnakeexit()

function movesnake()

## main ##

if [ "$1" == "movexy" ];then

movexy

else

bash $0 movexy &

movesnake $!  2>/dev/nullfi

用法:用方向鍵移動蛇即可。

有個bug:一開始蛇不會出現,按任意方向鍵蛇就可以出現了。

注意:不能回頭。比如蛇往右移動時,則這時不能按往左的方向鍵。

貪吃蛇遊戲

貪吃蛇遊戲 結構化程式設計 c語言程式設計 重要的的是結構化的程式設計思想 include include include include include include define field width 300 就做20個格仔的 define field height 300 define f...

貪吃蛇遊戲

include include include include include 使用當前時間做種子 enum dir 列舉型別enum dir 圍牆 class fencef 定義物件 畫框框 void fence initfence 顯示框框 void fence outputf int snak...

貪吃蛇遊戲

閒暇之餘,學習前輩經驗,再利用執行緒和窗體自己做了個貪吃蛇小遊戲。遊戲帶有加速功能,能顯示遊戲分數。借鑑的朋友們後期還可以在此基礎上增加其他功能。下面給大家顯示遊戲 以及詳細註解 一 bean類 蛇和食物的構造基礎,author deng public class node public node ...