Skip to content
Browse files

Added the missing score code to the chapter

  • Loading branch information...
1 parent 8744f56 commit dce23f448037c7ee2d1544a3d0d4b576ab082aa2 @kthakore kthakore committed
Showing with 52 additions and 11 deletions.
  1. +23 −3 dist/SDL_Manual.html
  2. BIN dist/SDL_Manual.pdf
  3. +29 −8 src/06-tetris.pod
View
26 dist/SDL_Manual.html
@@ -2090,6 +2090,9 @@
<pre><code> use SDL;
<b>use SDLx::Surface;</b>
+
+ ...
+
<b>my $back = SDLx::Surface-&gt;load( &#39;data/tetris_back.png&#39; );</b>
<b>my @piece = (undef);</b>
<b>push @piece, SDLx::Surface-&gt;load( &quot;data/tetris_$_.png&quot; ) for 1..7;</b></code></pre>
@@ -2156,12 +2159,12 @@
0,0,0,0],
);
- <b>my $next_tile = shuffle keys %pieces;</b>
+ <b>my $next_tile = get_next_tile();</b>
<b>my $curr_tile = [ undef, 4, 0 ];</b>
<b>@{ $curr_tile-&gt;[0] } = @{ $pieces{$next_tile} };</b>
- <b>$next_tile = shuffle keys %pieces;</b></code></pre>
+ <b>$next_tile = get_next_tile()</b>
-<p>Seems like a function to choose the next tile would be more useful.</p>
+ <b>sub get_next_tile { shuffle keys %pieces };</b></code></pre>
<p>This code randomly chooses a <code>$next_tile</code>, then sets the
piece data for the first piece in <code>$curr_tile</code>.</p>
@@ -2319,6 +2322,23 @@
<h3>Score and Game State</h3>
+<p>First we keep hold a variable to keep the game score and set the
+<code>SDLx::Text</code> font and options to draw the text on the screen
+later on.</p>
+
+<pre><code> use SDLx::App;
+ <b>use SDLx::Text;</b>
+ use SDLx::Rect;
+
+ ...
+
+ # create our game objects
+ <b>my $score_text = SDLx::Text-&gt;new( font =&gt; &#39;font.ttf&#39;,</b>
+ <b>h_align =&gt; &#39;left&#39;,</b>
+ <b>color =&gt; [255,255,255,255] );</b>
+ <b>my $score = 0;</b>
+ my $back = SDLx::Surface-&gt;load( &#39;data/tetris_back.png&#39; );</code></pre>
+
<p>The game state in Tetris is the combination of the fixed placement grid,
the current piece, and the current score. The move handler can update all
of these:</p>
View
BIN dist/SDL_Manual.pdf
Binary file not shown.
View
37 src/06-tetris.pod
@@ -51,6 +51,9 @@ hold them:
use SDL;
B<< use SDLx::Surface; >>
+
+ ...
+
B<< my $back = SDLx::Surface->load( 'data/tetris_back.png' ); >>
B<< my @piece = (undef); >>
B<< push @piece, SDLx::Surface->load( "data/tetris_$_.png" ) for 1..7; >>
@@ -129,18 +132,14 @@ Selecting pieces needs some randomness. The core C<List::Util> module can help:
0,0,0,0],
);
- B<< my $next_tile = shuffle keys %pieces; >>
+ B<< my $next_tile = get_next_tile(); >>
B<< my $curr_tile = [ undef, 4, 0 ]; >>
B<< @{ $curr_tile->[0] } = @{ $pieces{$next_tile} }; >>
- B<< $next_tile = shuffle keys %pieces; >>
+ B<< $next_tile = get_next_tile() >>
-=end programlisting
-
-=for author
-
-Seems like a function to choose the next tile would be more useful.
+ B<< sub get_next_tile { shuffle keys %pieces }; >>
-=end for
+=end programlisting
This code randomly chooses a C<$next_tile>, then sets the piece data for the
first piece in C<$curr_tile>.
@@ -325,6 +324,28 @@ appropriately:
=head2 Score and Game State
+First we keep hold a variable to keep the game score and set the C<SDLx::Text> font and options
+to draw the text on the screen later on.
+
+=begin programlisting
+
+ use SDLx::App;
+ B<< use SDLx::Text; >>
+ use SDLx::Rect;
+
+ ...
+
+ # create our game objects
+ B<< my $score_text = SDLx::Text->new( font => 'font.ttf', >>
+ B<< h_align => 'left', >>
+ B<< color => [255,255,255,255] ); >>
+ B<< my $score = 0; >>
+ my $back = SDLx::Surface->load( 'data/tetris_back.png' );
+
+
+
+=end programlisting
+
The game state in Tetris is the combination of the fixed placement grid, the current piece, and the current score. The move handler can update all of these:
=begin programlisting

0 comments on commit dce23f4

Please sign in to comment.
Something went wrong with that request. Please try again.