<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
<div id="NAME_CONTENT">
-<p>Lunar Lander - a small tutorial on Perl SDL</p>
+<p>SDL::Tutorial::LunarLander - a small tutorial on Perl SDL</p>
</div>
<h2 id="CATEGORY">CATEGORY</h2>
<h3 id="CREATING_A_DEMO">CREATING A DEMO</h3>
<div id="CREATING_A_DEMO_CONTENT">
<p>You can see the final version of the demo code by doing:</p>
-<p> </p>
-<pre> perl -MSDL::Tutorial::LunarLander=lander.pl -e1
+<pre>
+
+
+
+
+
+ perl -MSDL::Tutorial::LunarLander=lander.pl -e1
+
+
+
+
+
+
</pre>
-<p> </p>
-<p>this will create all three files used in the tutorial:</p>
+<p>this will create all three files used in the tutorial.</p>
If we start with a simpler simulation, we can worry with the presentation
later.</p>
<p>So, here's the initial code:</p>
-<p> </p>
-<pre> #!/usr/bin/perl
+<pre>
+
+
+
+
+
+ #!/usr/bin/perl
use strict;
use warnings;
print "You landed on the surface safely! :-D\n";
}
+
+
+
+
+
+
</pre>
-<p> </p>
<p>Run the code and you'll see something like this:</p>
-<p> </p>
-<pre> at 0 s height = 1000 m, velocity = 0 m/s
+<pre>
+
+
+
+
+
+ at 0 s height = 1000 m, velocity = 0 m/s
at 1 s height = 1000 m, velocity = 1 m/s
at 2 s height = 999 m, velocity = 2 m/s
at 3 s height = 997 m, velocity = 3 m/s
CRASH!!!
+
+
+
+
+
+
</pre>
-<p> </p>
<p>"What happened? How do I control the ship???"</p>
</div>
an scriptable spaceship will be easier to start. Remember, focus
on the game logic!)</p>
<p>So, create add this simple script to the end of your file:</p>
-<p> </p>
-<pre> __DATA__
+<pre>
+
+
+
+
+
+ __DATA__
at 41s, accelerate 10 m/s^2 up
at 43s, 10 m/s^2
at 45s, 10
at 47s, 10
at 49s, 10
+
+
+
+
+
+
</pre>
-<p> </p>
<p>The script is straightforward: it simply states a time when we
will push the spaceship up with a given acceleration. It accepts
free text: any two numbers you type will work.</p>
<p>We can parse the script using this regular expression:</p>
-<p> </p>
-<pre> my $script_re = qr/(\d+) \D+ (\d+)/x;
+<pre>
+
+
+
+
+
+ my $script_re = qr/(\d+) \D+ (\d+)/x;
+
+
+
+
+
+
</pre>
-<p> </p>
<p>And we can build a hash of ( time => acceleration ) with:</p>
-<p> </p>
-<pre> my %up = map { $_ =~ $script_re } <DATA>;
+<pre>
+
+
+
+
+
+ my %up = map { $_ =~ $script_re } <DATA>;
+
+
+
+
+
+
</pre>
-<p> </p>
<p>So the middle section of the program will become:</p>
-<p> </p>
-<pre> my $script_re = qr/(\d+) \D+ (\d+)/x;
+<pre>
+
+
+
+
+
+ my $script_re = qr/(\d+) \D+ (\d+)/x;
my %up = map { $_ =~ $script_re } <DATA>;
while ( $height > 0 ) {
$t = $t + 1;
}
+
+
+
+
+
+
</pre>
-<p> </p>
<p>That's it!</p>
<p>Try to run the program, and the ship should land safely:</p>
-<p> </p>
-<pre> ./lunar.pl autopilot.txt
+<pre>
+
+
+
+
+
+ ./lunar.pl autopilot.txt
at 0 s height = 1000 m, velocity = 0 m/s
at 1 s height = 1000 m, velocity = 1 m/s
at 2 s height = 999 m, velocity = 2 m/s
You landed on the surface safely! :-D
+
+
+
+
+
+
</pre>
-<p> </p>
<p>Cool, but...</p>
</div>
<h2 id="USING_SDL">USING SDL</h2>
<div id="USING_SDL_CONTENT">
<p>First step: use the required libraries:</p>
-<p> </p>
-<pre> use SDL; #needed to get all constants
+<pre>
+
+
+
+
+
+ use SDL; #needed to get all constants
use SDL::Video;
- use SDL::App;
+ use SDLx::App;
use SDL::Surface;
use SDL::Rect;
use SDL::Image;
+
+
+
+
+
+
</pre>
-<p> </p>
-<p>Second step: initialize <code>SDL::App</code>:</p>
-<p> </p>
-<pre> my $app = SDL::App->new(
- -title => "Lunar Lander",
- -width => 800,
- -height => 600,
- -depth => 32,
+<p>Second step: initialize <code>SDLx::App</code>:</p>
+<pre>
+
+
+
+
+
+ my $app = SDLx::App->new(
+ title => "Lunar Lander",
+ width => 800,
+ height => 600,
+ depth => 32,
);
+
+
+
+
+
+
</pre>
-<p> </p>
<p>Third step: load the images and create the necessary "rectangles":</p>
-<p> </p>
-<pre> my $background = SDL::Image::load('images/background.jpg');
+<pre>
+
+
+
+
+
+ my $background = SDL::Image::load('images/background.jpg');
my $ship = SDL::Image::load('images/ship.jpg');
my $background_rect = SDL::Rect->new(0,0,
$ship->h,
);
+
+
+
+
+
+
</pre>
-<p> </p>
<p>Fourth step: create a sub to draw the spaceship and background:</p>
-<p> </p>
-<pre> sub draw {
+<pre>
+
+
+
+
+
+ sub draw {
my ( $x, $y ) = @_; # spaceship position
# fix $y for screen resolution
SDL::Video::update_rects($app, $background_rect);
}
+
+
+
+
+
+
</pre>
-<p> </p>
<p>Note that this sub first combines all the bitmaps, using a blit
("Block Image Transfer") operation -- which is quite fast, but does
not update the display.</p>
<p>Finally, add the following lines to the end of the main loop, so that
we call the <code>draw()</code> function with the correct spaceship
coordinates:</p>
-<p> </p>
-<pre> while ( $height > 0 ) {
+<pre>
+
+
+
+
+
+ while ( $height > 0 ) {
# ...
$app->delay(10);
}
+
+
+
+
+
+
</pre>
-<p> </p>
<p>That's it!</p>
<p>Run the program and watch the spaceship landing safely on the surface
of the moon.</p>
<h1 id="COPYRIGHT_amp_LICENSE">COPYRIGHT & LICENSE</h1><p><a href="#TOP" class="toplink">Top</a></p>
<div id="COPYRIGHT_amp_LICENSE_CONTENT">
<p>Copyright 2009 Nelson Ferraz, all rights reserved.</p>
+<p>Updated and maintained by the SDL Perl project. See <b>AUTHORS</b> in <cite>SDL</cite>.</p>
<p>This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>