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

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

Program with Perl's Term::Animation quits prematurely

问题

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

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

更多内容继续出现

  1. use strict;
  2. use warnings;
  3. use Curses;
  4. use Term::Animation;
  5. my $anim = Term::Animation->new();
  6. # 设置对象的起始位置
  7. my $x = 0;
  8. $anim->new_entity(
  9. shape => ['@'], # 对象的符号
  10. position => [$x, 0, 0],
  11. auto_trans => 1,
  12. );
  13. halfdelay(1);
  14. while (1) {
  15. my $in = lc(getch());
  16. if ($in eq 'q') {
  17. quit(); # 退出
  18. }
  19. elsif ($in eq 'r') {
  20. last; # 重新绘制(将重新创建所有对象)
  21. }
  22. elsif ($in eq 'p') {
  23. $anim->pause(!$anim->paused()); # 暂停或恢复动画
  24. }
  25. elsif ($in eq 'd' || $in eq KEY_RIGHT) {
  26. # 向右移动(增加x位置)
  27. $x++;
  28. $anim->set_entity_position(0, $x, 0, 0);
  29. }
  30. elsif ($in eq 'a' || $in eq KEY_LEFT) {
  31. # 向左移动(减少x位置)
  32. $x--;
  33. $anim->set_entity_position(0, $x, 0, 0);
  34. }
  35. $anim->animate(); # 更新动画
  36. }
  37. $anim->update_term_size();
  38. $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.

  1. if ($in eq 'q') {
  2. endwin(); # Terminate program execution correctly
  3. last;
  4. }

More kept coming out

  1. use strict;
  2. use warnings;
  3. use Curses;
  4. use Term::Animation;
  5. my $anim = Term::Animation->new();
  6. # Set the starting position of the object
  7. my $x = 0;
  8. $anim->new_entity(
  9. shape => ['@'], # Symbol of the object
  10. position => [$x, 0, 0],
  11. auto_trans => 1,
  12. );
  13. halfdelay(1);
  14. while (1) {
  15. my $in = lc(getch());
  16. if ($in eq 'q') {
  17. quit(); # exit
  18. }
  19. elsif ($in eq 'r') {
  20. last; # Redraw (will recreate all objects)
  21. }
  22. elsif ($in eq 'p') {
  23. $anim->pause(!$anim->paused()); # Pause or resume the animation
  24. }
  25. elsif ($in eq 'd' || $in eq KEY_RIGHT) {
  26. # Move right (increment x position)
  27. $x++;
  28. $anim->set_entity_position(0, $x, 0, 0);
  29. }
  30. elsif ($in eq 'a' || $in eq KEY_LEFT) {
  31. # Mover para a esquerda (decrementar a posição x)
  32. $x--;
  33. $anim->set_entity_position(0, $x, 0, 0);
  34. }
  35. $anim->animate(); # Update the animation
  36. }
  37. $anim->update_term_size();
  38. $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

  1. $anim->end();

to set the screen back to normal, followed by

  1. last; # Exit loop (which exits the program)

or

  1. 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

  1. $anim->end();

to set the screen back to normal, followed by

  1. last; # Exit loop (which exits the program)

or

  1. 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:

确定