Skip to content
Browse files

Cleaned up tetris_pl from server client architecture. Need to clean u…

…p eventing
  • Loading branch information...
1 parent 6607b79 commit a4aa96c677f4f9ea27e60ccfe491e30aee24be95 @kthakore kthakore committed Dec 21, 2010
Showing with 22 additions and 34 deletions.
  1. +19 −33 code_listings/tetris.pl
  2. +3 −1 src/06-tetris.pod
View
52 code_listings/tetris.pl
@@ -10,9 +10,6 @@
use SDLx::Text;
use SDLx::Rect;
use SDLx::Surface;
-
-sub TO_SERVER { SDL_USEREVENT };
-sub TO_CLIENT { SDL_USEREVENT + 1 };
# create our main screen
my $app = SDLx::App->new(
w => 400,
@@ -28,14 +25,8 @@
my @piece = (undef);
push(@piece, SDLx::Surface->load( "data/tetris_$_.png" )) for(1..7);
-my $client = {
- grid => [],
- store => [],
-};
-my $server = {
- grid => [],
- store => [],
-};
+my $grid = [];
+my $store = [];
my %pieces = (
I => [0,5,0,0,
@@ -94,9 +85,9 @@ sub can_move_piece {
return if $direction eq 'right' && $x + $amount + $curr_tile->[1] > 9;
return if $direction eq 'down' && int($y + $amount + $curr_tile->[2]) > 22;
- return if $direction eq 'right' && $server->{store}->[ $x + $amount + $curr_tile->[1] + 10 * int($y + $curr_tile->[2]) ];
- return if $direction eq 'left' && $server->{store}->[ $x - $amount + $curr_tile->[1] + 10 * int($y + $curr_tile->[2]) ];
- return if $direction eq 'down' && $server->{store}->[ $x + $curr_tile->[1] + 10 * int($y + $amount + $curr_tile->[2]) ];
+ return if $direction eq 'right' && $store->[ $x + $amount + $curr_tile->[1] + 10 * int($y + $curr_tile->[2]) ];
+ return if $direction eq 'left' && $store->[ $x - $amount + $curr_tile->[1] + 10 * int($y + $curr_tile->[2]) ];
+ return if $direction eq 'down' && $store->[ $x + $curr_tile->[1] + 10 * int($y + $amount + $curr_tile->[2]) ];
}
}
}
@@ -118,11 +109,11 @@ sub move_piece {
$curr_tile->[2] += $amount;
}
- @{$server->{grid}} = ();
+ @{$grid} = ();
for my $y (0..3) {
for my $x (0..3) {
if($curr_tile->[0]->[$x + 4 * $y]) {
- $server->{grid}->[ $x + $curr_tile->[1] + 10 * ($y + int($curr_tile->[2])) ] = $curr_tile->[0]->[$x + 4 * $y];
+ $grid->[ $x + $curr_tile->[1] + 10 * ($y + int($curr_tile->[2])) ] = $curr_tile->[0]->[$x + 4 * $y];
}
}
}
@@ -134,7 +125,7 @@ sub store_piece {
for my $y (0..3) {
for my $x (0..3) {
if($curr_tile->[0]->[$x + 4 * $y]) {
- $server->{store}->[ $x + $curr_tile->[1] + 10 * ($y + int($curr_tile->[2])) ] = $curr_tile->[0]->[$x + 4 * $y];
+ $store->[ $x + $curr_tile->[1] + 10 * ($y + int($curr_tile->[2])) ] = $curr_tile->[0]->[$x + 4 * $y];
}
}
}
@@ -146,7 +137,7 @@ sub client_event_handler {
if ( $event->type == SDL_KEYDOWN ) {
if ( $event->key_sym & (SDLK_LEFT|SDLK_RIGHT|SDLK_UP|SDLK_DOWN) ) {
my $new_event = SDL::Event->new();
- $new_event->type(TO_SERVER);
+ $new_event->type(SDL_USEREVENT);
$new_event->user_data1($event->key_sym);
SDL::Events::push_event($new_event);
}
@@ -158,18 +149,18 @@ sub client_event_handler {
#warn 'up/down released';
}
}
- elsif ( $event->type == TO_CLIENT ) {
+ elsif ( $event->type == SDL_USEREVENT+1 ) {
if($event->user_code(1)) {
- $client->{grid} = $event->user_data1;
- $client->{store} = $event->user_data2;
+ $grid = $event->user_data1;
+ $store = $event->user_data2;
}
}
}
sub server_event_handler {
my ( $event, $app ) = @_;
- if ( $event->type == TO_SERVER ) {
+ if ( $event->type == SDL_USEREVENT ) {
if(defined $curr_tile) {
if($event->user_data1 == SDLK_LEFT && can_move_piece('left')) {
move_piece('left');
@@ -184,12 +175,7 @@ sub server_event_handler {
$curr_tile->[0] = rotate_piece($curr_tile->[0]);
}
}
- my $new_event = SDL::Event->new();
- $new_event->type(TO_CLIENT);
- $new_event->user_code(1);
- $new_event->user_data1($server->{grid});
- $new_event->user_data2($server->{store});
- SDL::Events::push_event($new_event);
+
}
$event = undef; # should we do this?
}
@@ -211,19 +197,19 @@ sub server_event_handler {
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(@{$server->{store}}[($y*10)..((($y+1)*10)-1)])) {
+ if(min(@{$store}[($y*10)..((($y+1)*10)-1)])) {
push(@to_delete, $y);
}
}
# deleting lines
foreach(@to_delete) {
- splice(@{$server->{store}}, $_*10, 10);
+ splice(@{$store}, $_*10, 10);
}
# adding blank rows to the top
foreach(@to_delete) {
- splice(@{$server->{store}}, 0, 0, (0,0,0,0,0,0,0,0,0,0));
+ splice(@{$store}, 0, 0, (0,0,0,0,0,0,0,0,0,0));
}
# launching new tile
@@ -243,14 +229,14 @@ sub server_event_handler {
my $x = 0;
my $y = 0;
- foreach(@{$client->{store}}) {
+ foreach(@{$store}) {
$piece[$_]->blit( $app, undef, [ 28 + $x%10 * 20, 28 + $y * 20 ] ) if $_;
$x++;
$y++ unless $x % 10;
}
$x = 0;
$y = 0;
- foreach(@{$client->{grid}}) {
+ foreach(@{$grid}) {
$piece[$_]->blit( $app, undef, [ 28 + $x%10 * 20, 28 + $y * 20 ] ) if $_;
$x++;
$y++ unless $x % 10;
View
4 src/06-tetris.pod
@@ -79,7 +79,9 @@ allow us to quickly access our pieces, based on their keys.
Further more we have a 1-dimensional array for each peice that represents a grid of the piece.
-TODO: Explaing the numbers, 1,2,3,4 ...
+The grid of each piece is filled with empty spaces and a number from 1 to 7. When this grid is
+imposed on the game grid, we can use the non zero number to draw the write piece block on to it.
+
=head1 Game flow
=head2 Considerations

0 comments on commit a4aa96c

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