iraid3r has asked for the wisdom of the Perl Monks concerning the following question:

I'm a newbie to Perl and I'm trying to make a simple game using the SDL library. My issue is that I'm unable to figure out how to dynamically create multiple objects that have the same sprite. This is similar to how I created my sprites
my $enemy_sprite = SDLx::Sprite->new ( width => $20, height => $20 ); $enemy_sprite->load('example.png');
And this is how I was attempting to instantiate it
my $enem = { sprite => $enemy_sprite, v_y => 5, v_x => 5, };
This seems to work well for creating one object. However, I need to find a decent approach to make more(upto 10). Perl Monks, can you help me?

Replies are listed 'Best First'.
Re: Perl SDL instantiation with Sprites
by Discipulus (Canon) on Apr 24, 2015 at 12:58 UTC
    Welcome to the monastery iraid3r

    Anonym pointend you about the fact that $enem is not an object, but maybe this is good for you: if i understand your question correctly you create an object-sprite and want to reuse for many anonymous hashes like $enem (so $enem and company will be not objects).

    In this case you have a lot of options: if you isolate,for example, coordinates for the creation of the army into an ArrayofArrays where each element is a pair of x and y. like  @coord_pairs = ( [1,2], [3,7], [5,9]) then you can cycle through them as in:
    my @coord_pairs = ( [1,2], [3,7], [5,9]); my @enemy_container; foreach my $index (coord_pairs) { my $enem = { sprite => $enemy_sprite , v_y => $coord_pairs[$index]->[0], # element 0 of the current p +air v_x => $coord_pairs[$index]->[1], # element 1 of the current p +air }; push @enemy_container, $enem; }
    As said there a lot of options to deal with lists and arrays: foreach while map shift pop ...

    i know nothing about SDL, and dunno if you can reuse the created sprite seveveral times: maybe you need to clone it, or in the worst case you can create a new sprite for each enemy created, even if the png is the same:
    my @coord_pairs = ( [1,2], [3,7], [5,9]); my @enemy_container; foreach my $index (coord_pairs) { my $temp_enemy_sprite = SDLx::Sprite->new ( width => $20, height => + $20 ); $enemy_sprite->load('example.png'); my $enem = { sprite => $temp_enemy_sprite , v_y => $coord_pairs[$index]->[0], # element 0 of the current p +air v_x => $coord_pairs[$index]->[1], # element 1 of the current p +air }; push @enemy_container, $enem; }
    As little side note, avoid name like $20 for your variables: if you are starting coding choice meaningful name for them as i tried to do in my little example. There is chance to other parts of the program change the value (accidentally) of $20 ending with $20 containing 30. Perl let you to define true constant as described in the docs. In this case you can use constant TWENTY => 20; because number are not allowed as constant name (but you can use _20).

    Post clear question to obtain the best from perlmonks. i'm courious to see your gaming attempt anyway.

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Perl SDL instantiation with Sprites
by Anonymous Monk on Apr 24, 2015 at 06:51 UTC

    $enem is not an object

    Here are ten sprites

    my @sprites = map { my $es = SDLx::Sprite->new; $es->load('example.png'); $es; } 1 .. 10;