bashrun and Beyond: Building an Advanced Terminal Game

By Amr

Starting with bashrun as a base and building upon it is a great way to create an advanced terminal-based game. bashrun is typically built using shell scripts, which can be a bit limiting but also provides a good foundation for learning and expanding.

Estimated reading time: 5 minutes

Edit on Github

Starting with bashrun as a base and building upon it is a great way to create an advanced terminal-based game. bashrun is typically built using shell scripts, which can be a bit limiting but also provides a good foundation for learning and expanding.

Here’s a step-by-step guide to start with bashrun and build upon it:

Step 1: Setting Up bashrun

  1. Clone the bashrun Repository: If bashrun is hosted on a repository, you can clone it using:
    git clone https://gitlab.com/slackermedia/bashrun.git bashrun
    cd bashrun
    
  2. Understand the Structure: Explore the directory structure and understand how the game is organized. Typically, it might include directories for maps, scripts for different levels, and resources for the game.

Step 2: Basic Improvements

  1. Enhance Storyline and Levels:
    • Add new levels with more complex puzzles and objectives.
    • Create more detailed and engaging storylines.

    Example of adding a new level (level2.sh):

    #!/bin/bash
    echo "Welcome to Level 2!"
    echo "You need to find the hidden key to unlock the treasure."
    
    # Player actions and logic here
    
  2. Improve Navigation and Interaction: Add more commands for interaction, such as examining objects or using items.

    Example of an interaction command:

    function examine {
        case $1 in
            "key")
                echo "It's a shiny golden key. It might unlock something."
                ;;
            "door")
                echo "It's a sturdy wooden door. It seems to be locked."
                ;;
            *)
                echo "You can't examine that."
                ;;
        esac
    }
    
  3. Add an Inventory System: Implement a simple inventory system to manage items collected by the player.

    Example inventory system:

    inventory=()
    
    function add_to_inventory {
        inventory+=("$1")
        echo "$1 added to your inventory."
    }
    
    function show_inventory {
        echo "Your inventory: ${inventory[@]}"
    }
    

Step 3: Advanced Features

  1. Implement Save/Load Functionality: Allow players to save their progress and load it later.

    Example save/load system:

    function save_game {
        echo "${inventory[@]}" > savefile.txt
        echo "Game saved."
    }
    
    function load_game {
        if [ -f savefile.txt ]; then
            inventory=($(cat savefile.txt))
            echo "Game loaded."
        else
            echo "No saved game found."
        fi
    }
    
  2. Add a Combat System: Create a simple combat system where players can encounter enemies and engage in battles.

    Example combat system:

    function combat {
        local enemy_health=10
        local player_health=20
    
        echo "You encounter an enemy!"
    
        while [ $enemy_health -gt 0 ] && [ $player_health -gt 0 ]; do
            echo "Choose an action: [attack/run]"
            read action
            case $action in
                "attack")
                    enemy_health=$((enemy_health - 2))
                    player_health=$((player_health - 1))
                    echo "You attack the enemy. Enemy health: $enemy_health"
                    echo "The enemy attacks you. Your health: $player_health"
                    ;;
                "run")
                    echo "You run away!"
                    return
                    ;;
                *)
                    echo "Invalid action."
                    ;;
            esac
        done
    
        if [ $player_health -le 0 ]; then
            echo "You have been defeated."
        else
            echo "You defeated the enemy!"
        fi
    }
    

Step 4: Polish and Expand

  1. Enhance User Interface: Improve the display and user interface to make the game more visually appealing. Use ASCII art for maps and objects.

  2. Add More Interactions and Puzzles: Introduce complex puzzles and more interactive elements to keep the game engaging.

  3. Create a Comprehensive Tutorial: Provide a detailed tutorial at the beginning to help new players understand the game mechanics.

  4. Test and Iterate: Continuously test your game and gather feedback. Use this feedback to make iterative improvements.

Example Directory Structure

bashrun/
├── maps/
│   ├── level1.txt
│   ├── level2.txt
├── scripts/
│   ├── level1.sh
│   ├── level2.sh
│   ├── common.sh  # Contains common functions like inventory and combat
├── resources/
│   ├── ascii_art/
│   ├── items.txt
├── savefile.txt
└── README.md

Example common.sh

#!/bin/bash

# Inventory system
inventory=()

function add_to_inventory {
    inventory+=("$1")
    echo "$1 added to your inventory."
}

function show_inventory {
    echo "Your inventory: ${inventory[@]}"
}

# Combat system
function combat {
    local enemy_health=10
    local player_health=20

    echo "You encounter an enemy!"

    while [ $enemy_health -gt 0 ] && [ $player_health -gt 0 ]; do
        echo "Choose an action: [attack/run]"
        read action
        case $action in
            "attack")
                enemy_health=$((enemy_health - 2))
                player_health=$((player_health - 1))
                echo "You attack the enemy. Enemy health: $enemy_health"
                echo "The enemy attacks you. Your health: $player_health"
                ;;
            "run")
                echo "You run away!"
                return
                ;;
            *)
                echo "Invalid action."
                ;;
        esac
    done

    if [ $player_health -le 0 ]; then
        echo "You have been defeated."
    else
        echo "You defeated the enemy!"
    fi
}

# Save/Load system
function save_game {
    echo "${inventory[@]}" > savefile.txt
    echo "Game saved."
}

function load_game {
    if [ -f savefile.txt ]; then
        inventory=($(cat savefile.txt))
        echo "Game loaded."
    else
        echo "No saved game found."
    fi
}

This guide should help you get started with building upon bashrun to create a more advanced and engaging terminal game. Enjoy the process and happy coding!