所以我创建这个帐户只是为了问这个问题,我很绝望,哈哈。
我现在正在Bash的一个小游戏中工作并且有一个很好的基础 - 我想。问题是每次我移动时,脚本都会变得越来越慢,直到它达到某个点,在这个点上游戏的运动“落后”比输入后半秒钟。
这是我的剧本:
#!/bin/bash
source $(dirname $0)/gui.sh
# Position coordinates of character
charx=0
chary=0
# OUTPUT THE MAP AND WAIT FOR KEY STROKE
function output {
clear
echo -e "$charx $chary\n"
j=0
while [ "$j" -le "$i" ]
do
outputLevel="level$j"
echo "${!outputLevel}"
((j++))
done
read -s -n1 control
movement
}
# THIS MANAGES MOVEMENT AND ALSO BORDERS
function movement {
if [ "$control" = "$moveLeftKey" ]
then
let moveToX="$charx-1"
moveToY="$chary"
elif [ "$control" = "$moveRightKey" ]
then
let moveToX="$charx+1"
moveToY="$chary"
elif [ "$control" = "$moveUpKey" ]
then
let moveToY="$chary+1"
moveToX="$charx"
elif [ "$control" = "$moveDownKey" ]
then
let moveToY="$chary-1"
moveToX="$charx"
else
output
fi
#PlayerPosition="array$chary[$charx]"
nextPosition="array$moveToY[$moveToX]"
if [ "${!nextPosition}" = " " ]
then
IFS= read "array$chary[$charx]" <<< " "
IFS= read "array$moveToY[$moveToX]" <<< "$charLetter"
charx="$moveToX"
chary="$moveToY"
render_map
else
output
fi
}
# SET THE OUTPUT VARS FOR FUNCTION OUTPUT() FROM THE READ MAP FILE
function render_map {
j=0
while [ "$j" -le "$i" ]
do
arr="array$j[@]"
for lvl in "${!arr}"
do
tmp+="$lvl"
done
declare level$j="$tmp"
tmp=
((j++))
done
output
}
# LOCATE THE PLAYER ASCII CHARACTER DEFINED IN $charLetter
function locate_player {
j=0
while [ "$j" -le "$i" ]
do
arr="array$j[@]"
a=0
for searchPos in "${!arr}"
do
if [ "$searchPos" = "$charLetter" ]
then
charx="$a"
chary="$j"
else
((a++))
fi
done
((j++))
done
render_map
}
# READ FROM THE VAR $MAPFILE
function read_map {
i=0
while IFS= read -r "var"
do
IFS=',' read -r -a "array$i" <<< "$var"
((i++))
done < "$mapfile"
locate_player
}
# START TRIGGER
function start {
gui
# After GUI finished start the game by reading the map file
read_map
}
start
注意:导入的脚本会更进一步处理gui,但是现在它只导入以下变量:$ charLetter,$ moveRightKey,$ moveLeftKey,$ moveDownKey,$ moveUpKey和$ mapfile。
我很高兴听到任何想法,建议或任何其他引人深思的冲动!
答案 0 :(得分:1)
你似乎在非常深刻地递归,并且从未实际从任何函数返回。 Bash不会进行尾部调用消除,因此您可以以函数调用堆栈的形式稳定地占用内存。
您希望使用某种while input; do action
循环替换深层嵌套。