Skip to content

Commit

Permalink
Cleaned up tetris_pl from server client architecture. Need to clean u…
Browse files Browse the repository at this point in the history
…p eventing
  • Loading branch information
kthakore committed Dec 22, 2010
1 parent 6607b79 commit a4aa96c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
52 changes: 19 additions & 33 deletions code_listings/tetris.pl
Expand Up @@ -10,9 +10,6 @@
use SDLx::Text; use SDLx::Text;
use SDLx::Rect; use SDLx::Rect;
use SDLx::Surface; use SDLx::Surface;

sub TO_SERVER { SDL_USEREVENT };
sub TO_CLIENT { SDL_USEREVENT + 1 };
# create our main screen # create our main screen
my $app = SDLx::App->new( my $app = SDLx::App->new(
w => 400, w => 400,
Expand All @@ -28,14 +25,8 @@
my @piece = (undef); my @piece = (undef);
push(@piece, SDLx::Surface->load( "data/tetris_$_.png" )) for(1..7); push(@piece, SDLx::Surface->load( "data/tetris_$_.png" )) for(1..7);


my $client = { my $grid = [];
grid => [], my $store = [];
store => [],
};
my $server = {
grid => [],
store => [],
};


my %pieces = ( my %pieces = (
I => [0,5,0,0, I => [0,5,0,0,
Expand Down Expand Up @@ -94,9 +85,9 @@ sub can_move_piece {
return if $direction eq 'right' && $x + $amount + $curr_tile->[1] > 9; 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 '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 'right' && $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 'left' && $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 'down' && $store->[ $x + $curr_tile->[1] + 10 * int($y + $amount + $curr_tile->[2]) ];
} }
} }
} }
Expand All @@ -118,11 +109,11 @@ sub move_piece {
$curr_tile->[2] += $amount; $curr_tile->[2] += $amount;
} }


@{$server->{grid}} = (); @{$grid} = ();
for my $y (0..3) { for my $y (0..3) {
for my $x (0..3) { for my $x (0..3) {
if($curr_tile->[0]->[$x + 4 * $y]) { 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];
} }
} }
} }
Expand All @@ -134,7 +125,7 @@ sub store_piece {
for my $y (0..3) { for my $y (0..3) {
for my $x (0..3) { for my $x (0..3) {
if($curr_tile->[0]->[$x + 4 * $y]) { 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];
} }
} }
} }
Expand All @@ -146,7 +137,7 @@ sub client_event_handler {
if ( $event->type == SDL_KEYDOWN ) { if ( $event->type == SDL_KEYDOWN ) {
if ( $event->key_sym & (SDLK_LEFT|SDLK_RIGHT|SDLK_UP|SDLK_DOWN) ) { if ( $event->key_sym & (SDLK_LEFT|SDLK_RIGHT|SDLK_UP|SDLK_DOWN) ) {
my $new_event = SDL::Event->new(); my $new_event = SDL::Event->new();
$new_event->type(TO_SERVER); $new_event->type(SDL_USEREVENT);
$new_event->user_data1($event->key_sym); $new_event->user_data1($event->key_sym);
SDL::Events::push_event($new_event); SDL::Events::push_event($new_event);
} }
Expand All @@ -158,18 +149,18 @@ sub client_event_handler {
#warn 'up/down released'; #warn 'up/down released';
} }
} }
elsif ( $event->type == TO_CLIENT ) { elsif ( $event->type == SDL_USEREVENT+1 ) {
if($event->user_code(1)) { if($event->user_code(1)) {
$client->{grid} = $event->user_data1; $grid = $event->user_data1;
$client->{store} = $event->user_data2; $store = $event->user_data2;
} }
} }
} }


sub server_event_handler { sub server_event_handler {
my ( $event, $app ) = @_; my ( $event, $app ) = @_;


if ( $event->type == TO_SERVER ) { if ( $event->type == SDL_USEREVENT ) {
if(defined $curr_tile) { if(defined $curr_tile) {
if($event->user_data1 == SDLK_LEFT && can_move_piece('left')) { if($event->user_data1 == SDLK_LEFT && can_move_piece('left')) {
move_piece('left'); move_piece('left');
Expand All @@ -184,12 +175,7 @@ sub server_event_handler {
$curr_tile->[0] = rotate_piece($curr_tile->[0]); $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? $event = undef; # should we do this?
} }
Expand All @@ -211,19 +197,19 @@ sub server_event_handler {
my @to_delete = (); my @to_delete = ();
for($y = 22; $y >= 0; $y--) { for($y = 22; $y >= 0; $y--) {
# there is no space if min of this row is true (greater than zero) # 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); push(@to_delete, $y);
} }
} }


# deleting lines # deleting lines
foreach(@to_delete) { foreach(@to_delete) {
splice(@{$server->{store}}, $_*10, 10); splice(@{$store}, $_*10, 10);
} }


# adding blank rows to the top # adding blank rows to the top
foreach(@to_delete) { 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 # launching new tile
Expand All @@ -243,14 +229,14 @@ sub server_event_handler {


my $x = 0; my $x = 0;
my $y = 0; my $y = 0;
foreach(@{$client->{store}}) { foreach(@{$store}) {
$piece[$_]->blit( $app, undef, [ 28 + $x%10 * 20, 28 + $y * 20 ] ) if $_; $piece[$_]->blit( $app, undef, [ 28 + $x%10 * 20, 28 + $y * 20 ] ) if $_;
$x++; $x++;
$y++ unless $x % 10; $y++ unless $x % 10;
} }
$x = 0; $x = 0;
$y = 0; $y = 0;
foreach(@{$client->{grid}}) { foreach(@{$grid}) {
$piece[$_]->blit( $app, undef, [ 28 + $x%10 * 20, 28 + $y * 20 ] ) if $_; $piece[$_]->blit( $app, undef, [ 28 + $x%10 * 20, 28 + $y * 20 ] ) if $_;
$x++; $x++;
$y++ unless $x % 10; $y++ unless $x % 10;
Expand Down
4 changes: 3 additions & 1 deletion src/06-tetris.pod
Expand Up @@ -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. 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 =head1 Game flow


=head2 Considerations =head2 Considerations
Expand Down

0 comments on commit a4aa96c

Please sign in to comment.