Skip to content

Commit

Permalink
Reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpalmer committed Jan 10, 2011
1 parent 42d4fc0 commit 9229037
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions lib/Games/Sokoban/SDL.pm
Expand Up @@ -11,39 +11,19 @@ use SDLx::App;
use SDLx::Sprite;
use SDLx::Sprite::Animated;
use Games::Sokoban;
use File::ShareDir;
use Path::Class;

my $size = 32;

my $grid;
my @boxes;

my ( $player_x, $player_y, $player_vx, $player_vy, $player_moving,
my ( $player, $player_x, $player_y, $player_vx, $player_vy, $player_moving,
$player_direction, $player_box );

my $share_dir = Path::Class::Dir->new('share') or die $!;
my ( $background, $wall, $box, $goal );

#= Path::Class::Dir->new( File::ShareDir::dist_dir('Games-Sokoban-SDL') );

my $background = SDLx::Surface->new( w => 640, h => 480 );

my $wall = SDLx::Surface->load( $share_dir->file('wall.bmp') );
my $box = SDLx::Surface->load( $share_dir->file('box.bmp') );
my $goal = SDLx::Surface->load( $share_dir->file('goal.bmp') );

my $player = SDLx::Sprite::Animated->new(
image => $share_dir->file('player.bmp'),
rect => SDL::Rect->new( 0, 0, $size, $size ),
ticks_per_frame => 10,
sequences => {
'north' => [ [ 0, 1 ], [ 0, 2 ], [ 0, 0 ] ],
'south' => [ [ 2, 1 ], [ 2, 2 ], [ 2, 0 ] ],
'west' => [ [ 3, 0 ], [ 3, 1 ], [ 3, 2 ] ],
'east' => [ [ 1, 0 ], [ 1, 1 ], [ 1, 2 ] ],
},
);
$player->sequence('south');
my $share_dir;

sub init_level {
my $level
Expand Down Expand Up @@ -227,18 +207,44 @@ sub handle_show {
$app->update();
}

my $app = SDLx::App->new(
w => 640,
h => 480,
exit_on_quit => 1,
);

$app->add_show_handler( \&handle_show );
$app->add_event_handler( \&handle_event );
$app->add_move_handler( \&handle_move );
sub run {
my $app = SDLx::App->new(
w => 640,
h => 480,
exit_on_quit => 1,
);

$app->add_show_handler( \&handle_show );
$app->add_event_handler( \&handle_event );
$app->add_move_handler( \&handle_move );

$share_dir = Path::Class::Dir->new('share') or die $!;

$background = SDLx::Surface->new( w => 640, h => 480 );

$wall = SDLx::Surface->load( $share_dir->file('wall.bmp') );
$box = SDLx::Surface->load( $share_dir->file('box.bmp') );
$goal = SDLx::Surface->load( $share_dir->file('goal.bmp') );

$player = SDLx::Sprite::Animated->new(
image => $share_dir->file('player.bmp'),
rect => SDL::Rect->new( 0, 0, $size, $size ),
ticks_per_frame => 10,
sequences => {
'north' => [ [ 0, 1 ], [ 0, 2 ], [ 0, 0 ] ],
'south' => [ [ 2, 1 ], [ 2, 2 ], [ 2, 0 ] ],
'west' => [ [ 3, 0 ], [ 3, 1 ], [ 3, 2 ] ],
'east' => [ [ 1, 0 ], [ 1, 1 ], [ 1, 2 ] ],
},
);
$player->sequence('south');
$player->alpha_key( [ 255, 0, 0 ] );

init_level();

$app->run();
}

$player->alpha_key( [ 255, 0, 0 ] );
init_level();
$app->run();
run() unless caller();

1;

0 comments on commit 9229037

Please sign in to comment.