好的,这是一个用ASCII字符绘制的动画,展示一只猫猫在雨中撑伞,雨滴动态下落并在伞上溅起水花。
这个程序使用Python编写,利用time.sleep来控制动画帧率,os.system来清空屏幕。
import os
import time
import random
# --- Constants ---
# 动画区域的宽度和高度
WIDTH = 40
HEIGHT = 18
# 帧延迟,控制动画速度(秒)
FRAME_DELAY = 0.1
# 最多允许的雨滴数量
MAX_DROPS = 35
# 水花持续的帧数
SPLASH_DURATION = 2
# --- Scene Element Positions ---
# 雨伞顶部的Y坐标 (UMBRELLA_Y_TOP)
# 雨伞体部的最低Y坐标 (UMBRELLA_Y_BOTTOM)
# 雨伞的X坐标范围 (UMBRELLA_X_MIN, UMBRELLA_X_MAX)
# 地面的Y坐标 (GROUND_Y)
UMBRELLA_Y_TOP = 2
UMBRELLA_Y_BOTTOM = 4 # The lowest part of the umbrella's 'body'
UMBRELLA_X_MIN = 11
UMBRELLA_X_MAX = 21 # Approximate span of the umbrella top for hit detection
GROUND_Y = 15
# --- Base Static Scene ---
# 这是一个列表,每行是一个字符串,代表静态背景
BASE_SCENE = [
" ", # 0
" ", # 1
" _.-"""-._ ", # 2 Umbrella top
" /_________\\ ", # 3
" `---------` ", # 4
" | ", # 5 Umbrella handle
" | ", # 6
" J ", # 7
" ", # 8
" ", # 9
" /\\_/\\ ", # 10 Cat head
" ( =^w^= ) ", # 11 Cat body
" \" \" ", # 12 Cat feet/base
" ", # 13 Space above ground
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", # 14 Ground (can be water/puddle)
" ", # 15
" ", # 16
" ", # 17
]
# 确保定义的宽度和高度与BASE_SCENE匹配
if len(BASE_SCENE[0]) != WIDTH or len(BASE_SCENE) != HEIGHT:
print("Warning: BASE_SCENE dimensions do not match WIDTH/HEIGHT constants.")
WIDTH = len(BASE_SCENE[0])
HEIGHT = len(BASE_SCENE)
# --- Helper Functions ---
def clear_screen():
"""清除终端屏幕,兼容Windows和Unix系统。"""
os.system('cls' if os.name == 'nt' else 'clear')
# --- Animation Elements Classes ---
class Raindrop:
"""表示一个雨滴。"""
def __init__(self, x, y):
self.x = x
self.y = y
def move(self):
"""将雨滴向下移动一格。"""
self.y += 1
class Splash:
"""表示一个水花。"""
def __init__(self, x, y, duration):
self.x = x
self.y = y
self.duration = duration
def decrease_duration(self):
"""减少水花的持续时间,如果持续时间大于0则返回True。"""
self.duration -= 1
return self.duration > 0
# --- Main Animation Loop ---
def main():
"""主动画循环。"""
raindrops = [] # 存储当前所有雨滴对象的列表
splashes = [] # 存储当前所有水花对象的列表
while True:
# 1. 创建当前帧的画布(复制基础场景)
# 将字符串列表转换为字符列表的列表,以便修改
current_frame_chars = [list(line) for line in BASE_SCENE]
# 2. 更新和绘制雨滴
new_raindrops = []
for drop in raindrops:
# 检查雨滴是否击中雨伞
if UMBRELLA_Y_TOP <= drop.y < UMBRELLA_Y_BOTTOM and UMBRELLA_X_MIN <= drop.x < UMBRELLA_X_MAX:
# 击中雨伞,生成水花
splashes.append(Splash(drop.x, UMBRELLA_Y_TOP, SPLASH_DURATION))
# 不再将这个雨滴添加到 new_raindrops,因为它“消失”了
elif drop.y < GROUND_Y: # 雨滴还在空中或刚击中地面但不是伞
# 绘制雨滴
if 0 <= drop.y < HEIGHT and 0 <= drop.x < WIDTH: # 确保在屏幕范围内
current_frame_chars[drop.y][drop.x] = '|'
drop.move() # 移动雨滴
new_raindrops.append(drop)
# 如果雨滴超过地面,它就自然消失了,不会被添加到 new_raindrops
raindrops = new_raindrops # 更新雨滴列表
# 3. 添加新的雨滴(如果需要)
# 随机决定是否生成新雨滴,并确保不超过最大数量
if len(raindrops) < MAX_DROPS and random.random() < 0.2: # 0.2 是生成概率,可调整
new_x = random.randint(0, WIDTH - 1)
# 避免在伞的顶部直接生成雨滴(会立即触发水花)
if not (UMBRELLA_Y_TOP <= 0 < UMBRELLA_Y_BOTTOM and UMBRELLA_X_MIN <= new_x < UMBRELLA_X_MAX):
raindrops.append(Raindrop(new_x, 0))
# 4. 更新和绘制水花
new_splashes = []
for splash in splashes:
if splash.decrease_duration(): # 如果水花还在持续
if 0 <= splash.y < HEIGHT and 0 <= splash.x < WIDTH: # 确保在屏幕范围内
current_frame_chars[splash.y][splash.x] = '*' # 水花中心
# 添加左右两边的'*'来模拟溅开的效果
if 0 < splash.x < WIDTH - 1:
if current_frame_chars[splash.y][splash.x-1] == ' ': # 只覆盖空格
current_frame_chars[splash.y][splash.x-1] = '*'
if current_frame_chars[splash.y][splash.x+1] == ' ': # 只覆盖空格
current_frame_chars[splash.y][splash.x+1] = '*'
new_splashes.append(splash)
splashes = new_splashes # 更新水花列表
# 5. 渲染当前帧
clear_screen() # 清空屏幕
for line_chars in current_frame_chars:
print("".join(line_chars)) # 将字符列表拼接成字符串并打印
# 6. 等待下一帧
time.sleep(FRAME_DELAY)
# --- Entry Point ---
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
# 允许用户通过Ctrl+C退出程序
print("\n动画结束。")
如何运行:
- 将上述代码保存为一个
.py 文件,例如 cat_rain_animation.py。
- 打开终端或命令提示符。
- 导航到保存文件的目录。
- 运行命令:
python cat_rain_animation.py
代码说明:
BASE_SCENE: 这是一个列表,存储了动画的静态部分(猫、伞和地面)。每一行都是一个字符串,构成了ASCII艺术。
WIDTH, HEIGHT: 定义了动画区域的尺寸。
FRAME_DELAY: 控制每帧之间的时间间隔,值越小动画越快。
MAX_DROPS: 屏幕上同时存在的雨滴数量上限,控制雨势大小。
SPLASH_DURATION: 水花显示多少帧后消失。
UMBRELLA_Y_TOP, UMBRELLA_Y_BOTTOM, UMBRELLA_X_MIN, UMBRELLA_X_MAX: 这些常量定义了雨伞在画面中的大致位置和尺寸,用于检测雨滴是否击中伞面。
GROUND_Y: 定义了地面线的Y坐标,雨滴落到这里就消失。
clear_screen(): 用于在每帧开始时清空终端,实现动画效果。
Raindrop 类: 简单地表示一个雨滴,有X、Y坐标,并有一个move()方法使其下落。
Splash 类: 表示水花,有X、Y坐标和duration(持续时间)。decrease_duration()方法用于倒计时。
main() 函数:
- 在每次循环开始时,它会复制
BASE_SCENE到一个可修改的current_frame_chars列表中(将字符串转换为字符列表的列表)。
- 它遍历现有的
raindrops:
- 如果雨滴落在雨伞的指定区域内,它会被移除,并创建一个新的
Splash对象。
- 否则,雨滴会向下移动一格,并在
current_frame_chars中对应位置绘制|字符。
- 如果雨滴落到
GROUND_Y以下,它就会被过滤掉。
- 根据
MAX_DROPS和随机概率,在顶部生成新的雨滴。
- 它遍历现有的
splashes:
- 减少水花的持续时间。
- 如果水花仍在持续,则在
current_frame_chars中对应位置绘制*字符(并稍微扩散)。
- 最后,清空屏幕,将
current_frame_chars拼接成字符串并打印出来,然后暂停一小段时间。
if __name__ == "__main__":: 确保main()函数只在脚本直接运行时执行,并添加了try-except KeyboardInterrupt以便用户可以按Ctrl+C优雅退出。
现在,您应该能看到一只小猫在雨中,雨滴哗啦啦地落到伞上,溅起朵朵水花的动画了!