Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

2d Sprite Engines for Perl

by friedo (Prior)
on Apr 09, 2007 at 19:48 UTC ( [id://609024]=perlquestion: print w/replies, xml ) Need Help??

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

I've decided to write a video game. Why? Well, I've never written a video game before, and it seems like it would be fun. Sure, hacking mod_perl and munging data is fun, too, but sometimes you just want to blow up some space monsters.

My favorite video games are the old-style 2D sprite-based scrollers, so I'm investigating what kind of open-source, preferably cross-platform sprite engines are available. I've been looking at SDL, which seems to have a robust Perl binding, but I wonder what else is available.

So, what are your general experiences with this type of graphics programming in Perl, and do you have any pointers to useful things to read, or examples to look at?

Replies are listed 'Best First'.
Re: 2d Sprite Engines for Perl
by Trizor (Pilgrim) on Apr 09, 2007 at 21:33 UTC

    I personally would reccomend SDL. Even in C SDL is a great library to use, and while I haven't used it in Perl, looking through the module on CPAN it seems to be able to do everything I'd want to do in a typical SDL app.

    As for useful tutorials and articles, I would reccomend learning SDL from the C perspective first from the SDL Tutorials then bringing that knowlege to Perl. At the very least give them a look over, as they have lots of useful tips and tricks scattered throughout.

Re: 2d Sprite Engines for Perl
by LTjake (Prior) on Apr 09, 2007 at 21:10 UTC

    I've always liked allegro for making games -- but the perl bindings seem kind of stale.

    --
    "Go up to the next female stranger you see and tell her that her "body is a wonderland."
    My hypothesis is that she’ll be too busy laughing at you to even bother slapping you.
    " (src)

Re: 2d Sprite Engines for Perl
by duelafn (Parson) on Apr 10, 2007 at 03:16 UTC
Re: 2d Sprite Engines for Perl
by derby (Abbot) on Apr 09, 2007 at 20:06 UTC

    ooOO can you please expand your knowledge by adding new levels to supertux! I'd appreciate it (but I'm sure my wife wouldn't).

    -derby
Re: 2d Sprite Engines for Perl
by chanio (Priest) on Apr 10, 2007 at 00:24 UTC
Re: 2d Sprite Engines for Perl
by zentara (Archbishop) on Apr 10, 2007 at 10:22 UTC
    Well I just have to throw in a plug for Tk::Zinc. :-) The nice thing about Zinc, is you can get persistent objects to play with, instead of SDL's sprite. I can't find the original node, for this cool Zinc game, but it was written by a fellow perlmonk thundergnat a few months back.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Zinc; use Time::HiRes qw( gettimeofday tv_interval ); my $mw = MainWindow->new; my ( $window_width, $window_height ) = ( 800, 600 ); $mw->geometry("${window_width}x$window_height"); $mw->resizable(0,0); $mw->update; my $zframe = $mw->Frame->pack( -expand => 1, -fill => 'both' ); my $zinc = $zframe->Zinc( -backcolor => 'black', -render => 1 )->pack( -fill => 'both', -expand => 1, ); my ( %ball, %wall, %time ); my $delay = 40; my $gravity = 1.5; my $repel_start = $gravity * .1; my $repel = $repel_start; $ball{velocity} = [ 4, -20 ]; my $top = 30; my $bottom = $window_height - $top; my $left = $top; my $right = $window_width - $left; my $group = $zinc->add( 'group', 1, -visible => 1 ); { $ball{radius} = 20; my $x = $window_width / 2; my $y = $window_height / 2; $ball{position} = [ $x, $y ]; $ball{widget} = $zinc->add( 'arc', $group, [ [ $x - $ball{radius}, $y - $ball{radius} ], [ $x + $ball{radius}, $y + $ball{radius} ] ], -filled => 1, -fillcolor => # '=radial -20 -20|#ffffff;50 0|#f700f7;50 48|#900090;50 80|# +ab00ab;50 100', '=radial -20 -20|#ffffff 0|#f700f7 48|#900090 80|#ab00ab 100 +', -linewidth => 0, -visible => 1, ); } $wall{widget} = $zinc->add( 'curve', $group, [ [ $left, $top ], [ $right, $top ], [ $right, $bottom ], [ $left, $bottom ], [ $left, $top ] ], -linecolor => '#00ff00', -linewidth => 6, -priority => 100, -visible => 1, ); $time{current}{widget} = $zinc->add( 'text', $group, -position => [ $window_width / 8, 0 ], -color => '#c0c000', -font => "Times 14", -visible => 1, ); $time{power}{widget} = $zinc->add( 'text', $group, -position => [ $window_width / 8 * 3, 0 ], -color => '#c0c000', -font => "Times 14", -visible => 1, ); $time{high}{widget} = $zinc->add( 'text', $group, -position => [ $window_width / 8 * 5, 0 ], -color => '#c0c000', -font => "Times 14", -visible => 1, ); $zframe->bind( '<Enter>' => sub { $zframe->configure( -cursor => 'dot' + ) } ); $zframe->bind( '<Leave>' => sub { $zframe->configure( -cursor => 'arro +w' ) } ); $time{current}{value} = gettimeofday; $time{high}{value} = 0; $mw->repeat( $delay, \&update ); MainLoop; sub update { my ( $x, $y ) = @{ $ball{position} }; my ( $dx, $dy ) = @{ $ball{velocity} }; my ( $mx, $my ) = ( $mw->pointerx - $mw->x, $mw->pointery - $mw->y ); # mouse p +osition my $ximpulse = 0; my $yimpulse = 0; $repel -= .00005; #power de +cay my $elasped = tv_interval( [ $time{current}{value} ], [gettimeofda +y] ); my $percent = sprintf "%.1f", $repel / $repel_start * 100; $zinc->itemconfigure( $time{current}{widget}, -text => ( sprintf "Current %.2f Secs.", $elasped ) ); $zinc->itemconfigure( $time{power}{widget}, -text => "$percent% Po +wer" ); if ( $time{high}{value} < $elasped ) { $time{high}{value} = $elasped; $zinc->itemconfigure( $time{high}{widget}, -text => ( sprintf "High %0.2f : $percent%%", $elasped ) +); } if ( $my > $top - $ball{radius} and $my < $bottom + $ball{radius} and $mx > $left - $ball{radius} and $mx < $right + $ball{radius} ) { my $y_component = $y - $my; my $x_component = $x - $mx; my $hypotenuse = ( $y_component**2 + $x_component**2 )**.5; $yimpulse = $y_component * 800 / $hypotenuse**2 * $repel; $ximpulse = $x_component * 800 / $hypotenuse**2 * $repel; } $dx *= .99; # a little velocity decay. $dy *= .99; if ( ( $x - $ball{radius} + $dx < $left ) or ( $x + $ball{radius} + $dx > $right ) ) { $dx = -$dx; reset_time( $elasped, $percent ); } if ( ( $y - $ball{radius} + $dy < $top ) or ( $y + $ball{radius} + $dy > $bottom ) ) { $dy = -$dy * .75; reset_time( $elasped, $percent ); } $zinc->translate( $ball{widget}, $dx, $dy ); $dy += ( ( $gravity**2 ) / 2 ) + $yimpulse; $dx += $ximpulse; my ( $x0, $y0, $x1, $y1 ) = $zinc->bbox( $ball{widget} ); $ball{position} = [ ( $x0 + $x1 ) / 2, ( $y0 + $y1 ) / 2 ]; $ball{velocity} = [ $dx, $dy ]; } sub reset_time { my ( $elasped, $percent ) = @_; printf "%.2f Seconds : %.1f%% Power\n", $elasped, $percent if $elasped > 10; $time{current}{value} = gettimeofday; $repel = $repel_start; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      is this the original node? a

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://609024]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found