blib/lib/Game/Snake/Raylib.pm

filter: Statementstmt 59.3% 102 / 172 Branchbran 38.7% 31 / 80 Conditioncond 16.6% 4 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 84.6% 22 / 26 Podpod 87.5% 7 / 8 totaltotal 51.0% 166 / 325 CC 53 SCAR 49.6
CC
53
Cov
50%
start
455.4
__ANON__
90.0
BUILD
12.2
CRAP 142.3
↑ Summary blib/lib/Game/Snake/Terminal.pm »
1 package Game::Snake::Raylib;
2  
3 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Game::Snake;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Game::Snake::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Game::Snake::Ticker;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our $VERSION = '0.12';
19  
20 2 my $WHY;
21  
22 2 ▶sub available {
Subroutine available: called CC=3 SCAR=11.5
Pod documented
23 2 ▶ return 1 if eval { require Raylib::FFI; require Raylib::Color; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Raylib::FFI; require Raylib::Color; 1 } } 0 2 2
24 2 my $why = $@;
25 2 $why =~ s/ at \S+ line \d+\.?\s*//g;
26 2 $why =~ s/\n.*//s;
27 2 ▶ $why = 'the window needs Raylib::FFI, which is not installed: cpanm Raylib::FFI, or play in the terminal without --raylib'
Branch: 1 branch
BranchTrueFalseTotal
if $why =~ /\ACan't locate Raylib/u 0 2 2
28 if $why =~ /\ACan't locate Raylib/;
29 2 $why =~ s/\s+\z//;
30 2 $WHY = $why;
31 2 return 0;
32 }
33  
34 3 ▶sub why_not { return $WHY }
Subroutine why_not: called CC=1 SCAR=0.0
Pod documented
35  
36 2 my %KEY = (
37 265 => 'up', 264 => 'down', 263 => 'left', 262 => 'right',
38 87 => 'up', 83 => 'down', 65 => 'left', 68 => 'right',
39 75 => 'up', 74 => 'down', 72 => 'left', 76 => 'right',
40 80 => 'pause', 32 => 'pause',
41 81 => 'quit', 256 => 'quit',
42 257 => 'again',
43 );
44  
45 2 my %TILE = (
46 head => { up => [ 192, 0 ], right => [ 256, 0 ], left => [ 192, 64 ], down => [ 256, 64 ] },
47 tail => { up => [ 192, 128 ], right => [ 256, 128 ], left => [ 192, 192 ], down => [ 256, 192 ] },
48 body => { horizontal => [ 64, 0 ], vertical => [ 128, 64 ] },
49 bend => {
50 'left:down' => [ 0, 0 ], 'up:right' => [ 0, 0 ],
51 'right:down' => [ 128, 0 ], 'up:left' => [ 128, 0 ],
52 'left:up' => [ 0, 64 ], 'down:right' => [ 0, 64 ],
53 'right:up' => [ 128, 128 ], 'down:left' => [ 128, 128 ],
54 },
55 food => [ 0, 192 ],
56 );
57  
58 2 my $TILE_SIZE = 64;
59  
60 14 ▶sub key_for {
Subroutine key_for: called CC=2 SCAR=6.9
Pod documented
61 14 my ($class, $code) = @_;
62 14 ▶ return defined $code ? $KEY{$code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 13 1 14
63 }
64  
65 26785 ▶my $straight = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
66 26785 my ($segment) = @_;
67 26785 ▶ return $TILE{body}{ $segment->{dir} =~ /\A(?:left|right)\z/ ? 'horizontal' : 'vertical' };
Branch: 1 branch
BranchTrueFalseTotal
$segment->{'dir'} =~ /\A(?:left|right)\z/u ? : 11536 15249 26785
68 2 };
69  
70 35211 ▶sub tile_for {
Subroutine tile_for: called CC=7 SCAR=19.5
Pod documented
71 35211 my ($class, $kind, $segment) = @_;
72 35211 ▶ return $TILE{food} if $kind eq 'food';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "food" 1 35210 35211
73 35210 ▶ return $TILE{head}{ $segment->{dir} } if $kind eq 'head';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "head" 984 34226 35210
74 34226 ▶ return $TILE{tail}{ $segment->{bend} || $segment->{dir} } if $segment->{last};
Branch: 1 branch
BranchTrueFalseTotal
if $segment->{"last"} 985 33241 34226
Condition66%2/3
$segment->{'bend'} || $segment->{'dir'}
l!l&&r!l&&!r
19840
MC/DC0%0/2
$segment->{'bend'} || $segment->{'dir'}
$segment->{'bend'} $segment->{'dir'}
Truth table66%2/3
A || B
A= $segment->{'bend'}B= $segment->{'dir'}
ABresult
1X1
011
000
75 33241 ▶ return $TILE{bend}{ "$segment->{dir}:$segment->{bend}" } || $straight->($segment) if $segment->{bend};
Branch: 1 branch
BranchTrueFalseTotal
if $segment->{"bend"} 6457 26784 33241
Condition66%2/3
$TILE{'bend'}{"$segment->{'dir'}:$segment->{'bend'}"} || &$straight($segment)
l!l&&r!l&&!r
645610
MC/DC0%0/2
$TILE{'bend'}{"$segment->{'dir'}:$segment->{'bend'}"} || &$straight($segment)
$TILE{'bend'}{"$segment->{'dir'}:$segment->{'bend'}"} &$straight($segment)
Truth table66%2/3
A || B
A= $TILE{'bend'}{"$segment->{'dir'}:$segment->{'bend'}"}B= &$straight($segment)
ABresult
1X1
011
000
76 26784 return $straight->($segment);
77 }
78  
79 1 ▶sub tiles { return { %TILE } }
Subroutine tiles: called CC=1 SCAR=0.0
Pod documented
80  
81 1 ▶sub sheet {
Subroutine sheet: called CC=1 SCAR=0.0
Pod documented
82 1 return File::Spec->catfile(File::Basename::dirname(__FILE__), 'resources', 'snake-graphics.png');
83 }
84  
85 2 has cols => (is => 'ro');
86 2 has rows => (is => 'ro');
87  
88 has seed => (
89 is => 'ro',
90 isa => Str,
91 default => ''
92 2 );
93  
94 has level => (
95 is => 'ro',
96 isa => Int,
97 default => 2
98 2 );
99  
100 has watch => (
101 is => 'ro',
102 isa => Int,
103 default => 0
104 2 );
105  
106 has cell => (
107 is => 'ro',
108 isa => Int,
109 default => 20
110 2 );
111  
112 has top => (
113 is => 'ro',
114 isa => Int,
115 default => 24
116 2 );
117  
118 has fps => (
119 is => 'ro',
120 isa => Int,
121 default => 60
122 2 );
123  
124 has title => (
125 is => 'ro',
126 isa => Str,
127 default => 'Snake'
128 2 );
129  
130 has max_ticks => (
131 is => 'ro',
132 isa => Int,
133 default => 0
134 2 );
135  
136 has game => (
137 is => 'rw',
138 isa => Object
139 2 );
140  
141 has clock => (
142 is => 'ro',
143 isa => Any,
144 7 ▶ default => sub { \&Time::HiRes::time }
Subroutine __ANON__: called CC=1 SCAR=0.0
145 2 );
146  
147 has ticker => (
148 is => 'rw',
149 isa => Object
150 2 );
151  
152 has frames => (
153 is => 'rw',
154 isa => Int,
155 default => 0
156 2 );
157  
158 has refused => (
159 is => 'rw',
160 isa => Int,
161 default => 0
162 2 );
163  
164 has high => (
165 is => 'rw',
166 isa => Int,
167 default => 0
168 2 );
169  
170 7 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=25.0
Pod undocumented
171 7 my ($self) = @_;
172  
173 7 Game::Snake::Bot->describe($self->level);
174  
175 6 for my $name (qw/ cols rows cell top fps max_ticks /) {
176 31 my $value = $self->$name;
177 31 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 7 24 31
178 24 ▶ Carp::croak("Game::Snake::Raylib: $name must be a whole number, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[0-9]+\z/u 1 23 24
179 unless $value =~ /\A[0-9]+\z/;
180 }
181 5 ▶ Carp::croak('Game::Snake::Raylib: a cell is at least 4 pixels') if $self->cell < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cell < 4 1 4 5
182 4 ▶ Carp::croak('Game::Snake::Raylib: the frame rate is at least 1') if $self->fps < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fps < 1 1 3 4
183 3 ▶ Carp::croak('Game::Snake::Raylib: the clock is a code reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->clock eq "CODE" 0 3 3
184 unless ref $self->clock eq 'CODE';
185  
186 3 ▶ unless ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->game) 3 0 3
187 3 my $seed = $self->seed;
188 3 ▶ $seed = Digest::SHA::sha256_hex(join ':', $self->clock->(), $$)
Branch: 1 branch
BranchTrueFalseTotal
unless length $seed 0 3 3
189 unless CORE::length $seed;
190 3 ▶ $self->game(Game::Snake->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $self->cols ? : 2 1 3
defined $self->rows ? : 1 2 3
191 cols => (defined $self->cols ? $self->cols : 40),
192 rows => (defined $self->rows ? $self->rows : 28),
193 seed => $seed,
194 ));
195 }
196  
197 2 ▶ $self->ticker(Game::Snake::Ticker->new(clock => $self->clock)) unless $self->ticker;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ticker 2 0 2
198  
199 2 return;
200 }
201  
202 0 ▶my $rectangle = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
203 0 my ($x, $y, $w, $h) = @_;
204 0 return Raylib::FFI::Rectangle->new(x => $x, y => $y, width => $w, height => $h);
205 2 };
206  
207 0 ▶my $blit = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 my ($self, $sheet, $tile, $x, $y) = @_;
209 0 my $cell = $self->cell;
210 0 Raylib::FFI::DrawTexturePro(
211 $sheet,
212 $rectangle->($tile->[0], $tile->[1], $TILE_SIZE, $TILE_SIZE),
213 $rectangle->($cell * $x, $self->top + $cell * $y, $cell, $cell),
214 Raylib::FFI::Vector2D->new(x => 0, y => 0),
215 0,
216 Raylib::Color::WHITE(),
217 );
218 0 return;
219 2 };
220  
221 0 ▶my $draw = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
222 0 my ($self, $sheet, $paused) = @_;
223 0 my $game = $self->game;
224 0 my $cell = $self->cell;
225 0 my ($cols, $rows) = ($game->cols, $game->rows);
226  
227 0 Raylib::FFI::ClearBackground(Raylib::Color::BLACK());
228  
229 0 for my $y (0 .. $rows - 1) {
230 0 for my $x (0 .. $cols - 1) {
231 0 ▶ Raylib::FFI::DrawRectangleRec(
Branch: 1 branch
BranchTrueFalseTotal
($x + $y) % 2 ? : 0 0 0
232 $rectangle->($cell * $x, $self->top + $cell * $y, $cell, $cell),
233 ($x + $y) % 2 ? Raylib::Color::GRAY() : Raylib::Color::DARKGRAY(),
234 );
235 }
236 }
237  
238 0 ▶ if (my $food = $game->food) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $food = $game->food) 0 0 0
239 0 $self->$blit($sheet, $TILE{food}, @$food);
240 }
241  
242 0 my $segments = $game->segments;
243 0 for my $i (0 .. $#$segments) {
244 0 my $segment = $segments->[$i];
245 0 ▶ my $tile = __PACKAGE__->tile_for($i == 0 ? 'head' : 'body', $segment);
Branch: 1 branch
BranchTrueFalseTotal
$i == 0 ? : 0 0 0
246 0 $self->$blit($sheet, $tile, $segment->{x}, $segment->{y});
247 }
248  
249 Raylib::FFI::DrawText(
250 0 sprintf('Score: %d High score: %d Speed: %d/s', $game->score, $self->high, $game->speed),
251 10, 2, 20, Raylib::Color::WHITE());
252  
253 0 my ($w, $h) = ($cell * $cols, $self->top + $cell * $rows);
254 0 ▶ if ($game->status eq 'finished') {
Branch: 2 branches
BranchTrueFalseTotal
if ($game->status eq 'finished') { } 0 0 0
elsif ($paused) { } 0 0 0
255 0 Raylib::FFI::DrawText('Game Over', int($w / 2) - 110, int($h / 2) - 40, 40, Raylib::Color::WHITE());
256 0 Raylib::FFI::DrawText($game->result->stringify, int($w / 2) - 160, int($h / 2) + 10, 20, Raylib::Color::WHITE());
257 0 Raylib::FFI::DrawText('Enter plays again, q quits', int($w / 2) - 130, int($h / 2) + 40, 20, Raylib::Color::WHITE());
258 }
259 elsif ($paused) {
260 0 Raylib::FFI::DrawText('Paused', int($w / 2) - 60, int($h / 2) - 20, 40, Raylib::Color::WHITE());
261 }
262 0 return;
263 2 };
264  
265 0 ▶my $bot_turn = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
266 0 my ($self) = @_;
267 0 my $game = $self->game;
268 0 my $direction = Game::Snake::Bot->choose($game, level => $self->level);
269 0 ▶ $game->turn($direction) if defined $direction && $direction ne $game->heading;
Branch: 1 branch
BranchTrueFalseTotal
if defined $direction and $direction ne $game->heading 0 0 0
Condition0%0/3
defined $direction and $direction ne $game->heading
!ll&&!rl&&r
000
MC/DC0%0/2
defined $direction and $direction ne $game->heading
defined $direction $direction ne $game->heading
Truth table0%0/3
A and B
A= defined $directionB= $direction ne $game->heading
ABresult
0X0
100
111
270 0 return;
271 2 };
272  
273 1 ▶sub start {
Subroutine start: called CC=22 SCAR=61.2
Pod documented
274 1 my ($self) = @_;
275 1 ▶ return undef unless $self->available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->available 1 0 1
276  
277 0 my $game = $self->game;
278 0 my $cell = $self->cell;
279 0 Raylib::FFI::InitWindow($cell * $game->cols, $self->top + $cell * $game->rows, $self->title);
280 0 Raylib::FFI::SetTargetFPS($self->fps);
281 0 Raylib::FFI::SetExitKey(0);
282 0 my $sheet = Raylib::FFI::LoadTexture($self->sheet);
283  
284 0 my $ticker = $self->ticker->start(1 / $game->speed);
285 0 my ($paused, $quit) = (0, 0);
286  
287 0 ▶ until ($quit || Raylib::FFI::WindowShouldClose()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quit or Raylib::FFI::WindowShouldClose()) 0 0 0
Condition0%0/3
$quit or Raylib::FFI::WindowShouldClose()
l!l&&r!l&&!r
000
MC/DC0%0/2
$quit or Raylib::FFI::WindowShouldClose()
$quit Raylib::FFI::WindowShouldClose()
Truth table0%0/3
A or B
A= $quitB= Raylib::FFI::WindowShouldClose()
ABresult
1X1
011
000
288 0 ▶ while (my $code = Raylib::FFI::GetKeyPressed()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = Raylib::FFI::GetKeyPressed()) 0 0 0
289 0 ▶ my $key = $KEY{$code} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $KEY{$code} or next 0 0 0
290 0 ▶ if ($key eq 'quit') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'quit') { } 0 0 0
elsif ($key eq 'pause') { } 0 0 0
elsif ($key eq 'again') { } 0 0 0
elsif (not $self->watch and $game->status ne 'finished') { } 0 0 0
291 0 $quit = 1;
292 }
293 elsif ($key eq 'pause') {
294 0 $paused = !$paused;
295 0 ▶ $ticker->start(1 / $game->speed) unless $paused;
Branch: 1 branch
BranchTrueFalseTotal
unless $paused 0 0 0
296 }
297 elsif ($key eq 'again') {
298 0 ▶ next unless $game->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "finished" 0 0 0
299 0 $game->reset;
300 0 $paused = 0;
301 0 $ticker->start(1 / $game->speed);
302 }
303 ▶ elsif (!$self->watch && $game->status ne 'finished') {
Condition0%0/3
not $self->watch and $game->status ne "finished"
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->watch and $game->status ne "finished"
not $self->watch $game->status ne "finished"
Truth table0%0/3
not(A) and B
A= not $self->watchB= $game->status ne "finished"
ABresult
0X0
100
111
304 0 ▶ $self->refused($self->refused + 1) unless $game->turn($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $game->turn($key) 0 0 0
305 }
306 }
307  
308 0 ▶ if (!$paused && $game->status ne 'finished' && $ticker->due) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $paused and $game->status ne "finished" and $ticker->due) 0 0 0
Condition0%0/6
not $paused and $game->status ne "finished" and $ticker->due
!ll&&!rl&&r
000
not $paused and $game->status ne "finished"
!ll&&!rl&&r
000
MC/DC0%0/3
not $paused and $game->status ne "finished" and $ticker->due
not $paused $game->status ne "finished" $ticker->due
Truth table0%0/4
not(A) and B and C
A= not $pausedB= $game->status ne "finished"C= $ticker->due
ABCresult
0XX0
10X0
1100
1111
309 0 ▶ $self->$bot_turn if $self->watch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch 0 0 0
310 0 $game->tick;
311 0 $ticker->tick(1 / $game->speed);
312 0 ▶ $self->high($game->score) if $game->score > $self->high;
Branch: 1 branch
BranchTrueFalseTotal
if $game->score > $self->high 0 0 0
313 }
314  
315 0 ▶ last if $self->max_ticks && $ticker->ticks >= $self->max_ticks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_ticks and $ticker->ticks >= $self->max_ticks 0 0 0
Condition0%0/3
$self->max_ticks and $ticker->ticks >= $self->max_ticks
!ll&&!rl&&r
000
MC/DC0%0/2
$self->max_ticks and $ticker->ticks >= $self->max_ticks
$self->max_ticks $ticker->ticks >= $self->max_ticks
Truth table0%0/3
A and B
A= $self->max_ticksB= $ticker->ticks >= $self->max_ticks
ABresult
0X0
100
111
316  
317 0 Raylib::FFI::BeginDrawing();
318 0 $self->$draw($sheet, $paused);
319 0 Raylib::FFI::EndDrawing();
320 0 $self->frames($self->frames + 1);
321 }
322  
323 0 Raylib::FFI::UnloadTexture($sheet);
324 0 Raylib::FFI::CloseWindow();
325  
326 0 ▶ $game->quit unless $game->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "finished" 0 0 0
327 0 return $game->result;
328 }
329  
330 2 1;
331  
332 __END__
↑ Summary blib/lib/Game/Snake/Terminal.pm »