Bash Executing ASCII

I started learning vim and started commanding the shell of a bash game similar to the classic 1978 Space Invaders. I am relatively new to ASCII art, and I would like to know if it is better to programmatically define contents such as a spaceship, or read the contents of a symbol from a file? If so, what would you recommend and how would you solve it programmatically?

#!/bin/bash function getShip() { declare -r FILENAME=/Users/demetriusford/space_ship for index in $FILENAME; do cat $index done } 

Inside the file:

  * /\ * !__/()\__! /==: :==\ (/\/\) 
+4
source share
2 answers

Even though you win the 2013 โ€œworst-ever inconsistency between programming language and programโ€ award, you will fight against rivals such as an x86-based accounting package or a COBOL-based operating system, or anything written in Pascal :-), it will probably be easier for you to simply encode this form directly in a bash script.

It seems that there is no need to separate the form from the code itself.


For the starter, here is a little script that uses tput (curses and terminfo database) to animate your ship at the bottom of the window.

This is not exactly World of Warcraft , but it should be a good starting point. First, we will set up material that never changes, ship strings and bullets, as well as various elements of the screen coordinates:

 #!/bin/bash bull1=' * * ' bull2=' ' ship1=' ^ /\ ^ ' ship2=' !__/()\__! ' ship3=' /==: :==\ ' ship4=' (/\/\) ' tput clear ((line1 = $(tput lines) - 6)) ((line2 = line1 + 1)) ((line3 = line2 + 1)) ((line4 = line3 + 1)) ((maxcol = $(tput cols) - 15)) 

Then we initialize the corresponding variable and introduce an infinite loop:

 ((bullline = 1)) ((bullcol = 1)) ((curcol = 1)) ((coldir = 1)) while true ; do 

Inside the loop, we simply move the sprites, quenching previous values โ€‹โ€‹and writing new ones. Bullet positions are updated in this section. Please note that we do not need to clear the ship explicitly, as it is surrounded by spaces, which border symbols fade in any case:

  tput cup $bullline $bullcol ; echo "$bull2" if [[ $bullline -le 2 ]] ; then ((bullline = line1 - 1)) ((bullcol = curcol)) else ((bullline = bullline - 2)) fi tput cup $bullline $bullcol ; echo "$bull1" tput cup $line1 $curcol ; echo "$ship1" tput cup $line2 $curcol ; echo "$ship2" tput cup $line3 $curcol ; echo "$ship3" tput cup $line4 $curcol ; echo "$ship4" 

Then we just update the variables for the ship so that they move left and right, finally lingering a bit so that the animation is smooth:

  ((curcol = curcol + coldir)) if [[ $curcol -eq $maxcol ]] ; then ((coldir = -coldir)) else if [[ $curcol -eq 1 ]] ; then ((coldir = -coldir)) fi fi sleep 0.1 done 

As a result, you get an animation that looks tolerable, given the limitations of the platform:

  * * * 

         ^ / \ ^ 
         ! __ / () \ __! 
         / ==:: == \ 
           (/ \ / \) 
+2
source

Games usually have two approaches:

  • Vector graphics: definition of an object by lines, circles, triangles, etc.
  • Images. Identify the object from the image.

Obviously, in both cases, the definition of the object is read from files (otherwise you need to recompile to change the art). With vector graphics you get a much better resolution, while with images you can have more beautiful art. Note that at the end, as a rule, the first method also has involved images (called "textures").

In the case of ASCII art, perhaps the images (ASCII art are โ€œdrawnโ€ in the file) are better, since you donโ€™t have much resolution anyway, and itโ€™s quite difficult to get beautiful ASCII art by defining lines and circles.

PS bash script for the game is interesting and very unusual!

0
source

All Articles