A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://inventwithpython.com/bigbookpython/project15.html below:

Deep Cave

#15
Deep Cave

This program is an animation of a deep cave that descends forever into the earth. Although short, this program takes advantage of the scrolling nature of the computer screen to produce an interesting and unending visualization, proof that it doesn’t take much code to produce something fun to watch. This program is similar to Project 58, “Rainbow.”

The Program in Action

When you run deepcave.py, the output will look like this:

Deep Cave, by Al Sweigart [email protected]
Press Ctrl-C to stop.
####################          ########################################
####################         #########################################
####################          ########################################
####################          ########################################
#####################          #######################################
######################          ######################################
#####################          #######################################
####################          ########################################
###################          #########################################
--snip--
How It Works

This program takes advantage of the fact that printing new lines eventually causes the previous lines to move up the screen. By printing a slightly different gap on each line, the program creates a scrolling animation that looks like the viewer is moving downward.

The number of hashtag characters on the left side is tracked by the leftWidth variable. The number of spaces in the middle is tracked by the gapWidth variable. The number of hashtag characters on the right side is calculated from WIDTH - gapWidth - leftWidth. This ensures that each line is always the same width.

 1. """Deep Cave, by Al Sweigart [email protected]
 2. An animation of a deep cave that goes forever into the earth.
 3. View this code at https://nostarch.com/big-book-small-python-projects
 4. Tags: tiny, beginner, scrolling, artistic"""
 5.
 6.
 7. import random, sys, time
 8.
 9. # Set up the constants:
10. WIDTH = 70  # (!) Try changing this to 10 or 30.
11. PAUSE_AMOUNT = 0.05  # (!) Try changing this to 0 or 1.0.
12.
13. print('Deep Cave, by Al Sweigart [email protected]')
14. print('Press Ctrl-C to stop.')
15. time.sleep(2)
16.
17. leftWidth = 20
18. gapWidth = 10
19.
20. while True:
21.     # Display the tunnel segment:
22.     rightWidth = WIDTH - gapWidth - leftWidth
23.     print(('#' * leftWidth) + (' ' * gapWidth) + ('#' * rightWidth))
24.
25.     # Check for Ctrl-C press during the brief pause:
26.     try:
27.         time.sleep(PAUSE_AMOUNT)
28.     except KeyboardInterrupt:
29.         sys.exit()  # When Ctrl-C is pressed, end the program.
30.
31.     # Adjust the left side width:
32.     diceRoll = random.randint(1, 6)
33.     if diceRoll == 1 and leftWidth > 1:
34.         leftWidth = leftWidth - 1  # Decrease left side width.
35.     elif diceRoll == 2 and leftWidth + gapWidth < WIDTH - 1:
36.         leftWidth = leftWidth + 1  # Increase left side width.
37.     else:
38.         pass  # Do nothing; no change in left side width.
39.
40.     # Adjust the gap width:
41.     # (!) Try uncommenting out all of the following code:
42.     #diceRoll = random.randint(1, 6)
43.     #if diceRoll == 1 and gapWidth > 1:
44.     #    gapWidth = gapWidth - 1  # Decrease gap width.
45.     #elif diceRoll == 2 and leftWidth + gapWidth < WIDTH - 1:
46.     #    gapWidth = gapWidth + 1  # Increase gap width.
47.     #else:
48.     #    pass  # Do nothing; no change in gap width.

After entering the source code and running it a few times, try making experimental changes to it. The comments marked with (!) have suggestions for small changes you can make.

Exploring the Program

Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.

  1. What happens if you change (' ' * gapWidth) on line 23 to ('.' * gapWidth)?
  2. What happens if you change random.randint(1, 6) on line 32 to random.randint(1, 1)?
  3. What happens if you change random.randint(1, 6) on line 32 to random.randint(2, 2)?
  4. What error message do you get if you delete or comment out leftWidth = 20 on line 17?
  5. What happens if you change WIDTH = 70 on line 10 to WIDTH = -70?
  6. What error message do you get if you change PAUSE_AMOUNT = 0.05 on line 11 to PAUSE_AMOUNT = -0.05?

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4