# PerlGameDev/SDL_Manual

Finished up the tetris explaination

1 parent 3fc42e5 commit 50ab00ed1960344e68ba4b7376eb0f7e28b1e6a6 kthakore committed Jan 18, 2011
Showing with 98 additions and 30 deletions.
1. +64 −30 dist/SDL_Manual.html
2. dist/SDL_Manual.pdf
3. games/tetris.zip
4. +34 −0 src/06-tetris.pod
 @@ -2058,6 +2058,15 @@

Tetris

+
+ +

Get Tetris Game

+ +

Get the tetris code from +https://github.com/PerlGameDev/SDL_Manual/raw/master/games/tetris.zip.

+ +
+

The Game Window

First we will make our window with a fixed size so we can place our art @@ -2189,9 +2198,12 @@

Moving Pieces

is stored. Once a piece has collided with sometime we move it to $store grid and hold it there until a line is cleared. -$next_tile         = shuffle(keys %pieces);
+
$next_tile = shuffle(keys %pieces); - + sub rotate_piece { + To rotate a piece we apply a transformation on each element of the +piece. + + + sub rotate_piece { + my$_piece   = shift;
+     my $_rotated = []; + my$_i = 0;
@@ -2200,9 +2212,12 @@

Moving Pieces

+ $_i++; + } + return$_rotated; - + } + + }
- + sub can_move_piece { +

Additionally we do a simple collision checking between the non zero +elements in the pieces with the direction the user wants to move.

+ +
+ sub can_move_piece {
+     my $direction = shift; + my$amount    = shift || 1;
+     for my $y (0..3) { @@ -2231,9 +2246,13 @@ Moving Pieces + } + } + return 1; - + } + + } - + sub move_piece { + Finally we move the move piece by using the collision check and +overlaying the piece array into the @grid for each next +position. + + + sub move_piece { + my$direction = shift;
+     my $amount = shift || 1; + if($direction eq 'right') {
@@ -2266,9 +2285,12 @@

Moving Pieces

+ } + } + } - + } + + }
- + sub trigger_move_event_handler { +

Finally we hook it into the event handler where we use the events to +move the pieces in the right direction.

+ +
+ sub trigger_move_event_handler {
+     my ( $event,$app ) = @_;
+     if( $event->type == SDL_KEYDOWN ) { + my$key = $event->key_sym; @@ -2293,34 +2315,43 @@ Moving Pieces Score and Game State + Next we add the move handler to update the game state. In tetris the +game state can be summarized as the grid, current piece and the score. In +this move handler we update all these things . + +$app->add_move_handler( sub {
-    +     my ( $step,$app ) = @_;
-    +     if(can_move_piece('down', $step / 2)) { + + my ($step, $app ) = @_; + + We update the current piece's state as movable or fixed. + + + if(can_move_piece('down',$step / 2)) {
+         move_piece('down', $step / 2); + } + else { + store_piece($curr_tile); # placing the tile
-    +
-    +         # checking for lines to delete
-    +         my $y; - + my @to_delete = (); - + for($y = 22; $y >= 0;$y--) {
-    +             # there is no space if min of this row is true (greater than zero)
-    +             if(min(@{$store}[($y*10)..((($y+1)*10)-1)])) { - + push(@to_delete,$y);
-    +             }
-    +         }
-    +         # deleting lines
+    +
+ +

We update the status of the grid and see if there are lines to remove. + +# checking for lines to delete + my $y; + my @to_delete = (); + for($y = +22; $y >= 0;$y--) { + # there is no space if min of this row is true +(greater than zero) + if(min(@{$store}[($y*10)..((($y+1)*10)-1)])) { + +push(@to_delete,$y); + } + }

+ +

When we delete lines increment the score of the user.

+ +
+         # deleting lines
+         foreach(@to_delete) {
+             splice(@{$store},$_*10, 10);
+             $score++; + } + +Next for each deleted line we clear the grid. + # adding blank rows to the top + foreach(@to_delete) { + splice(@{$store}, 0, 0, (0,0,0,0,0,0,0,0,0,0));
+         }
+
+Finally we lauch a new current tile if needed.
+         # launching new tile
+         @{$curr_tile->[0]} = @{$pieces{$next_tile}}; +$curr_tile->[1]     = 4;
@@ -2331,6 +2362,10 @@

Score and Game State

Showing the Game

+

In the show handler we iterate through each element in the store and +grid array and place the right colored tile where needed (using the +numbers).

+
+ # renders game objects on the screen
+ $app->add_show_handler( + sub { @@ -2367,8 +2402,11 @@ Showing the Game + ); + } + } - + } - +$score_text->write_xy( $app, 248, 20, "Next Piece" ); + + } + + Lastly we draw texts needed. + + +$score_text->write_xy( $app, 248, 20, "Next Piece" ); +$score_text->write_xy( $app, 248, 240, "Score:$score" );
+         # finally, we update the screen
+         $app->update; @@ -3710,7 +3748,7 @@ Conclusion Pixel Effects In this chapter we will look at how to use pixel effects in Perl. Pixel -effects are operations that are done directly on the bacnk of a +effects are operations that are done directly on the bank of a SDL_Surface's pixel. These effects are used to do visual effects in games and applications, most notably by Frozen Bubble. @@ -3739,7 +3777,7 @@ Sol's Ripple Effect Pure Perl - First lets make the effect in pure perl. To do any operations with a + First lets make the effect in pure Perl. To do any operations with a SDL::Surface we must do SDL::Video::lock_surface() call as seen below. Locking the surface prevents other process in SDL from accessing the surface. The @@ -3798,7 +3836,7 @@ Pure Perl Inline Effects - In the below example we use Inline to write inline + In the below example we use Inline to write Inline C code to handle the pixel effect for us. SDL now provides support to work with Inline. The render callback is now moved to C code, using Inline C. @@ -3859,10 +3897,6 @@ Inline Effects END - Modules - - Making it usable at least. - Additional Modules PDL Binary file not shown. Binary file not shown.  @@ -8,6 +8,15 @@ In this chapter we work on creating the classic Tetris game using what we have l \label{fig:tetris} +=begin sidebar + +=head2 Get Tetris Game + +Get the tetris code from U. + +=end sidebar + + =head1 The Game Window First we will make our window with a fixed size so we can place our art work @@ -134,6 +143,8 @@ there until a line is cleared.$next_tile = shuffle(keys %pieces); +To rotate a piece we apply a transformation on each element of the piece. + + sub rotate_piece { + my $_piece = shift; + my$_rotated = []; @@ -145,6 +156,8 @@ there until a line is cleared. + return $_rotated; + } +Additionally we do a simple collision checking between the non zero elements in the pieces with the direction the user wants to move. + + sub can_move_piece { + my$direction = shift; + my $amount = shift || 1; @@ -176,6 +189,8 @@ there until a line is cleared. + return 1; + } +Finally we move the move piece by using the collision check and overlaying the piece array into the C<@grid> for each next position. + + sub move_piece { + my$direction = shift; + my $amount = shift || 1; @@ -211,6 +226,8 @@ there until a line is cleared. + } + } +Finally we hook it into the event handler where we use the events to move the pieces in the right direction. + + sub trigger_move_event_handler { + my ($event, $app ) = @_; + if($event->type == SDL_KEYDOWN ) { @@ -236,14 +253,21 @@ there until a line is cleared. =head2 Score and Game State +Next we add the move handler to update the game state. In tetris the game state can be summarized as the grid, current piece and the score. In this move handler we update all these things . + + $app->add_move_handler( sub { + my ($step, $app ) = @_; + +We update the current piece's state as movable or fixed. + + if(can_move_piece('down',$step / 2)) { + move_piece('down', $step / 2); + } + else { + store_piece($curr_tile); # placing the tile + + +We update the status of the grid and see if there are lines to remove. + # checking for lines to delete + my $y; + my @to_delete = (); @@ -253,17 +277,22 @@ there until a line is cleared. + push(@to_delete,$y); + } + } + +When we delete lines increment the score of the user. + + # deleting lines + foreach(@to_delete) { + splice(@{$store},$_*10, 10); + $score++; + } + +Next for each deleted line we clear the grid. + # adding blank rows to the top + foreach(@to_delete) { + splice(@{$store}, 0, 0, (0,0,0,0,0,0,0,0,0,0)); + } + +Finally we lauch a new current tile if needed. + # launching new tile + @{$curr_tile->[0]} = @{$pieces{$next_tile}}; +$curr_tile->[1] = 4; @@ -275,6 +304,8 @@ there until a line is cleared. =head2 Showing the Game +In the show handler we iterate through each element in the store and grid array and place the right colored tile where needed (using the numbers). + + # renders game objects on the screen + $app->add_show_handler( + sub { @@ -312,6 +343,9 @@ there until a line is cleared. + } + } + } + +Lastly we draw texts needed. + +$score_text->write_xy( $app, 248, 20, "Next Piece" ); +$score_text->write_xy( $app, 248, 240, "Score:$score" ); + # finally, we update the screen