Обнаружение столкновений с термином анимации Perl не работаетLinux

Ответить
Anonymous
 Обнаружение столкновений с термином анимации Perl не работает

Сообщение Anonymous »

Я пытаюсь создать анимацию терминала Perl, где сущность меняет направление, когда она сталкивается с другим объектом. Вот код, который у меня есть: < /p>
use Term::Animation 2.0;
use Term::Animation::Entity;
use Time::HiRes qw(time);
use Data::Dumper;
use Curses;
use strict;
use warnings;

main();

sub add_animal {
my ($anim, $y) = @_;

# Add the animal to the animation
my $animal = $anim->new_entity(
type => 'animal',
position => [3, $y, 1],
shape => '*',
color => 'r',
callback_args => [1, 0, 0, 0],
die_offscreen => 0,
coll_handler => \&animal_collision,
);
}

sub animal_collision {
my ($animal, $anim) = @_;
my $collisions = $animal->collisions();
#return unless defined $collisions;

foreach my $col_obj (@{$collisions}) {
# Get the current x direction from the callback_args
my $x_dir = $animal->callback_args->[0];

# Reverse the direction
$animal->callback_args->[0] = -$x_dir;
}
}

sub add_background {
my ($anim, $screen_width, $screen_height) = @_;
my $half_width = int($screen_width / 2);
my $ground_level = int($screen_height * 0.7);

for my $y (0..($screen_height)) {
$anim->new_entity(
shape => ['|'],
position => [$half_width, $y, 21],
color => ['w'],
);
}
}

sub main {

my $anim = Term::Animation->new();
$anim->color(1);

halfdelay(1);
my $paused = 0;

while(1) {
my $screen_width = $anim->width();
my $screen_height = $anim->height();
my $half_width = int($screen_width / 2);
my $ground_level = int($screen_height * 0.7);

add_background($anim, $screen_width, $screen_height);
add_animal($anim, ($ground_level - 1));

# animation loop
while(1) {
my $current_time = time();

# run and display a single animation frame
$anim->animate() unless($paused);

# use getch to control the frame rate, and get input at the same time.
my $input = getch();
if($input eq 'q') { quit(); }
if($input eq 'r' || $input eq KEY_RESIZE()) { last; }
if($input eq 'p') { $paused = !$paused; }
}
$anim->update_term_size();
$anim->remove_all_entities();
}

$anim->end();
}
< /code>
На данный момент объект животного проходит через другой объект без какого -либо изменения направления. Пожалуйста, дайте мне знать, что не так с детектором столкновений или любыми другими проблемами.

Подробнее здесь: https://stackoverflow.com/questions/794 ... ot-working
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Linux»