Permalink
Browse files

added gfx for tetris, tile rotation works now

  • Loading branch information...
Tobias Leich
Tobias Leich committed Nov 10, 2010
1 parent 9c2b29d commit 3bf506e71f4d3ec6a97d5ea9240b02b6989d82dc
View
@@ -2,27 +2,32 @@
use warnings;
use Data::Dumper;
+use List::Util qw(shuffle);
use SDL;
use SDL::Event;
use SDL::Events;
use SDLx::App;
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 => 500,
- h => 500,
+ w => 400,
+ h => 512,
exit_on_quit => 1,
dt => 0.2,
title => 'SDLx Tetris'
);
# create our game objects
my $score = SDLx::Text->new( font => 'font.ttf', h_align => 'center' );
+my $back = SDLx::Surface->load( 'data/tetris_back.png' );
+my @piece = (undef);
+push(@piece, SDLx::Surface->load( "data/tetris_$_.png" )) for(1..7);
my $client = {
grid => [],
@@ -33,36 +38,39 @@
};
my $tile;
-my @pieces = (
- [4,1,1,2,1,1], # I
- [2,4,0,1,0,2,0,1,0,1], # -
- [4,1,1,2,1,1], # I
- [2,4,0,1,0,2,0,1,0,1], # -
- [3,2,1,0,0,2,1,1], # J
- [2,3,2,1,1,0,1,0], # J
- [3,2,1,0,0,2,1,1], # J
- [3,2,1,0,0,2,1,1], # J
- [3,2,0,0,1,1,1,2], # L
- [3,2,0,0,1,1,1,2], # L
- [3,2,0,0,1,1,1,2], # L
- [3,2,0,0,1,1,1,2], # L
- [2,2,2,1,1,1], # O
- [2,2,2,1,1,1], # O
- [2,2,2,1,1,1], # O
- [2,2,2,1,1,1], # O
- [3,2,0,2,1,1,1,0], # S
- [3,2,0,2,1,1,1,0], # S
- [3,2,0,2,1,1,1,0], # S
- [3,2,0,2,1,1,1,0], # S
- [3,2,0,1,0,1,2,1], # T
- [3,2,0,1,0,1,2,1], # T
- [3,2,0,1,0,1,2,1], # T
- [3,2,0,1,0,1,2,1], # T
- [3,2,1,2,0,0,1,1], # Z
- [3,2,1,2,0,0,1,1], # Z
- [3,2,1,2,0,0,1,1], # Z
- [3,2,1,2,0,0,1,1], # Z
+my %pieces = (
+ I => [0,5,0,0,
+ 0,5,0,0,
+ 0,5,0,0,
+ 0,5,0,0],
+ J => [0,0,0,0,
+ 0,0,6,0,
+ 0,0,6,0,
+ 0,6,6,0,
+ 0,0,0,0],
+ L => [0,0,0,0,
+ 0,2,0,0,
+ 0,2,0,0,
+ 0,2,2,0,
+ 0,0,0,0],
+ O => [0,0,0,0,
+ 0,3,3,0,
+ 0,3,3,0,
+ 0,0,0,0],
+ S => [0,0,0,0,
+ 0,4,4,0,
+ 4,4,0,0,
+ 0,0,0,0],
+ T => [0,0,0,0,
+ 0,7,0,0,
+ 7,7,7,0,
+ 0,0,0,0],
+ Z => [0,0,0,0,
+ 1,1,0,0,
+ 0,1,1,0,
+ 0,0,0,0],
);
+
sub check_collision {
my ($A, $B) = @_;
@@ -75,6 +83,17 @@ sub check_collision {
return 1;
}
+sub rotate {
+ my $_piece = shift;
+ my $_rotated = [];
+ my $_i = 0;
+ for(@{$_piece}) {
+ $_rotated->[$_i + (($_i%4+1)*3) - (5*int($_i/4))] = $_;
+ $_i++;
+ }
+ return $_rotated;
+}
+
sub client_event_handler {
my ( $event, $app ) = @_;
@@ -95,6 +114,7 @@ sub client_event_handler {
print "\n";
}
print "\n";
+ ($tile->[0]) = shuffle(keys %pieces) if defined $tile;
}
}
elsif ( $event->type == SDL_KEYUP ) {
@@ -119,14 +139,14 @@ sub server_event_handler {
if($event->user_data1 == SDLK_LEFT && $tile->[1] > 0) {
$tile->[1]--;
}
- elsif($event->user_data1 == SDLK_RIGHT && $tile->[1] + $pieces[$tile->[0]]->[0] < 10) {
+ elsif($event->user_data1 == SDLK_RIGHT && $tile->[1] + $pieces{$tile->[0]}->[0] < 10) {
$tile->[1]++;
}
- elsif($event->user_data1 == SDLK_DOWN && $tile->[2] + $pieces[$tile->[0]]->[1] - 1 < 20) {
+ elsif($event->user_data1 == SDLK_DOWN && $tile->[2] + $pieces{$tile->[0]}->[1] - 1 < 24) {
$tile->[2]++;
}
elsif($event->user_data1 == SDLK_UP) {
- $tile->[0] = int($tile->[0]/4)*4 + ($tile->[0]%4 + 1)%4;
+ $pieces{$tile->[0]} = rotate($pieces{$tile->[0]});
}
}
my $new_event = SDL::Event->new();
@@ -135,6 +155,7 @@ sub server_event_handler {
$new_event->user_data2($server->{grid});
SDL::Events::push_event($new_event);
}
+ $event = undef; # should we do this?
}
$app->add_event_handler( \&client_event_handler );
@@ -143,24 +164,27 @@ sub server_event_handler {
$app->add_move_handler( sub {
my ( $step, $app ) = @_;
- $tile = [int(rand(7))*4, 4, 0] unless defined $tile;
+ unless(defined $tile) {
+ $tile = ['J', 4, 0];
+ ($tile->[0]) = shuffle(keys %pieces);
+ }
my $x = $tile->[1];
my $y = int($tile->[2]);
- my $w = $pieces[$tile->[0]]->[0];
- my $h = $pieces[$tile->[0]]->[1];
+ my $w = 4;
+ my $h = 4;
@{$server->{grid}} = @{$server->{grid2}};
- for(2..$#{$pieces[$tile->[0]]}) {
- $server->{grid}->[ $x + 10 * $y ] = $pieces[$tile->[0]]->[$_] if $pieces[$tile->[0]]->[$_];
- $x++;
- unless(($_-1) % $w) {
- $y++;
- $x = $tile->[1];
+ for my $y (0..3) {
+ for my $x (0..3) {
+ if($pieces{$tile->[0]}->[$x + 4 * $y]) {
+ $server->{grid}->[ $x + $tile->[1] + 10 * ($y + int($tile->[2])) ] = $pieces{$tile->[0]}->[$x + 4 * $y];
+ }
}
}
+
$tile->[2] += $step * 0.5;
- if($y + $h - 1 >= 20) {
+ if($tile->[2] >= 24) {
@{$server->{grid2}} = @{$server->{grid}};
$tile = undef;
}
@@ -171,11 +195,13 @@ sub server_event_handler {
sub {
# first, we clear the screen
$app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000 );
+ $back->blit( $app );
my $x = 0;
my $y = 0;
foreach(@{$client->{grid}}) {
- $app->draw_rect( [ $x%10 * 20, $y * 20, 19, 19 ], 0xFF0000 ) if $_;
+ #$app->draw_rect( [ 28 + $x%10 * 20, 28 + $y * 20, 19, 19 ], 0xFF0000 ) if $_;
+ $piece[$_]->blit( $app, undef, [ 28 + $x%10 * 20, 28 + $y * 20 ] ) if $_;
$x++;
$y++ unless $x%10;
}
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.

0 comments on commit 3bf506e

Please sign in to comment.