按“d”移动对象,但执行结束

问题描述 投票:0回答:1

当试图通过按

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

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();
perl curses
1个回答
0
投票

它被隐藏了,但程序因错误而死亡

无法通过包“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
键没有执行您期望的操作。

© www.soinside.com 2019 - 2024. All rights reserved.