Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Как написать игру на BASH

Как написать игру на BASH

Исходный код тут
https://github.com/Andrew8xx8/shell-games

Andrew Kulakov

April 11, 2015
Tweet

More Decks by Andrew Kulakov

Other Decks in Programming

Transcript

  1. *------------------------* |........................| |........................| |........................| $$$$$$\ $$$$$$$$\ $$$$$$\ $$\ $$\ $$\

    $$\ $$$$$$\ |................#.......| $$ __$$\\__$$ __|$$ __$$\ $$ | $$ |$$ | $$ |$$ __$$\ |.............#####:.....| $$ / \__| $$ | $$ / $$ |$$ | $$ |$$ |$$ / $$ / $$ | |..........#########::...| $$ | $$ | $$$$$$$$ |$$$$$$$$ |$$$$$ / $$$$$$$$ | |.......#############:::.| $$ | $$ | $$ __$$ |\_____$$ |$$ $$< $$ __$$ | |..#.#################:::| $$ | $$\ $$ | $$ | $$ | $$ |$$ |\$$\ $$ | $$ | |..####################::| $$ | $$\ $$ | $$ | $$ | $$ |$$ |\$$\ $$ | $$ | |..::::::::::::::::::::::| \______/ \__| \__| \__| \__|\__| \__|\__| \__| |....::::::::::::::::::::| |......::::::::::::::::::| |........::::::::::::::::| *------------------------* $$\ /$$\ $$$$$$$$|$$$$$$$\ $$\ $$\ $$ |/$$$ |$$ ____|$$ __$$\ $$ | $$ | $$ /$$$$ |$$ | $$ | $$ |$$ | |$$ | $$/$$/$$ |$$ | $$$$$$$ |$$$$$$$\ |$$ | $$$$/ $$ |$$ | $$ ____/ $$ __$$\ $$ | $$$| $$ |$$ | $$ | $$ | $$ |$$ | $$ | $$ |$$ | $$ | $$ | $$ |$$ | \__| \__|\__| \__| \_______/ \__| $$\ $$\ $$$$$$\ $$ | $$ |$$ __$$\ $$ | $$ |$$ / $$ | $$$$$$$$ |$$$$$$$$ | $$ __$$ |$$ __$$ | $$ | $$ |$$ | $$ | $$ | $$ |$$ | $$ | \__| \__|\__| \__| $$$$$$$\ $$$$$$\ $$$$$$\ $$\ $$\ $$ __$$\ $$ __$$\ $$ __$$\ $$ | $$ | $$ | $$ |$$ / $$ |$$ / \__|$$ | $$ | $$$$$$$\ |$$$$$$$$ |\$$$$$$\ $$$$$$$$ | $$ __$$\ $$ __$$ | \____$$\ $$ __$$ | $$ | $$ |$$ | $$ |$$\ $$ |$$ | $$ | $$$$$$$ |$$ | $$ |\$$$$$$ |$$ | $$ | \_______/ \__| \__| \______/ \__| \__|
  2. __,--"""""""""--,. _ -'" _\ ^-,_ ,-" _/ \_ , /

    \ \ ,' /_ | \ / _____,--""" / ) \ / / / ( | | / / ) | | / | \ ( (_/\ ) / \ \ \_ ____,====""" / | \ /" /"" | \_ _,-" |___,-'--------'" | "`------"" --" ,-' / / ---" / \___/ __,-----,___ ) \ ,--'"============""""-'" "-'" | |=================/ /___\===============/ -Joachim Hoffmueller- / |=============/" \ \_________,-" | | | | Jo.
  3. $_

  4. 8xx8@nastachku ~ $ man stty ! NAME ! ! stty

    - печатает или изменяет параметры терминала ! ! Лоакльные установки: ! ! echo (-echo) отображать или нет вводимые символы
  5. 8xx8@nastachku ~ $ man tput ! NAME ! ! tput

    - изменяет параметры терминала ! EXAMPLES ! ! tput cols Выводит кол-во символов в строке окна терминала ! tput lines Выводит кол-во строк в окне терминала
  6. tput civis Скрывает курсор ! tput cnorm Показывает курсор !

    tput cup Y X Поместить курсор в позицию X, Y на экране ! tput setaf COLOR Установить цвет символов
  7. 8xx8@nastachku ~ $ man bash | grep IFS ! $IFS

    ! Внутренний раздеитель полей Используется для определения границы слов. ! По умолчанию ``<space><tab><newline>’'. ! Мы установим его в ‘’ для того, чтобы выводить пустое пространство пустое
  8. 8xx8@nastachku ~ $ ( x=`tput op` y=`printf %$((${COLUMNS}-6))s`; for i

    in {0..256};do o=00$i; echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x; done; )
  9. 8xx8@nastachku ~ $ ( x=`tput op` y=`printf %$ ((${COLUMNS}-6))s`;for i

    in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done; ) 000 ========================================== 001 ========================================== 002 ========================================== 003 ========================================== 004 ========================================== 005 ========================================== 006 ========================================== 007 ========================================== 008 ========================================== 009 ========================================== 010 ========================================== 011 ========================================== 012 ========================================== 013 ==========================================
  10. 8xx8@nastachku ~ $ man bash | grep declare ! Arrays

    Bash поддерживает одномерные массивы. Любая переменная может быть использована как массив; Встроенная функция declare может отпределить массив явно. ! ! declare [-afFirtx] [-p] [name[=value] …] ! -a Определить массив
  11. 8xx8@nastachku ~ $ ./arrays-vs-strings.sh I = $(($X * $ROW +

    $Y)) ! real 0m21.090s user 0m20.166s sys 0m0.848s ! let "I = $X * $ROW + $Y" ! real 0m24.038s user 0m23.087s sys 0m0.871s ! I = 1$X0$Y ! real 0m17.223s user 0m16.531s sys 0m0.635s
  12. function calcIndex { echo $(($1 * $1)) } ! $(calcIndex

    $i) ! ! function calcIndex { return $(($1 * $1)) } ! calcIndex $i array[$i]=$?
  13. 8xx8@nastachku ~ $ ./function-vs-direct.sh Return by echo ! real 0m2.188s

    user 0m0.817s sys 0m1.298s ! Return by $? function ! real 0m0.117s user 0m0.111s sys 0m0.005s ! Calc direct ! real 0m0.073s user 0m0.069s sys 0m0.004s
  14. 8xx8@nastachku ~ $ man trap ! NAME ! trap -

    привязывает выполнение пользовательской функции и системному сигналу ! SYNOPSIS ! trap [COMMANDS] [SIGNALS] ! SIGNALS ! SIGALRM — сигнал, посылаемый процессу по истечении времени, предварительно заданного функцией alarm().
  15. function move() { ( sleep 0.1; kill -ALRM $$ )

    & ! ... do something … ! } ! trap move ALRM ! move ! while : do # read 1 byte long cursor codes: read -rsn1 -d '' key case "$key" in ... keys ... esac done
  16. X=0 Y=10 function move() { (sleep 1 && kill -ALRM

    $$) & ! tput cup $Y $X echo " " X=$(($X + 1)) tput cup $Y $X echo "*" } ! trap move ALRM ! move ! while : do read -rsn3 -d '' -n 1 key done
  17. if [ $ballY -gt $((BOTTOM - 1)) ] then if

    [ $ballX -le $((plateX + plateW)) ] && [ $ballX -ge $plateX ] then ballX=$((plateX + plateW / 2)) ballDY=$((-ballDY)) ballY=$((ballY + ballDY + ballDY)) else state='stop' lifes=$((lifes - 1)) fi fi
  18. ( . ) __ __ __ __ ( . )

    / o \ \ \/ / \ \/ / / o \ \__ __//__/\__\/__/\__\\_____/ *---------------------* | | | @8xx8 | | http://8xx8.ru | | | | Стачка, 2015 | *---------------------*
  19. *------------------------* |........................| |........................| |........................| $$$$$$\ $$$$$$$$\ $$$$$$\ |................#.......| $$ __$$\\__$$

    __|$$ __$$\ |.............#####:.....| $$ / \__| $$ | $$ / $$ | |..........#########::...| $$ | $$ | $$$$$$$$ | |.......#############:::.| $$ | $$ | $$ __$$ | |..#.#################:::| $$ | $$\ $$ | $$ | $$ | |..####################::| $$ | $$\ $$ | $$ | $$ | |..::::::::::::::::::::::| \______/ \__| \__| \__| |....::::::::::::::::::::| |......::::::::::::::::::| |........::::::::::::::::| *------------------------* ------------* ............| ............| ............| $$$$$$\ $$$$$$$$\ $$$$$$\ $$\ $$\ $$\ $$\ $$$$$$\ ....#.......| $$ __$$\\__$$ __|$$ __$$\ $$ | $$ |$$ | $$ |$$ __$$\ .#####:.....| $$ / \__| $$ | $$ / $$ |$$ | $$ |$$ |$$ / $$ / $$ | #######::...| $$ | $$ | $$$$$$$$ |$$$$$$$$ |$$$$$ / $$$$$$$$ | ########:::.| $$ | $$ | $$ __$$ |\_____$$ |$$ $$< $$ __$$ | #########:::| $$ | $$\ $$ | $$ | $$ | $$ |$$ |\$$\ $$ | $$ | ##########::| $$ | $$\ $$ | $$ | $$ | $$ |$$ |\$$\ $$ | $$ | ::::::::::::| \______/ \__| \__| \__| \__|\__| \__|\__| \__| ::::::::::::| ::::::::::::| ::::::::::::| ------------*