While command-line games can be interesting and even quite fun, users have come to expect a lot more from games in our current day and age. Going into any depth into high-end graphics games programming is beyond the scope of an introductory programming text. However, it is possible to give you a brief introduction into this exciting area of programming, and some suggestions as to where you can turn to explore this exciting topic in more depth.
For Windows programmers (you will be introduced to Windows programming in Chapters 15 and 16), you have an awesome tool at your disposal. That tool is the Direct X Library™. This library is a set of classes and functions that you can use to create a wide range of graphics and sounds operations. Essentially, Direct X is a multimedia library that is accessible to Windows programmers. If you have any experience playing Windows games then you probably have installed some game wherein the Direct X Library was installed as part of the games installation.
For those of you wanting to delve into graphics on a simpler level, you can try using ASCII graphics. ASCII graphics simply refers to creating graphics with simple ASCII symbols. In other words, simply use the symbols that your keyboard can produce to draw various items. Obviously such drawings will be rather simple, but it is a good place to start. The next example draws an arrow on the screen, using asterisks (*), and then moves the arrow across the screen.
Step 1: Enter the following code into your favorite text editor and save it as 14-02.cpp.
#include <iostream> using namespace std; //function prototypes void drawarrow(int); void movearrow(); int main() { for (int j = 0; j<15;) { drawarrow(j); j +=5; } cout << endl<<endl<<endl; return 0; }//end of main void drawarrow(int offset) { int i; // this is used to draw the top part of // the arrow head. for (i = 0;i < (8 + offset);i++) { cout << ' '; } cout << '*'; cout << endl; // this loop shifts the shaft of // the arrow, enough spaces to match // the offset for (i = 0;i<offset;i++) { cout << ' '; } // this draws the shaft for(i = 0;i<10;i++) { cout << '*'; } // this moves to a new line cout << endl; // this is used to draw the bottom part of // the arrow head. for (i = 0;i < (8 + offset);i++) { cout << ' '; } cout << '*'; cout << endl; }// end of drawarrow
Step 2: Compile and run the program. You should see an image like the one depicted in Figure 14.4.
Figure 14.4: The asterisk arrow.
This simple graphics illustration illustrates how a little creativity can allow you to create moving graphics from simple keyboard symbols. You also see that loops are used extensively for drawing the arrow.