How to make a cmd text based game
The command line interface knows this because "north" is the only command that begins with "nor". If there were other commands that began with "nor", pressing Tab would bring up a list of possible complete commands.
However, say that the player has an inventory value of ['sword', 'swingset', 'swampwater'] and had entered "drop sw" and pressed Tab. But items on the ground or in the inventory are specific to your program, so you will need a way to tell the command line interface what it should return for possible tab completions.
These methods have the following parameters:. Your program will look like this file on GitHub. When you run the program, you will be able to type "drop sw", then press Tab, and the command will complete to "drop sword". Typing "look" will print the current room's DEC key's value.
Typing "look exits" will print the names of all the adjacent rooms, while "look " will print the name of the room in that direction. The player can also type "look " to look at a item that is either on the ground or in their inventory.
The program will now look like this file on GitHub. The value for the SHOP is a list of items that the shop sells.
Inside these rooms, the player can run the "list" command to see what is for sale , the "buy" command to purchase an item , and the "sell" command to pawn an item from the player's inventory. For simplicity, any item can be sold to any shop. Also, money is currently not implemented in this game, so items in the shop are free.
Several things must be checked when the player tries to run these commands: Is the player currently in a shop room? Did the player forget to specify what they want to buy or sell? Does the shop sell what the player wants to buy? Does the player have the item they want to sell to the shop? The following code implements the "list", "buy", and "sell" commands and addresses all these issues. When you are finished typing in this code, your program will look like this file on GitHub. For simplicity, eating things doesn't do anything other than remove them from your inventory.
But you could implement health or hunger levels in your text adventure, and need eat or drink items or else have adverse effects. With the above code added, you will have finished the entire text adventure program. That's it for this text adventure tutorial. Because of the cmd module's command line interface features, it is fairly easy to add new commands to your game. From here, there are several things you could add to your game:.
If you are interested in creating a roguelike a genre that is sort of like the Diablo games except with ASCII-art graphics , the libtcod module will be very helpful. There are tutorials here and here. I detail the differences between these genre of text-based games in my blog post, Text Adventure vs.
MUD vs. Roguelike vs. Dwarf Fortress. You can try playing MUDs you find through The Mud Connector to get new ideas for additional features you'd like to add. Good luck, and have fun! Subscribe to the weekly mailing list for news and updates:. Toggle navigation. The cmd module provides a generic command-line interface that has several useful features: Tab-completion - Type a partial command and press tab, and the game can guess the rest of the command. History browsing - Press the up arrow key to bring up the previously entered commands.
Automatic help - A help system for commands is automatically generated for your commands. Demo of Game Play Here's what our text adventure game will look like. Text Adventure Demo! A welcome sign stands here. A bubbling fountain of green water. A sign stands here, not bolted to the ground. The "Choose Your Own Adventure" Mistake Beginners tend to write code that looks like a "Choose Your Own Adventure" book: the execution starts at the beginning of the program, then it jumps to another part of the code just like the reader would turn to some page at a branching point in the book.
You won't have a townSquare function that displays the Town Square text when called like so: def townSquare : print 'The town square is a large open space with a fountain in the center. You can play Pacman4console right from your Linux terminal and set high scores.
Type pacman4console in your terminal to launch the game or press q to quit the application. Alienwave is a great variant of the famous space invaders game. Opponents have various shapes and the difficulty level is high.
Download the source from here , cd into your Downloads folder and type these commands. We can't get to this place and still not mention chess. You can play chess against the computer or use it as a chess engine for graphical chess frontends such as XBoard. Yes, you only live once. This is a Seven Day Roguelike game which concentrates on telling a simple story and engaging the user in simple tactical combat.
You're engaged in a very quick to complete tactical combat with heavy plot and characterization. Get the most recent version from here , cd into your Downloads folder and run these commands. You'll find a number of games here including air traffic controller, hangman, robots and morse. You can play all 40 games included in this package by typing their respective names, eg hangman , quiz , robots , etc.
How about some cool rain in your console? Ascii-rain is a C-based 'game' written with Ncurses, which displays rainfall inside your terminal. There's no interaction with this game - just make it rain. Ascii-rain is available from its GitHub repo. You'll need a ncurses library in order to make it rain. Press any key to return to the main screen. Rear echo B.
Front echo C. Left echo D. Serve as ballast on submersible cars echo B. Remove dust from the car by using little fans echo C. Create a winning screen. Creating a winning screen is as simple as the instructions screen. Create a text document with praise for winning and save it as WIN. Add the following lines to the end of your game for the winning screen:. Your game should now look like the code below:. Touch up your file.
Start by going to each label and placing the cls command after it. This will clear the screen at each label so you don't have a screen full of unnecessary information. Then correct grammar where appropriate. If you want, make all of the answers on the list complete sentences.
These symbols will create a syntax error that causes the program to stop, lag, or crash. Create graphics for the game if you would like. Generate ASCII art in separate text documents and use the type command to display them in the program:.
Then correct any typing errors that you can find. Think of your own things to correct. Then add your color with the color command. It is suggested that you place it at the beginning of the program so that the whole program is of this color. Here is the explanation of how to use it directly from the command line:. Sets the default console foreground and background colors.
COLOR [attr]. Color attributes are specified by TWO hex digits -- the first corresponds to the background; the second the foreground. Each digit can be any of the following values:.
If no argument is given, this command restores the color to what it was when CMD. EXE started. In other words, if you wanted a bright white background and black text:. Congratulations, you have just created a basic computer game with batch script.
0コメント