使用Perl's Term::Animation的程序过早退出

jv4diomz  于 2023-05-18  发布在  Perl
关注(0)|答案(1)|浏览(131)

尝试按"d""a"移动对象时,perl退出。
我认为这个问题与使用quit()函数有关,这个函数没有定义。为了解决这个问题,我尝试使用Curses库中的endwin()函数正确结束程序。

if ($in eq 'q') {
    endwin(); # Terminate program execution correctly
    last;
}

更多的人不断地冒出来

use strict;
use warnings;
use Curses;
use Term::Animation;

my $anim = Term::Animation->new();
# Set the starting position of the object
my $x = 0;

$anim->new_entity(
    shape           => ['@'],  # Symbol of the object
    position        => [$x, 0, 0],
    auto_trans      => 1,
);

halfdelay(1);

while (1) {
    my $in = lc(getch());

    if ($in eq 'q') {
        quit();  # exit
    }
    elsif ($in eq 'r') {
        last;  # Redraw (will recreate all objects)
    }
    elsif ($in eq 'p') {
        $anim->pause(!$anim->paused());  # Pause or resume the animation
    }
    elsif ($in eq 'd' || $in eq KEY_RIGHT) {
       # Move right (increment x position)
        $x++;
        $anim->set_entity_position(0, $x, 0, 0);
    }
    elsif ($in eq 'a' || $in eq KEY_LEFT) {
        # Mover para a esquerda (decrementar a posição x)
        $x--;
        $anim->set_entity_position(0, $x, 0, 0);
    }

    $anim->animate();  # Update the animation
}

$anim->update_term_size();
$anim->remove_all_entities();
ruarlubt

ruarlubt1#

它被隐藏了,但是程序因为错误而死亡
无法通过包“Term::Animation”找到对象方法“set_entity_position”
实际上,我在docs中没有看到这样的方法。
Term::Animation::Entity有一个position方法可以用来改变它们的位置。当然,您需要先获得要移动的实体。->new_entity返回一个实体的引用。
关于quit(),您应该使用

$anim->end();

将屏幕设置为正常,然后

last;        # Exit loop (which exits the program)

exit;         # Exit the program directly.

这表明r键没有执行您期望的操作。

相关问题