Diferencia entre revisiones de «Roguelike amb scripts»
Sin resumen de edición |
Sin resumen de edición |
||
Línea 153: | Línea 153: | ||
done | done | ||
</nowiki> | </nowiki> | ||
== Solució amb portes i arquers == | |||
<nowiki> | |||
#!/bin/bash | |||
## Declaració de variables | |||
tecla=1 | |||
pantalla_actual=pantalla$1 | |||
y=$(cat $pantalla_actual | grep @ -n | cut -f1 -d":") | |||
x=$(cat $pantalla_actual | grep @ | tr " " "\n" | grep -n @ | cut -f1 -d":") | |||
jugador=$(cat $pantalla_actual | grep @ | cut -f$x -d" ") | |||
x=$((x-1)) | |||
y=$((y-1)) | |||
pantalla=($(cat $pantalla_actual | grep '^#' | tr "\n" " ")) | |||
mida=$(cat $pantalla_actual | head -1 | wc -w) | |||
inventari="" | |||
fletxes="0" | |||
dispar_automatic=0 | |||
clear | |||
## Declaració de funcions | |||
function moviment { | |||
dx=$1 | |||
dy=$2 | |||
destix=$((x+dx)) | |||
destiy=$((y+dy)) | |||
desti=${pantalla[$((destiy*mida+destix))]} | |||
if [[ "$desti" == '.' ]] | |||
then | |||
pantalla[$((destiy*mida+destix))]=$jugador | |||
pantalla[$((y*mida+x))]='.' | |||
x=$destix | |||
y=$destiy | |||
fi | |||
if [[ $desti == $\;[0-9] ]] | |||
then | |||
pantalla[$((destiy*mida+destix))]=$jugador | |||
pantalla[$((y*mida+x))]='.' | |||
x=$destix | |||
y=$destiy | |||
inventari="$inventari $desti" | |||
fi | |||
if [[ $desti == ♡\;[0-9][0-9] ]] # vida | |||
then | |||
pantalla[$((destiy*mida+destix))]=$jugador | |||
pantalla[$((y*mida+x))]='.' | |||
x=$destix | |||
y=$destiy | |||
arma_jugador=$(echo $jugador | cut -d";" -f3) | |||
vida_jugador=$(echo $jugador | cut -d";" -f2) | |||
vida=$(echo $desti | cut -d";" -f2) | |||
vida_jugador=$(($vida_jugador+$vida)) | |||
jugador="@;$vida_jugador;$arma_jugador" | |||
fi | |||
if [[ $desti == 𝀃 ]] # arma | |||
then | |||
pantalla[$((destiy*mida+destix))]=$jugador | |||
pantalla[$((y*mida+x))]='.' | |||
x=$destix | |||
y=$destiy | |||
arma_jugador=$(echo $jugador | cut -d";" -f3) | |||
vida_jugador=$(echo $jugador | cut -d";" -f2) | |||
arma_jugador=$(($arma_jugador+20)) | |||
jugador="@;$vida_jugador;$arma_jugador" | |||
fi | |||
} | |||
function interaccio { | |||
dx=$1 | |||
dy=$2 | |||
elementx=$((x+dx)) | |||
elementy=$((y+dy)) | |||
enemic=${pantalla[$((elementy*mida+elementx))]} | |||
echo -en "\e[s\e[$((dy+2));$(($mida+25+($dx*5)))H$enemic\e[u" | |||
echo -en "\e[s\e[2;$(($mida+25))H@\e[u" | |||
if [[ "$enemic" == [sa]* ]] # si és un soldat (o arquer a curta distància) | |||
then | |||
soldat=$enemic | |||
arma_soldat=$(echo $soldat | cut -d";" -f3) | |||
if [ "$enemic" == a* ] | |||
then | |||
arma_soldat=0; | |||
fi | |||
vida_soldat=$(echo $soldat | cut -d";" -f2) | |||
arma_jugador=$(echo $jugador | cut -d";" -f3) | |||
vida_jugador=$(echo $jugador | cut -d";" -f2) | |||
vida_jugador=$(($vida_jugador-$arma_soldat)) | |||
vida_soldat=$(($vida_soldat-$arma_jugador)) | |||
# Experiència | |||
arma_jugador=$(($arma_jugador+1)) | |||
soldat="s;$vida_soldat;$arma_soldat" | |||
jugador="@;$vida_jugador;$arma_jugador" | |||
if [ $vida_soldat -le 0 ] | |||
then | |||
soldat='.' | |||
fi | |||
if [ $vida_jugador -le 0 ] | |||
then | |||
echo "Has mort!!" | |||
exit | |||
fi | |||
pantalla[$((elementy*mida+elementx))]=$soldat | |||
fi | |||
if [[ "$enemic" == +* ]] # si és una porta | |||
then | |||
porta=$enemic | |||
n_porta=$(echo $porta | cut -d";" -f2) | |||
if echo $inventari | grep "$;$n_porta" > /dev/null | |||
then | |||
porta='.' | |||
fi | |||
pantalla[$((elementy*mida+elementx))]=$porta | |||
fi | |||
} | |||
function arquers { | |||
dx=$1 | |||
dy=$2 | |||
elementx=$((x+dx)) | |||
elementy=$((y+dy)) | |||
distancia=$((${dx#-}+${dy#-})) | |||
pos_element=$((elementy*mida+elementx)) | |||
if [ $pos_element -ge 0 ] && [ $distancia -gt 2 ] # pot eixir de l'array | |||
then | |||
enemic=${pantalla[$pos_element]} | |||
if [[ "$enemic" == a* ]] # si és un arquer | |||
then | |||
arquer=$enemic | |||
arma_arquer=$(echo $arquer | cut -d";" -f3) | |||
arma_jugador=$(echo $jugador | cut -d";" -f3) | |||
vida_jugador=$(echo $jugador | cut -d";" -f2) | |||
vida_jugador=$(($vida_jugador-$arma_arquer)) | |||
jugador="@;$vida_jugador;$arma_jugador" | |||
if [ $vida_jugador -le 0 ] | |||
then | |||
echo "Has mort!!" | |||
exit | |||
fi | |||
fi | |||
fi | |||
} | |||
## Joc principal | |||
while [[ $tecla != 0 ]] | |||
do | |||
echo -en "\e[s\e[0;0H\n$(echo ${pantalla[*]} | sed "s/\(\([^ ]\+ \)\{$mida\}\)/\1\n/g" | sed 's/\(.\)\(;[0-9]\+\)\+/\1/g' | tr -d " " | sed -e 's/#/\\e[32m#\\e[0m/g' -e 's/@/\\e[31m@\\e[0m/g')\n\nJugador:$jugador Inventari:$inventari $dispar_automaticᗡ;$fletxes \e[u" | |||
read -s -n 1 tecla | |||
echo -en "\e[s\e[0;0H \e[u" | |||
echo -en "\e[s\e[0;$(($mida+20))H \e[u" | |||
echo -en "\e[s\e[1;$(($mida+20))H \e[u" | |||
echo -en "\e[s\e[2;$(($mida+20))H \e[u" | |||
echo -en "\e[s\e[3;$(($mida+20))H \e[u" | |||
case $tecla in | |||
[aA]) | |||
moviment -1 0 | |||
;; | |||
[dD]) | |||
moviment 1 0 | |||
;; | |||
[wW]) | |||
moviment 0 -1 | |||
;; | |||
[sS]) | |||
moviment 0 1 | |||
;; | |||
[fF]) | |||
dispar_automatic=1 | |||
;; | |||
[gG]) | |||
dispar_automatic=0 | |||
;; | |||
*) | |||
echo "La tecla $tecla no val per a res" | |||
clear | |||
;; | |||
esac | |||
interaccio 0 1 | |||
interaccio 0 -1 | |||
interaccio 1 0 | |||
interaccio -1 0 | |||
for i in -3 -2 -1 0 1 2 3 | |||
do | |||
for j in -3 -2 -1 0 1 2 3 | |||
do | |||
arquers $i $j | |||
done | |||
done | |||
done | |||
</nowiki |
Revisión del 13:22 17 abr 2013
Pantalla amb mags
# # # # # # # # # # # # # # # # # # # # # # # # # # # . . . . . . . . . . . @;100;25 . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . a;10;4 . # # # # . . . . . . . . . . . . . . # # . . . . a;10;4 . # . $;1 # . . . . . . . . . . . . . . # # . . . . a;10;4 . . . m;1 # . . . . . . . . . . . . . . # # . . . . a;10;4 . # # # # . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . ᗡ;5 . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . . . . . . . . . . . . . . . . . . . . . . . # # . . s;1;100 s;1;100 . . . . . . . . . . . . . . . . . . . m;2 # # # # +;1 # # # # # # # # # # # # # # # # # # # # # #
m1-pantalla[6*mida+9]='s;1000;1000' m2-pantalla[6*mida+9]='.'
Solució amb funcions fins a interacció amb soldats:
#!/bin/bash ## Declaració de variables tecla=1 y=$(cat pantalla$1 | grep @ -n | cut -f1 -d":") x=$(cat pantalla$1 | grep @ | tr " " "\n" | grep -n @ | cut -f1 -d":") jugador=$(cat pantalla$1 | grep @ | cut -f$x -d" ") x=$((x-1)) y=$((y-1)) pantalla=($(cat pantalla$1 | tr "\n" " ")) mida=$(cat pantalla$1 | head -1 | wc -w) inventari="" clear ## Declaració de funcions function moviment { dx=$1 dy=$2 destix=$((x+dx)) destiy=$((y+dy)) desti=${pantalla[$((destiy*mida+destix))]} if [[ "$desti" == '.' ]] then pantalla[$((destiy*mida+destix))]=$jugador pantalla[$((y*mida+x))]='.' x=$destix y=$destiy fi if [[ $desti == $\;[0-9] ]] then pantalla[$((destiy*mida+destix))]=$jugador pantalla[$((y*mida+x))]='.' x=$destix y=$destiy inventari="$inventari $desti" fi } function interaccio { dx=$1 dy=$2 elementx=$((x+dx)) elementy=$((y+dy)) enemic=${pantalla[$((elementy*mida+elementx))]} if [[ "$enemic" == s* ]] # si és un soldat then soldat=$enemic arma_soldat=$(echo $soldat | cut -d";" -f3) vida_soldat=$(echo $soldat | cut -d";" -f2) arma_jugador=$(echo $jugador | cut -d";" -f3) vida_jugador=$(echo $jugador | cut -d";" -f2) vida_jugador=$(($vida_jugador-$arma_soldat)) vida_soldat=$(($vida_soldat-$arma_jugador)) soldat="s;$vida_soldat;$arma_soldat" jugador="@;$vida_jugador;$arma_jugador" if [ $vida_soldat -le 0 ] then soldat='.' fi if [ $vida_jugador -le 0 ] then echo "Has mort!!" exit fi pantalla[$((elementy*mida+elementx))]=$soldat fi } ## Joc principal while [[ $tecla != 0 ]] do echo -en "\e[s\e[0;0H\n$(echo ${pantalla[*]} | sed "s/\(\([^ ]\+ \)\{$mida\}\)/\1\n/g" | sed 's/\(.\)\(;[0-9]\+\)\+/\1/g' | tr -d " " | sed -e 's/#/\\e[32m#\\e[0m/g' -e 's/@/\\e[31m@\\e[0m/g')\n\nJugador:$jugador Inventari:$inventari\e[u" read -s -n 1 tecla case $tecla in a) moviment -1 0 ;; d) moviment 1 0 ;; w) moviment 0 -1 ;; s) moviment 0 1 ;; *) echo "La tecla $tecla no val per a res" clear ;; esac interaccio 0 1 interaccio 0 -1 interaccio 1 0 interaccio -1 0 done
Solució amb portes i arquers
<nowiki>
- !/bin/bash
- Declaració de variables
tecla=1 pantalla_actual=pantalla$1 y=$(cat $pantalla_actual | grep @ -n | cut -f1 -d":") x=$(cat $pantalla_actual | grep @ | tr " " "\n" | grep -n @ | cut -f1 -d":") jugador=$(cat $pantalla_actual | grep @ | cut -f$x -d" ") x=$((x-1)) y=$((y-1)) pantalla=($(cat $pantalla_actual | grep '^#' | tr "\n" " ")) mida=$(cat $pantalla_actual | head -1 | wc -w) inventari="" fletxes="0" dispar_automatic=0 clear
- Declaració de funcions
function moviment { dx=$1 dy=$2 destix=$((x+dx)) destiy=$((y+dy)) desti=${pantalla[$((destiy*mida+destix))]} if "$desti" == '.' then pantalla[$((destiy*mida+destix))]=$jugador pantalla[$((y*mida+x))]='.' x=$destix y=$destiy fi
if [[ $desti == $\;[0-9] ]] then pantalla[$((destiy*mida+destix))]=$jugador pantalla[$((y*mida+x))]='.' x=$destix y=$destiy inventari="$inventari $desti" fi
if [[ $desti == ♡\;[0-9][0-9] ]] # vida then pantalla[$((destiy*mida+destix))]=$jugador pantalla[$((y*mida+x))]='.' x=$destix y=$destiy arma_jugador=$(echo $jugador | cut -d";" -f3) vida_jugador=$(echo $jugador | cut -d";" -f2) vida=$(echo $desti | cut -d";" -f2) vida_jugador=$(($vida_jugador+$vida)) jugador="@;$vida_jugador;$arma_jugador" fi
if $desti == 𝀃 # arma
then
pantalla[$((destiy*mida+destix))]=$jugador
pantalla[$((y*mida+x))]='.'
x=$destix
y=$destiy
arma_jugador=$(echo $jugador | cut -d";" -f3)
vida_jugador=$(echo $jugador | cut -d";" -f2)
arma_jugador=$(($arma_jugador+20))
jugador="@;$vida_jugador;$arma_jugador"
fi
}
function interaccio { dx=$1 dy=$2 elementx=$((x+dx)) elementy=$((y+dy))
enemic=${pantalla[$((elementy*mida+elementx))]}
echo -en "\e[s\e[$((dy+2));$(($mida+25+($dx*5)))H$enemic\e[u" echo -en "\e[s\e[2;$(($mida+25))H@\e[u"
if [[ "$enemic" == [sa]* ]] # si és un soldat (o arquer a curta distància) then
soldat=$enemic arma_soldat=$(echo $soldat | cut -d";" -f3) if [ "$enemic" == a* ] then arma_soldat=0; fi vida_soldat=$(echo $soldat | cut -d";" -f2)
arma_jugador=$(echo $jugador | cut -d";" -f3) vida_jugador=$(echo $jugador | cut -d";" -f2)
vida_jugador=$(($vida_jugador-$arma_soldat)) vida_soldat=$(($vida_soldat-$arma_jugador))
# Experiència arma_jugador=$(($arma_jugador+1))
soldat="s;$vida_soldat;$arma_soldat" jugador="@;$vida_jugador;$arma_jugador"
if [ $vida_soldat -le 0 ] then soldat='.' fi
if [ $vida_jugador -le 0 ] then echo "Has mort!!" exit fi
pantalla[$((elementy*mida+elementx))]=$soldat fi
if "$enemic" == +* # si és una porta then
porta=$enemic n_porta=$(echo $porta | cut -d";" -f2)
if echo $inventari | grep "$;$n_porta" > /dev/null then porta='.' fi
pantalla[$((elementy*mida+elementx))]=$porta
fi
}
function arquers { dx=$1 dy=$2 elementx=$((x+dx)) elementy=$((y+dy)) distancia=$((${dx#-}+${dy#-}))
pos_element=$((elementy*mida+elementx)) if [ $pos_element -ge 0 ] && [ $distancia -gt 2 ] # pot eixir de l'array then enemic=${pantalla[$pos_element]}
if "$enemic" == a* # si és un arquer then arquer=$enemic arma_arquer=$(echo $arquer | cut -d";" -f3)
arma_jugador=$(echo $jugador | cut -d";" -f3) vida_jugador=$(echo $jugador | cut -d";" -f2) vida_jugador=$(($vida_jugador-$arma_arquer)) jugador="@;$vida_jugador;$arma_jugador"
if [ $vida_jugador -le 0 ] then echo "Has mort!!" exit fi fi
fi
}
- Joc principal
while $tecla != 0 do
echo -en "\e[s\e[0;0H\n$(echo ${pantalla[*]} | sed "s/\(\([^ ]\+ \)\{$mida\}\)/\1\n/g" | sed 's/\(.\)\(;[0-9]\+\)\+/\1/g' | tr -d " " | sed -e 's/#/\\e[32m#\\e[0m/g' -e 's/@/\\e[31m@\\e[0m/g')\n\nJugador:$jugador Inventari:$inventari $dispar_automaticᗡ;$fletxes \e[u" read -s -n 1 tecla echo -en "\e[s\e[0;0H \e[u" echo -en "\e[s\e[0;$(($mida+20))H \e[u" echo -en "\e[s\e[1;$(($mida+20))H \e[u" echo -en "\e[s\e[2;$(($mida+20))H \e[u" echo -en "\e[s\e[3;$(($mida+20))H \e[u"
case $tecla in [aA])
moviment -1 0
;; [dD])
moviment 1 0
;; [wW]) moviment 0 -1 ;; [sS]) moviment 0 1 ;; [fF]) dispar_automatic=1 ;; [gG]) dispar_automatic=0 ;; *) echo "La tecla $tecla no val per a res" clear ;; esac
interaccio 0 1 interaccio 0 -1 interaccio 1 0 interaccio -1 0
for i in -3 -2 -1 0 1 2 3 do for j in -3 -2 -1 0 1 2 3 do arquers $i $j done done
done
</nowiki