Skip to content

Latest commit

 

History

History
96 lines (73 loc) · 4.08 KB

File metadata and controls

96 lines (73 loc) · 4.08 KB

十五、深坑

原文:http://inventwithpython.com/bigbookpython/project15.html

这个节目是一个永远深入地下的深洞的动画。虽然很短,但这个程序利用了计算机屏幕的滚动特性,产生了一个有趣且无休止的可视化效果,证明了制作有趣的东西并不需要太多代码。这个项目类似于 58 项目“彩虹”

运行示例

当您运行deepcave.py时,输出如下:

Deep Cave, by Al Sweigart email@protected
Press Ctrl-C to stop.
####################          ########################################
####################         #########################################
####################          ########################################
####################          ########################################
#####################          #######################################
######################          ######################################
#####################          #######################################
####################          ########################################
###################          #########################################
`--snip--`

工作原理

这个程序利用了这样一个事实,即打印新行最终会导致前面的行在屏幕上上移。通过在每行上打印一个稍微不同的间隙,程序创建了一个滚动动画,看起来好像观众在向下移动。

左侧的井号字符数由leftWidth变量跟踪。中间的空格数由gapWidth变量跟踪。右侧标签字符的数量从WIDTH - gapWidth - leftWidth开始计算。这确保了每一行总是相同的宽度。

"""Deep Cave, by Al Sweigart email@protected
An animation of a deep cave that goes forever into the earth.
This code is available at https://nostarch.com/big-book-small-python-programming
Tags: tiny, beginner, scrolling, artistic"""


import random, sys, time

# Set up the constants:
WIDTH = 70  # (!) Try changing this to 10 or 30.
PAUSE_AMOUNT = 0.05  # (!) Try changing this to 0 or 1.0.

print('Deep Cave, by Al Sweigart email@protected')
print('Press Ctrl-C to stop.')
time.sleep(2)

leftWidth = 20
gapWidth = 10

while True:
    # Display the tunnel segment:
    rightWidth = WIDTH - gapWidth - leftWidth
    print(('#' * leftWidth) + (' ' * gapWidth) + ('#' * rightWidth))

    # Check for Ctrl-C press during the brief pause:
    try:
        time.sleep(PAUSE_AMOUNT)
    except KeyboardInterrupt:
        sys.exit()  # When Ctrl-C is pressed, end the program.

    # Adjust the left side width:
    diceRoll = random.randint(1, 6)
    if diceRoll == 1 and leftWidth > 1:
        leftWidth = leftWidth - 1  # Decrease left side width.
    elif diceRoll == 2 and leftWidth + gapWidth < WIDTH - 1:
        leftWidth = leftWidth + 1  # Increase left side width.
    else:
        pass  # Do nothing; no change in left side width.

    # Adjust the gap width:
    # (!) Try uncommenting out all of the following code:
    #diceRoll = random.randint(1, 6)
    #if diceRoll == 1 and gapWidth > 1:
    #    gapWidth = gapWidth - 1  # Decrease gap width.
    #elif diceRoll == 2 and leftWidth + gapWidth < WIDTH - 1:
    #    gapWidth = gapWidth + 1  # Increase gap width.
    #else:
    #    pass  # Do nothing; no change in gap width. 

在输入源代码并运行几次之后,尝试对其进行实验性的修改。标有(!)的注释对你可以做的小改变有建议。

探索程序

试着找出下列问题的答案。尝试对代码进行一些修改,然后重新运行程序,看看这些修改有什么影响。

  1. 如果把第 23 行的(' ' * gapWidth)改成('.' * gapWidth)会怎么样?
  2. 如果把第 32 行的random.randint(1, 6)改成random.randint(1, 1)会怎么样?
  3. 如果把第 32 行的random.randint(1, 6)改成random.randint(2, 2)会怎么样?
  4. 如果删除或注释掉第 17 行的leftWidth = 20,会得到什么错误信息?
  5. 如果把第 10 行的WIDTH = 70改成WIDTH = -70会怎么样?
  6. 如果将第 11 行的PAUSE_AMOUNT = 0.05改为PAUSE_AMOUNT = -0.05,会得到什么错误信息?