Ascii Art
Objectives
- Get more comfortable with nesting loops
Background
Before modern graphics were feasible, a lot of visuals were creating using just text. You can take a look at some examples here.
Do not use string multiplication ('hi' * 3) to solve these problems. The only valid use of string multiplication is to create spacing (' ' * 5), e.g. in the printDiamond function.
Exercises
To get spaces between the characters that make up your shapes, you can simply append a space character to the passed symbol(s) before printing anything. The right-most characters will have a trailing space, but we’re going to rely on the fact that they’re invisible and ignore them.
Problem 1 Write the printRect function.
printRect takes three arguments (width, height, symbol) and prints a width-by-height rectangle of symbols.
Here is some example output:
Problem 2 Write the printTri function.
printTri takes three arguments (width, symbol, rightSideUp) and prints a right triangle of symbols whose legs are width long. rightSideUp is a Boolean that determines whether the triangle is upside-down or not.
Here is some example output:
Problem 3 Write the printBumps function.
printBumps takes three arguments (num, symbol1, symbol2) and prints num successively larger bumps, starting with a height of 1.
Do you see any triangles inside the bumps?
Problem 4 Write the printDiamond function.
printDiamond takes two arguments (width, symbol) and prints a width-wide diamond of symbols.
Here is some example output:
Optional Bonus Challenge
In the following problems, we are going to use a list of lists to represent a 2D grid. Elements in the outer list are lists that represent the rows, and elements in the inner lists are the data that populates each row (and therefore the whole grid). The data in the inner lists, in this case, are symbols that we’ll be printing on the screen. For example, we could change the printRect function to return a list of lists rather than print immediately.
Problem 1 Rewrite printRect as genRect. genRect takes the same arguments, but instead of printing to the screen, returns a list of lists that corresponds to what printRect would’ve printed.
Problem 2 Write the function display(L) that prints a list of lists L. Remember each inner list (i.e. an element of the outer list L) represents a row. That is, the first element in L is a list of characters that corresponds to the first row of the ascii art to be printed. Unlike previously, don’t print a space between each character.
The following two should have equivalent behavior:
printRect(4, 6, '%')display(genRect(4, 6, '%'))
Problem 3 Write an animate(frames) function that takes one argument (frames) that is a list containing list of lists and continuously displays the elements of frames in order, over and over. In between each frame, use time.sleep(s) to pause for s seconds. Remember, you’ll need to import the time package to do so.
Print some large number of blank lines right before you print each frame, as well. This is just to make the animation easier to look at in the terminal. It might be a good idea to run this file in a standalone terminal (outside of vscode) for easier viewing.
When you have your functions written, try running the following:
Try making your own little animation, too!