lib/Games/Snake.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Games/Snake/Level.pm ↑ Summary
1 package Games::Snake;
2 {
3 $Games::Snake::VERSION = '0.000001';
4 }
5  
6 # ABSTRACT: Snake game
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MooX::Types::MooseLike::Base qw( Int ArrayRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 0 ▶use SDL 2.500;
Subroutine BEGIN: called CC=1 SCAR=2.6
14 use SDL::Event;
15 use SDLx::App;
16 use Games::Snake::Player;
17 use Games::Snake::Level;
18  
19 has app => (
20 is => 'ro',
21 lazy => 1,
22 builder => '_build_app',
23 handles => [qw( run )],
24 );
25  
26 has size => (
27 is => 'ro',
28 isa => Int,
29 default => quote_sub q{ 10 },
30 );
31  
32 has player => (
33 is => 'rw',
34 lazy => 1,
35 builder => '_build_player',
36 );
37  
38 has level => (
39 is => 'ro',
40 lazy => 1,
41 builder => '_build_level',
42 );
43  
44 has apple => (
45 is => 'rw',
46 isa => ArrayRef,
47 lazy => 1,
48 builder => '_build_apple',
49 );
50  
51 sub _build_app {
52 return SDLx::App->new(
53 title => 'Snake',
54 w => 800,
55 h => 600,
56 eoq => 1,
57 dt => 0.05,
58 delay => 20,
59 );
60 }
61  
62 sub _build_level {
63 my ($self) = @_;
64 return Games::Snake::Level->new(
65 size => $self->size,
66 w => $self->app->w / $self->size,
67 h => $self->app->h / $self->size,
68 color => 0x00C2BB0FF,
69 );
70 }
71  
72 sub _build_player {
73 my ($self) = @_;
74 return Games::Snake::Player->new(
75 size => $self->size,
76 color => 0x6EC200FF,
77 growing => 9,
78 segments => [ [ 40, 30 ] ],
79 direction => [ 1, 0 ],
80 );
81 }
82  
83 sub _build_apple {
84 my ($self) = @_;
85  
86 my $level = $self->level;
87 my $player = $self->player;
88  
89 my $coord;
90  
91 do {
92 $coord = [ int( rand( $level->w ) ), int( rand( $level->h ) ) ];
93 } while ( $player->is_segment($coord) || $level->is_wall($coord) );
94  
95 return $coord;
96 }
97  
98 sub BUILD {
99 my ($self) = @_;
100  
101 my $app = $self->app;
102 $app->add_event_handler( sub { $self->handle_event(@_) } );
103 $app->add_move_handler( sub { $self->handle_move(@_) } );
104 $app->add_show_handler( sub { $self->handle_show(@_) } );
105 }
106  
107 sub handle_event {
108 my ( $self, $event, $app ) = @_;
109  
110 my $player = $self->player;
111  
112 if ( $event->type == SDL_KEYDOWN ) {
113 $player->direction( [ -1, 0 ] ) if $event->key_sym == SDLK_LEFT;
114 $player->direction( [ 1, 0 ] ) if $event->key_sym == SDLK_RIGHT;
115 $player->direction( [ 0, -1 ] ) if $event->key_sym == SDLK_UP;
116 $player->direction( [ 0, 1 ] ) if $event->key_sym == SDLK_DOWN;
117  
118 my $key = SDL::Events::get_key_name( $event->key_sym );
119 if ( !$player->alive && $key eq 'r' ) {
120 $self->player( $self->_build_player );
121 }
122 }
123 }
124  
125 sub handle_move {
126 my ( $self, $step, $app, $t ) = @_;
127  
128 my $level = $self->level;
129 my $player = $self->player;
130  
131 $player->move($t);
132  
133 if ( $player->hit_self() || $level->is_wall( $player->head ) ) {
134 $player->alive(0);
135 }
136 elsif ($player->head->[0] == $self->apple->[0]
137 && $player->head->[1] == $self->apple->[1] )
138 {
139 $player->speed( $player->speed * 0.9 );
140 $player->growing( $player->growing + 10 );
141 $self->apple( $self->_build_apple );
142 }
143 }
144  
145 sub handle_show {
146 my ( $self, $delta, $app ) = @_;
147  
148 $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000FF );
149  
150 my $size = $self->size;
151 $app->draw_rect(
152 [ ( map { $_ * $size } @{ $self->apple } ), $size, $size ],
153 0xC20006FF );
154  
155 $self->player->draw($app);
156 $self->level->draw($app);
157  
158 if ( !$self->player->alive ) {
159 $app->draw_gfx_text( [ 12, 12 ],
160 0xFFFFFFFF,
161 'Snake length: ' . scalar @{ $self->player->segments } );
162 $app->draw_gfx_text( [ 12, 22 ], 0xFFFFFFFF, 'Press R to restart' );
163 }
164  
165 $app->update();
166 }
167  
168 1;
169  
170  
171  
172 =pod
173  
174 =head1 NAME
175  
176 Games::Snake - Snake game
177  
178 =head1 VERSION
179  
180 version 0.000001
181  
182 =head1 DESCRIPTION
183  
184 Games::Snake is a clone of the classic Snake game.
185  
186 This game was originally created for The SDL Perl Game Contest!
187  
188 See L<snake.pl> for instructions to play the game.
189  
190 =for Pod::Coverage BUILD handle_event handle_move handle_show size
191  
192 =head1 SEE ALSO
193  
194 =over 4
195  
196 =item * L<snake.pl>
197  
198 =item * L<SDL>
199  
200 =item * L<The SDL Perl Game Contest!|http://onionstand.blogspot.com/2011/02/sdl-perl-game-contest.html>
201  
202 =item * L<Snake|http://en.wikipedia.org/wiki/Snake_%28video_game%29>
203  
204 =back
205  
206 =head1 AUTHOR
207  
208 Jeffrey T. Palmer <jtpalmer@cpan.org>
209  
210 =head1 COPYRIGHT AND LICENSE
211  
212 This software is Copyright (c) 2012 by Jeffrey T. Palmer.
213  
214 This is free software, licensed under:
215  
216 The MIT (X11) License
217  
218 =cut
219  
220  
221 __END__
« lib/Games/Snake/Level.pm ↑ Summary