#! /bin/bash # # cassebriq.sh : Un cassebrique (sans briques) en bash # # Edouard.Thiel@lif.univ-mrs.fr - 25/09/2014 # # This program is free software under the terms of the # GNU Lesser General Public License (LGPL) version 2.1. init_jeu () { ncols=$(tput cols) nlines=$(tput lines) raq_t="=========" raq_l=${#raq_t} raq_x=$(( (ncols-raq_l)/2 )) } init_balle () { bal_x=$((ncols/2)) bal_y=$((nlines/2)) bal_dx=1 bal_dy=-1 continuer=true } deplacer_gauche () { ((raq_x-=2)) if ((raq_x < 0)); then raq_x=0; fi } deplacer_droite () { ((raq_x+=2)) if ((raq_x >= ncols-raq_l)); then raq_x=$((ncols-raq_l-1)); fi } afficher_raquette () { tput cup $((nlines-2)) $raq_x printf "%s" "$raq_t" } placer_curseur_en_bas () { tput cup $((nlines-1)) 0 } afficher_balle () { tput cup $bal_y $bal_x printf "O" } afficher_aide () { tput cup 0 0 printf "a: gauche z: droite n: nouveau jeu q: quitter" } afficher_perdu () { tput cup $((nlines/2)) $((ncols/2-7)) printf "PERDU !" } terminer () { echo echo "Merci d'avoir joué à ${0##*/}" } progresser_balle () { ((bal_x += bal_dx)) ((bal_y += bal_dy)) if ((bal_x < 0)); then bal_x=0; ((bal_dx = -bal_dx)) elif ((bal_x >= ncols)); then bal_x=$((ncols-1)); ((bal_dx = -bal_dx)) fi if ((bal_y < 0)); then bal_y=0; ((bal_dy = -bal_dy)) elif ((bal_y == nlines-2)); then if ((bal_x >= raq_x && bal_x <= raq_x+raq_l)); then bal_y=$((nlines-3)); ((bal_dy = -bal_dy)) else continuer=false fi fi } afficher_tout () { tput clear afficher_aide afficher_raquette afficher_balle if ! $continuer ; then afficher_perdu ; fi placer_curseur_en_bas } progresser_jeu () { if $continuer ; then progresser_balle ; fi afficher_tout } init_jeu init_balle trap terminer EXIT trap progresser_jeu USR1 ( while true ; do kill -USR1 $$ >/dev/null 2>&1 || exit 0 sleep 0.05 done ) & while read -n 1 -s c ; do case "$c" in a) deplacer_gauche ;; z) deplacer_droite ;; n) init_balle ;; q) break ;; esac done exit 0