我正在编写一个 perl 脚本,使用 Curses 库在终端中显示动画,但当终端大小改变时,我无法让它重新动画。目标是当终端大小调整时,动画从终端中间重新启动。
这是我尝试过的代码,但即使终端尺寸发生变化,它也永远不会重新绘制实体:
#!/usr/bin/env perl
use Term::Animation;
use Term::Animation::Entity;
use Curses;
# Declaration of $anim1 and addition of entity
my ($width, $height, $assumed_size) = $anim1->screen_size();
while (1) {
# Animate the current frame
$anim1->animate();
# Check for terminal resize and update if necessary
my ($new_width, $new_height) = $anim1->screen_size();
if ($new_width != $width || $new_height != $height) {
# Update terminal size variables
$width = $new_width;
$height = $new_height;
# Update entity position based on new terminal size
my $entity->{position} = [$height / 2 - 7, $width / 2 - 25, 0];
# Redraw all entities
$anim1->update_term_size();
}
# Handle user input
my $input = getch();
if ($input && $input eq 'c') {
last; # Exit the loop if 'c' is pressed
}
}