物体之间的碰撞和拟声词(splat)的添加

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

collision between objects and addition of onomatopoeias(splat)

问题

这是您提供的代码的翻译部分:

我这里有3个物体'sword'),'target'目标'splat'飞溅物)。
我每次试图让剑与目标物体碰撞时group_death会销毁实际碰撞的关联实体nib”。
target_collision会添加飞溅物但都没有奏效我参考了这个模板 [git asciiquarium][1]但目标物体和武器鲨鱼的移动非常复杂
我试图构建的示例中只有枪移动而目标是固定的
我已尝试寻找其他Perl碰撞字体但没有剩下的了
有谁知道并愿意提供一些帮助吗
[第二次尝试比下面的多28行][2]< - 我使用的脚本我保留了下面的脚本因为它更简单最耗时的部分只是图像对象)。

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

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

my @sword = (
q{
        /| _________
   O|==|* >________>
        \|
},
q{
     /| _________
O|==|* >________>
     \|
},
q{
        /| _________
   O|==|* >________>
        \|
},
q{
          /| _________
     O|==|* >________>
          \|
} );
my @launch;
push(@launch, [0,0,0,0]) for(1..10);
push(@launch, [0,0,0,1]) for(1..2);
push(@launch, [0,0,0,1]) for(1..2);
push(@launch, [0,0,0,3]) for(1..1);
$s->new_entity(
shape           => \@sword,
    position        => [0, 4, 3],
    callback_args   => [0, [@launch]],
    defeaut_color   => 'blue',
    auto_trans      => 1,
    die_offscreen   => 1,
    death_cb        => sub { group_death(@_, 'nib') },
);
my $target = q#
     ___
  ,-^   ^-.
 / _    _  \
| / \  / \ |
| \_/  \_/ |
 \   /|\  /
  |  \_/ |
   \     /
    ^---^
#;
$s->new_entity(
    type            => 'nib',
    shape           => $target,
    position        => [ 21, 1, 2],
    callback_args   => [0,0,0,0],
    die_offscreen   => 1,
    death_cb        => \&add_target,
    wrap            => 1,
    default_color   => 'Y',
    auto_trans      => 1,
    physical        => 1,
    coll_handler    => \&target_collision,
);
sub add_splat {
    my ($anim, $x, $y, $z) = @_;
    my @splat_image = (
q{
 


      \ | / 
      *@@@* 
    --@@@@@--
      *@@@*
      / | \
 
 
 
},
q{
        |
    \   |   /
     \  &  /
      &***& 
     d*@@@*b 
----&*@@@@@*---
     q*@@@*p
      &***&
     /  &  \
    /   |   \
        |
},
q{
 


   *        *
      #   &
    *   @    %
@     @  @
  *  *p%  #
     @   &
       &   *
   d
});

    $s->new_entity(
        shape           => \@splat_image,
        position        => [ 19, 0, 1 ],
        default_color   => 'r',
        callback_args   => [ 0, 0, 0, .25 ],
        transparent     => ' ',
        die_frame       => 15,
    );
}
sub group_death {
        my ($entity, $s, @bound_types) = @_;
        foreach my $type (@bound_types) {
                my $bound_entities = $s->get_entities_of_type($type);
                foreach my $obj (@{$bound_entities}) {
                        $s->del_entity($obj);
                }
        }
}
sub target_collision {
        my ($target, $s) = @_;
        my $collisions = $target->collisions();
        foreach my $col_obj (@{$collisions}) {
                if($col_obj->type eq 'nib') {
                        add_splat($s, $col_obj->position());
                        $target->kill();
                        last;
                }
        }
}
halfdelay( 2 );
for(1..500) {
    $s->animate();
    my $in = lc( getch() );
    if($in eq 'q') { last; }
}

[1]: https://github.com/cmatsuoka/asciiquarium/blob/master/asciiquarium
[2]: https://pastebin.com/5cwUbPcT

请注意,其中包含了代码的翻译,但不包括评论或其他解释性内容。如果您需要进一步的帮助,请随时提出。

英文:

I have 3 objects here: 'sword', 'target' and 'splat'.
I was trying every time the sword collided with the target object, the group_death
destroy the "nib" for, the associated entity that does the actual collision
target_collision adds the splat
but none of that worked. Was following this template git asciiquarium. But very complex with the target object and the weapon (shark) moving.
The example I'm trying to build only the gun moves while the target is fixed.
I've tried to find other perl collision fonts, but there's nothing left.
Does anyone know and can please collaborate with some?
2nd attempt with 28 more lines than the one below. (<- script I use. I left the one below because it has less. What takes up most is just the image object).

use strict;
use warnings;
use Curses;
use Term::Animation 2.0;
my $s = Term::Animation-&gt;new();
$s-&gt;color(1); 
my @sword = (
q{
/| _________
O|==|* &gt;_________&gt;
\|
},
q{
/| _________
O|==|* &gt;_________&gt;
\|
},
q{
/| _________
O|==|* &gt;_________&gt;
\|
},
q{
/| _________
O|==|* &gt;_________&gt;
\|
} );
my @launch;
push(@launch, [0,0,0,0]) for(1..10);
push(@launch, [0,0,0,1]) for(1..2);
push(@launch, [0,0,0,1]) for(1..2);
push(@launch, [0,0,0,3]) for(1..1);
$s-&gt;new_entity(
shape           =&gt; \@sword,
position        =&gt; [0, 4, 3],
callback_args   =&gt; [0, [@launch]],
defeaut_color   =&gt; &#39;blue&#39;,
auto_trans      =&gt; 1,
die_offscreen   =&gt; 1,
death_cb        =&gt; sub { group_death(@_, &#39;nib&#39;) },
);
my $target = q#
___
,-^   ^-.
/ _    _  \
| / \  / \ |
| \_/  \_/ |
\   /|\  /
|  \_/ |
\     /
^---^
#;
$s-&gt;new_entity(
type            =&gt; &#39;nib&#39;,
shape           =&gt; $target,
position        =&gt; [ 21, 1, 2],
callback_args   =&gt; [0,0,0,0],
die_offscreen   =&gt; 1,
death_cb        =&gt; \&amp;add_target,
wrap            =&gt; 1,
default_color   =&gt; &#39;Y&#39;,
auto_trans      =&gt; 1,
physical        =&gt; 1,
coll_handler    =&gt; \&amp;target_collision,
);
sub add_splat {
my ($anim, $x, $y, $z) = @_;
my @splat_image = (
q{
\ | / 
*@@@* 
--@@@@@--
*@@@*
/ | \
},
q{
|
\   |   /
\  &amp;  /
&amp;***&amp; 
d*@@@*b 
----&amp;*@@@@@*&amp;---
q*@@@*p
&amp;***&amp;
/  &amp;  \
/   |   \
|
},
q{
*        *
#   &amp;
*   @    %
@     @  @
*  *p%  #
@   &amp;
&amp;   *
d
});
$s-&gt;new_entity(
shape           =&gt; \@splat_image,
position        =&gt; [ 19, 0, 1 ],
default_color   =&gt; &#39;r&#39;,
callback_args   =&gt; [ 0, 0, 0, .25 ],
transparent     =&gt; &#39; &#39;,
die_frame       =&gt; 15,
);
}
sub group_death {
my ($entity, $s, @bound_types) = @_;
foreach my $type (@bound_types) {
my $bound_entities = $s-&gt;get_entities_of_type($type);
foreach my $obj (@{$bound_entities}) {
$s-&gt;del_entity($obj);
}
}
}
sub target_collision {
my ($target, $s) = @_;
my $collisions = $target-&gt;collisions();
foreach my $col_obj (@{$collisions}) {
if($col_obj-&gt;type eq &#39;nib&#39;) {
add_splat($s, $col_obj-&gt;position());
$target-&gt;kill();
last;
} 
}
}
halfdelay( 2 );
for(1..500) {
$s-&gt;animate();
my $in = lc( getch() );
if($in eq &#39;q&#39;) { last; }
}

答案1

得分: 1

剑必须是实体的,而且必须具有与钢笔相同的 Z 坐标才能发生碰撞,另外,剑的形状有多个帧,但它不会移动。然后,剑的宽度被计算为其帧的最大宽度。以下是一个示例,其中剑的形状只有一个帧,但该形状在 X 方向上移动:

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

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

my @sword = (
q{
     /| _________
O|==|* >_________>
     \|
}
);
my @launch;
push(@launch, [0,0,0,0]) for(1..10);
push(@launch, [1,0,0,0]) for(1..2);
push(@launch, [2,0,0,0]) for(1..2);
push(@launch, [3,0,0,0]) for(1..1);

$s->new_entity(
    type            => "sword",
    shape           => \@sword,
    position        => [0, 4, 3],
    callback_args   => [0, [@launch]],
    default_color   => 'blue',
    auto_trans      => 1,
    die_offscreen   => 1,
    physical        => 1,
    coll_handler    => \&sword_collision,
);

my $target = q#
     ___
  ,-^   ^-.
 / _    _  \
| / \  / \ |
| \_/  \_/ |
 \   /|\  /
  |  \_/ |
   \     /
    ^---^
#;
$s->new_entity(
    type            => 'nib',
    shape           => $target,
    position        => [ 21, 1, 3],
    callback_args   => [0,0,0,0],
    die_offscreen   => 1,
    wrap            => 1,
    default_color   => 'Y',
    auto_trans      => 1,
    physical        => 1,
);

sub add_splat {
    my ($anim, $x, $y, $z) = @_;
    my @splat_image = (
q{



      \ | / 
      *@@@* 
    --@@@@@--
      *@@@*
      / | \





},
q{
        |
    \   |   /
     \  &  /
      &***& 
     d*@@@*b 
----&*@@@@@*---
     q*@@@*p
      &***&
     /  &  \
    /   |   \
        |
},
q{



   *        *
      #   &
    *   @    %
@     @  @
  *  *p%  #
     @   &
       &   *
   d
});

    $s->new_entity(
        shape           => \@splat_image,
        position        => [ 19, 0, 1 ],
        default_color   => 'r',
        callback_args   => [ 0, 0, 0, .25 ],
        transparent     => ' ',
        die_frame       => 15,
    );
} # end of add_splat sub

sub sword_collision {
    my ($entity, $anim) = @_;
    my $collisions = $entity->collisions();
    if ($collisions) {
        my $type = $entity->type;
        foreach my $col_obj (@{$collisions}) {
            if($col_obj->type eq 'nib') {
                add_splat($s, $col_obj->position());
                $col_obj->kill();
                last;
            }
        }
    }
}

halfdelay( 2 );
for(1..500) {
    $s->animate();
    my $in = lc( getch() );
    if($in eq 'q') { last; }
}

请注意,我已将代码部分保持原样,只翻译了注释和字符串。

英文:

The sword must be physical, and have the same z-coordinate as the nib for a collision to occur, also the shape of the sword has multiple frames but it does not move. Then the width of the sword is calculated as the maximum width of its frames. Here is an example where the sword shape has only one frame, but that shape does move in the x-direction:

use strict;
use warnings;
use Curses;
use Term::Animation 2.0;
my $s = Term::Animation-&gt;new();
$s-&gt;color(1); 
my @sword = (
q{
/| _________
O|==|* &gt;_________&gt;
\|
}
);
my @launch;
push(@launch, [0,0,0,0]) for(1..10);
push(@launch, [1,0,0,0]) for(1..2);
push(@launch, [2,0,0,0]) for(1..2);
push(@launch, [3,0,0,0]) for(1..1);
$s-&gt;new_entity(
type            =&gt; &quot;sword&quot;,
shape           =&gt; \@sword,
position        =&gt; [0, 4, 3],
callback_args   =&gt; [0, [@launch]],
defeaut_color   =&gt; &#39;blue&#39;,
auto_trans      =&gt; 1,
die_offscreen   =&gt; 1,
physical        =&gt; 1,
coll_handler    =&gt; \&amp;sword_collision,
);
my $target = q#
___
,-^   ^-.
/ _    _  \
| / \  / \ |
| \_/  \_/ |
\   /|\  /
|  \_/ |
\     /
^---^
#;
$s-&gt;new_entity(
type            =&gt; &#39;nib&#39;,
shape           =&gt; $target,
position        =&gt; [ 21, 1, 3],
callback_args   =&gt; [0,0,0,0],
die_offscreen   =&gt; 1,
wrap            =&gt; 1,
default_color   =&gt; &#39;Y&#39;,
auto_trans      =&gt; 1,
physical        =&gt; 1,
);
sub add_splat {
my ($anim, $x, $y, $z) = @_;
my @splat_image = (
q{
\ | / 
*@@@* 
--@@@@@--
*@@@*
/ | \
},
q{
|
\   |   /
\  &amp;  /
&amp;***&amp; 
d*@@@*b 
----&amp;*@@@@@*&amp;---
q*@@@*p
&amp;***&amp;
/  &amp;  \
/   |   \
|
},
q{
*        *
#   &amp;
*   @    %
@     @  @
*  *p%  #
@   &amp;
&amp;   *
d
});
$s-&gt;new_entity(
shape           =&gt; \@splat_image,
position        =&gt; [ 19, 0, 1 ],
default_color   =&gt; &#39;r&#39;,
callback_args   =&gt; [ 0, 0, 0, .25 ],
transparent     =&gt; &#39; &#39;,
die_frame       =&gt; 15,
);
} # end of add_splat sub
sub sword_collision {
my ($entity, $anim) = @_;
my $collisions = $entity-&gt;collisions();
if ($collisions) {
my $type = $entity-&gt;type;
foreach my $col_obj (@{$collisions}) {
if($col_obj-&gt;type eq &#39;nib&#39;) {
add_splat($s, $col_obj-&gt;position());
$col_obj-&gt;kill();
last;
}
}
}
}
halfdelay( 2 );
for(1..500) {
$s-&gt;animate();
my $in = lc( getch() );
if($in eq &#39;q&#39;) { last; }
}

huangapple
  • 本文由 发表于 2023年3月8日 14:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75669950.html
匿名

发表评论

匿名网友

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

确定