Roguelike amb scripts

De Jose Castillo Aliaga
Ir a la navegación Ir a la búsqueda

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

#!/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 | 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


Amb fletxes

#!/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 -v '^m' | 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]* ]] # 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


	if [[ $desti == ᗡ\;[0-9]* ]] # arc
	then
	pantalla[$((destiy*mida+destix))]=$jugador
	pantalla[$((y*mida+x))]='.'
	x=$destix
	y=$destiy
	cant=$(echo $desti | cut -d";" -f2)
	fletxes=$(($fletxes+$cant))
	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

if [[ "$enemic" == m* ]]   # si és un mag
then

	mag=$enemic
	n_mag=$(echo $mag | cut -d";" -f2)

	script_mag=$(cat $pantalla_actual | grep "^m$n_mag" | cut -d"-" -f2)
	eval $script_mag
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


if [[ "$enemic" == [as]* ]] && [ $fletxes -gt 0 ] && [ $dispar_automatic -eq 1 ]  # si és un arquer o soldat puc atacar el fletxes
then

	arma_jugador=$(echo $jugador | cut -d";" -f3)
	tipus_enemic=$(echo $enemic | cut -d";" -f1)
	arma_enemic=$(echo $enemic | cut -d";" -f3)
	vida_enemic=$(echo $enemic | cut -d";" -f2)
	vida_enemic=$(($vida_enemic-($arma_jugador/10)))
	
	# Experiència
	arma_jugador=$(($arma_jugador+1))

	enemic="$tipus_enemic;$vida_enemic;$arma_enemic"

	if [ $vida_enemic -le 0 ]
	then
	enemic='.'
	fi

	pantalla[$((elementy*mida+elementx))]=$enemic
	fletxes=$(($fletxes-1))
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