Turtle Graphics
Turtle Graphics
Your turtle graphics program will be relatively simple. You will create a
20x25 2D char array. The capital letter ‘O’ will represent the turtle and
lines will be created using the * symbol. As the turtle moves around the
screen it will leave the * symbol behind creating a trail of where it has
been.
Before starting this lab, select TDM-GCC 9.2.0 65-bit Debug at the
top of your compiler.
When the program begins, the turtle’s field is outlined with the = symbol
marking the top and bottom border of the field, and the | symbol marking the
left and right borders. The turtle is shown in its starting position of 1, 1. The
outline symbols are NOT part of the character array. Three rocks will be
randomly placed in the field.
The number pad can be used with keys, 8, 2, 4, and 6 to move the turtle
around the field to create drawings. These numbers correspond with the
arrows on the number pad of your keyboard.
To move the turtle around the screen, you will use the getch() method rather than the cin
command. The getch() will pause the program to wait for a keyboard input and it will return the key
entered as a char. If 1 is entered it will return ‘1’, if 2 is entered it will return ‘2’. This will allow you to
enter the direction without having to press the Enter key after each entry and easily move the turtle
around the field.
Part 1: Write the basic Turtle Graphics program.
1. You have been provided with the following starter file. Copy and paste this starter code into
your C++ Compiler.
int main()
{
char dir;
do{
//Show menu options
cout << "\t 8 - Move Up" << endl;
cout << "\t 2 - Move Down" << endl;
cout << "\t 4 - Move Left" << endl;
cout << "\t 6 - Move Right" << endl;
cout << "\t 0 - Exit" << endl;
if(dir != '0')
move(field, dir); //Move the turtle.
}while(dir != '0');
return 0;
//Function Definitions – TO DO
5. Complete the move function. This function will receive the desired move as the dir variable.
This function should do the following:
• Assign ‘*’ to the current position of the turtle.
• Modify the tr or td values based on the value of the dir variable. For example, if the turtle
should move up, reduce the tr value by 1.
• Assign the ‘O’ to the new position of the turtle.
• Call the display function to display the updated field.
The program has a major flaw in it! There is nothing that keeps the turtle from moving out of the
range of the field. For this part, you should incorporate a check feature that will verify that the move
is valid before moving the turtle. When the turtle hits a wall, it should no longer be able to move in
that direction. Test your program to make sure that the turtle remains within the borders of the field.
This function should create a “rock” and place it into the field at a
random location. Specifications of the rock:
• This function will be called from the initialize function.
• The rock should consist of four ‘X’ characters.
XX
XX
• The rock cannot be directly next to the border of the field, the
must be placed within a smaller range within the field so that
no rocks are spawned on top of the turtle or outside the field.
• Generate a random x and y value for the top left ‘X’ of the
rock.
• Place this rock into the field… add the ‘X’ values to the field
array.
Part 4: Modify the program so that the turtle can not walk through rocks.
• There are several ways you can do this. Use what you know to figure this out.
Bonus Challenge!
Create a Chaser Turtle: This turtle will be moved by the program. Specifications for the Chaser Turtle
are:
• The Chaser Turtle should be represented by the @ symbol.
• The Chaser Turtle should start at the bottom right corner of the field.
• It should make one move toward your turtle every time your turtle makes a move. It should
use the turtle’s current tr and tc values to determine it’s next move.
• The Chaser Turtle must follow the same move rules as the original turtle, can’t walk out of
range and can’t walk through rocks.
• The Chaser Turtle also can not walk through the trail left by your turtle.
• If the Challenge Turtle catches your turtle, the program should reset by calling the initialize
function.