As a participant in a Coding Nomads bootcamp, one of the early projects in the Python course is a simple Dungeon and Dragons command line game. The objective: find a sword and slay a dragon. The command line game is driven by user input (responding to presented binary choices; e.g., yes / no, fight / safety). In addition to user input, flag variables are essential in the code to both keep track of where the player is located in the game (i.e., player_pos = "c" ("c" = corridor; "l" = left (empty) room; "r" = right (dragon) room), whether the player has the sword, and finally, whether the dragon is alive. The sword is initially hidden in the empty room on the left, and is tracked via the flag "has_sword." Initially set to "False", when the sword is looked for and found, the flag switches to "True". The status of the dragon is tracked via the Boolean flag variable, "dragon_dead = False". In addition to tracking the dragon's status, this Boolean flag also keeps the player within a while loop where the player can move between the corridor, the left room, and the right room. In order to claim victory in this game, the player must find and take the sword, then enter the dragon's lair, and chose to fight it. Alternatively, if the player choses to fight the dragon without the sword, they will meet their demise in the command line game. Once the dragon_dead Boolean flag variable flips to "True", the code exits from the while loop, and the player can claim victory.
Again, this was an early in my Python learning, but the project emphasized the utility that flag variables offer in coding. Additionally, to get the script to operate fluidly, nested loops had to be constructed and carefully place to ensure the player could not backtrack and repeat previous actions they already took.
Top comments (0)