使用Perl的Term::Animation编写的程序提前退出。

huangapple go评论60阅读模式
英文:

Program with Perl's Term::Animation quits prematurely

问题

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

if ($in eq 'q') {
    endwin(); # 正确终止程序执行
    last;
}

更多内容继续出现

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

my $anim = Term::Animation->new();
# 设置对象的起始位置
my $x = 0;

$anim->new_entity(
    shape           => ['@'],  # 对象的符号
    position        => [$x, 0, 0],
    auto_trans      => 1,
);

halfdelay(1);

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

    if ($in eq 'q') {
        quit();  # 退出
    }
    elsif ($in eq 'r') {
        last;  # 重新绘制(将重新创建所有对象)
    }
    elsif ($in eq 'p') {
        $anim->pause(!$anim->paused());  # 暂停或恢复动画
    }
    elsif ($in eq 'd' || $in eq KEY_RIGHT) {
        # 向右移动(增加x位置)
        $x++;
        $anim->set_entity_position(0, $x, 0, 0);
    }
    elsif ($in eq 'a' || $in eq KEY_LEFT) {
        # 向左移动(减少x位置)
        $x--;
        $anim->set_entity_position(0, $x, 0, 0);
    }

    $anim->animate();  # 更新动画
}

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

请注意,代码中的注释已被翻译。

英文:

When trying to move the object by pressing "d" or "a" perl quits.
I thought the problem was related to using the quit() function which was not defined. To fix this, I tried using the endwin() function from the Curses library to end the program correctly.

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

More kept coming out

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();

答案1

得分: 1

"It's being hidden but the program is dying with the error:

> Can't locate object method "set_entity_position" via package "Term::Animation"

And indeed, I see no such method in the docs.

Term::Animation::Entity have a position method which can be used to change their position. Of course, you will need to obtain the entity you want to move first. ->new_entity returns a reference to the entity it creates.


Regarding quit(), you should be using

$anim->end();

to set the screen back to normal, followed by

last;        # Exit loop (which exits the program)

or

exit;         # Exit the program directly.

This suggests the r key doesn't do what you expect it to do."

英文:

It's being hidden but the program is dying with the error

> Can't locate object method "set_entity_position" via package "Term::Animation"

And indeed, I see no such method in the docs.

Term::Animation::Entity have a position method which can be used to change their position. Of course, you will need to obtain the entity you want to move first. ->new_entity returns a reference to the entity is creates.


Regarding quit(), you should be using

$anim->end();

to set the screen back to normal, followed by

last;        # Exit loop (which exits the program)

or

exit;         # Exit the program directly.

This suggests the r key doesn't do what you expect it to do.

huangapple
  • 本文由 发表于 2023年5月14日 05:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定