Permalink
Browse files

all 'magic' numbers have names now

  • Loading branch information...
1 parent 225e2f1 commit e09b49b280ca9178cb0c9aab4dfd9406ec18ad96 Tobias Leich committed Mar 14, 2011
Showing with 34 additions and 21 deletions.
  1. +34 −21 solitaire.pl
View
@@ -37,10 +37,17 @@ package Games::Solitaire;
my $fps = SDLx::FPS->new(fps => 60);
my @selected_cards = ();
my $left_mouse_down = 0;
-my @rewind_deck_1_position = ( 20, 20);
-my @rewind_deck_1_hotspot = ( 40, 40);
-my @rewind_deck_2_position = ( 130, 20);
-my @rewind_deck_2_hotspot = ( 150, 40);
+my @rewind_deck_1_position = ( 20, 20);
+my @rewind_deck_1_hotspot = ( 40, 40);
+my @rewind_deck_2_position = ( 130, 20);
+my @rewind_deck_2_hotspot = ( 150, 40);
+my @left_stack_position = ( 20, 200);
+my @left_stack_hotspot = ( 40, 220);
+my @left_target_position = ( 350, 20);
+my @left_target_hotspot = ( 370, 40);
+my @space_between_stacks = ( 110, 20);
+my $hotspot_offset = 20;
+my %KING_CARDS = (map { $_ => 1 } (12,25,38,51));
init_background();
init_cards();
@@ -75,7 +82,7 @@ sub _handle_mouse_button_up
for(-1..6) {
my $layer = $_ == -1
? $layers->by_position( @rewind_deck_2_hotspot )
- : $layers->by_position( 40 + 110 * $_, 220 );
+ : $layers->by_position( $left_stack_hotspot[0] + $space_between_stacks[0] * $_, $left_stack_hotspot[1] );
my @stack = ($layer, @{$layer->ahead});
$layer = pop @stack if scalar @stack;
@@ -84,7 +91,7 @@ sub _handle_mouse_button_up
&& $layer->data->{visible}
&& !scalar @{$layer->ahead}) {
my $target = $layers->by_position(
- 370 + 110 * int($layer->data->{id} / 13), 40
+ $left_target_hotspot[0] + $space_between_stacks[0] * int($layer->data->{id} / 13), $left_target_hotspot[1]
);
if(can_drop($layer->data->{id}, $target->data->{id})) {
@@ -233,13 +240,13 @@ sub game
# to face-up card
elsif($stack[0]->data->{visible}
&& can_drop($selected_cards[0]->data->{id}, $stack[0]->data->{id})) {
- @position_before = @{$layers->detach_xy($stack[0]->pos->x, $stack[0]->pos->y + 20)};
+ @position_before = @{$layers->detach_xy($stack[0]->pos->x, $stack[0]->pos->y + $space_between_stacks[1])};
$dropped = 1;
}
if($dropped && scalar @position_before) {
- $position_before[0] += 20; # transparent border
- $position_before[1] += 20;
+ $position_before[0] += $hotspot_offset; # transparent border
+ $position_before[1] += $hotspot_offset;
show_card(@position_before);
}
}
@@ -291,7 +298,7 @@ sub game
&& $layer->data->{id} =~ m/\d+/
&& $layer->data->{visible}) {
my $target = $layers->by_position(
- 370 + 11 * int($layer->data->{id} / 13), 40
+ $left_target_hotspot[0] + 11 * int($layer->data->{id} / 13), $left_target_hotspot[1]
);
if(can_drop($layer->data->{id}, $target->data->{id})) {
@@ -316,18 +323,16 @@ sub game
}
}
-my %KING_CARDS = (map { $_ => 1 } (12,25,38,51));
-
sub can_drop {
my $card = shift;
my $card_color = int($card / 13);
my $target = shift;
- my $stack = $layers->by_position(370 + 110 * $card_color, 40);
+ my $stack = $layers->by_position($left_target_hotspot[0] + $space_between_stacks[0] * $card_color, $left_target_hotspot[1]);
#my @stack = $layers->get_layers_behind_layer($stack);
#my @stack = $layers->get_layers_ahead_layer($stack);
- # Kings cannot be put on empty fields.
+ # Kings can be put on empty fields.
if (exists($KING_CARDS{$card})) {
return 1 if $target =~ m/empty_stack/;
}
@@ -384,11 +389,19 @@ sub show_card {
my @layers_;
sub init_background {
- $layers->add(SDLx::Layer->new(SDL::Image::load('data/background.png'), {id => 'background'}));
- $layers->add(SDLx::Layer->new(SDL::Image::load('data/empty_stack.png'), @rewind_deck_1_position, {id => 'rewind_deck'}));
- $layers->add(SDLx::Layer->new(SDL::Image::load('data/empty_stack.png'), @rewind_deck_2_position, {id => 'empty_deck'}));
- $layers->add(SDLx::Layer->new(SDL::Image::load('data/empty_target_' . $_ . '.png'), 350 + 110 * $_, 20, {id => 'empty_target_' . $_})) for(0..3);
- $layers->add(SDLx::Layer->new(SDL::Image::load('data/empty_stack.png'), 20 + 110 * $_, 200, {id => 'empty_stack'})) for(0..6);
+ $layers->add(SDLx::Layer->new(SDL::Image::load('data/background.png'), {id => 'background'}));
+ $layers->add(SDLx::Layer->new(SDL::Image::load('data/empty_stack.png'), @rewind_deck_1_position, {id => 'rewind_deck'}));
+ $layers->add(SDLx::Layer->new(SDL::Image::load('data/empty_stack.png'), @rewind_deck_2_position, {id => 'empty_deck'}));
+
+ $layers->add(
+ SDLx::Layer->new(SDL::Image::load('data/empty_target_' . $_ . '.png'),
+ $left_target_position[0] + $space_between_stacks[0] * $_, $left_target_position[1],
+ {id => 'empty_target_' . $_})) for(0..3);
+
+ $layers->add(
+ SDLx::Layer->new(SDL::Image::load('data/empty_stack.png'),
+ $left_stack_position[0] + $space_between_stacks[0] * $_, $left_stack_position[1],
+ {id => 'empty_stack'})) for(0..6);
}
sub init_cards {
@@ -413,8 +426,8 @@ sub init_cards {
$image = 'data/card_' . $card_value[$_] . '.png';
$visible = 1;
}
- $x = 20 + 110 * $stack_index;
- $y = 200 + 20 * $stack_position;
+ $x = $left_stack_position[0] + $space_between_stacks[0] * $stack_index;
+ $y = $left_stack_position[1] + $space_between_stacks[1] * $stack_position;
$stack_position++;
}

0 comments on commit e09b49b

Please sign in to comment.