I’ve started learning vim and began shell scripting a game in bash similar to the 1978 classic, Space Invaders. I’m relatively new to ASCII art and I would like to know is it better to programmatically define contents such as a spaceship or read the character contents 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 of the file:
* /\ *
!__/()\__!
/==: :==\
(/\/\)
Despite the possibility you’ll win the “worst ever mismatch between programming language and program” award for 2013, going up against contenders like an accounting package written in x86, or a COBOL-based operating system, or anything written in Pascal :-), you’ll probably find it’s just easier to code that shape directly in your bash script.
There appears to be no pressing need to separate the shape from the code itself.
For a starter, here’s a little script that uses
tput(curses and the terminfo database) to animate your ship along the bottom of the window.It’s not exactly
World of Warcraftbut it should be a good starting point. First we’ll set up the stuff that never changes, ship and bullet strings, and various screen coordinate stuff:Then we’ll initialise the relevant variable and enter an infinite loop:
Inside the loop, we simply move the sprites by blanking the previous values and writing new ones. The bullet positions are updated in this section as well. Note that we don’t need to explicitly blank the ship since it’s surrounded by spaces which blanks the edge characters anyway:
Then we simply update variables for the ship to have it move left and right, finally delaying for a little bit so the animation is smooth:
What you end up with is an animation that looks bearable, given the limitations of the platform:
* * ^ /\ ^ !__/()\__! /==: :==\ (/\/\)