script/bubble-breaker.pl untested

filter: Statementstmt 0.0% 0 / 239 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 77 MC/DCmcdc 0.0% 0 / 40 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 440 CC 15 SCAR 28.4
CC
15
Cov
0%
draw_highscore
42.0
remove_selection
30.0
neighbours
30.0
CRAP 17.1
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use warnings;
5  
6 use Time::HiRes;
7 use File::ShareDir qw(dist_dir);
8 use File::HomeDir;
9 use File::Spec::Functions qw(splitpath catpath catdir catfile);
10  
11 use SDL;
12 use SDL::Event;
13 use SDL::Events;
14 use SDL::Mouse;
15 use SDL::Video;
16 use SDL::VideoInfo;
17 use SDL::Surface;
18 use SDLx::App;
19 use SDLx::Surface;
20 use SDLx::SFont;
21  
22 print STDOUT <<OUT;
23 **************************** Information **********************************
24 Click on a bubble to select all contiguous buubles of same color and double
25 click to destroy them. The more bubbles you destroy at once the more points
26 you get.
27  
28 To quit press ESC.
29  
30 Have fun!
31 ***************************************************************************
32 OUT
33  
34 my $videodriver = $ENV{SDL_VIDEODRIVER};
35 $ENV{SDL_VIDEODRIVER} = 'dummy' if $ENV{'BUBBLEBREAKER_TEST'};
36  
37 # initializing video and retrieving current video resolution
38 SDL::init(SDL_INIT_VIDEO);
39 my $video_info = SDL::Video::get_video_info();
40 my $screen_w = $video_info->current_w;
41 my $screen_h = $video_info->current_h;
42 $ENV{SDL_VIDEO_CENTERED} = 'center';
43 my $app = SDLx::App->new( width => 800, height => 352,
44 depth => 32, title => "BubbleBreaker", color => 0x000000FF,
45 flags => SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_NOFRAME,
46 init => 0, eoq => 1, delay => 20 );
47 my $HOME = File::HomeDir->my_home . "/.bubble-breaker";
48 mkdir($HOME) unless -d $HOME;
49 my ($v, $p, $f) = splitpath(__FILE__);
50 my $SHARE = -e catpath($v, catdir($p, '..', 'share'), 'background.png')
51 ? catpath($v, catdir($p, '..', 'share'))
52 : dist_dir('Games-BubbleBreaker');
53 my $last_click = Time::HiRes::time;
54 my $sfont_white = SDLx::SFont->new( catfile($SHARE, 'font_white.png') );
55 my $sfont_blue = SDLx::SFont->new( catfile($SHARE, 'font_blue.png') );
56  
57 # ingame states
58 my $points = 0;
59 my @controls = ();
60 my %balls = ();
61 my $neighbours = {};
62 my @highscore = ();
63  
64 # images
65 my $background = SDLx::Surface->load( catfile($SHARE, 'background.png') );
66 my @balls = (
67 SDLx::Surface->load( catfile($SHARE, 'red.png') ),
68 SDLx::Surface->load( catfile($SHARE, 'green.png') ),
69 SDLx::Surface->load( catfile($SHARE, 'yellow.png') ),
70 SDLx::Surface->load( catfile($SHARE, 'pink.png') ),
71 SDLx::Surface->load( catfile($SHARE, 'blue.png') )
72 );
73  
74 new_round();
75  
76 if($ENV{'BUBBLEBREAKER_TEST'}) {
77 $app->add_show_handler( sub {
78 if(SDL::get_ticks > 1000) {
79 my $esc_event = SDL::Event->new();
80 $esc_event->type(SDL_KEYDOWN);
81 $esc_event->key_sym(SDLK_ESCAPE);
82 SDL::Events::push_event($esc_event);
83 }
84 elsif(SDL::get_ticks > 3000) {
85 $app->stop;
86 }
87 } );
88 }
89  
90 $app->add_show_handler( sub { $app->update } );
91 $app->add_event_handler( sub {
92 my $e = shift;
93
94 if($e->type == SDL_KEYDOWN && $e->key_sym == SDLK_ESCAPE) {
95 $app->stop;
96 }
97
98 elsif ($e->type == SDL_MOUSEBUTTONDOWN && $e->button_button == SDL_BUTTON_LEFT) {
99 my $time = Time::HiRes::time;
100 if ($time - $last_click < 0.3) {
101 for(@controls) {
102 if($_->[0] < $e->button_x && $e->button_x < $_->[2]
103 && $_->[1] < $e->button_y && $e->button_y < $_->[3]) {
104 remove_selection($neighbours);
105 $neighbours = {};
106
107 $background->blit( $app );
108 for my $x (0..14) {
109 for my $y (0..11) {
110 if( defined $balls{$x}{$y} ) {
111 $balls[$balls{$x}{$y}]->blit( $app, undef, [ 280 + $x * 25, 30 + $y * 25, 0, 0] );
112 }
113 }
114 }
115 SDLx::SFont::print_text($app, 250 - SDLx::SFont::SDL_TEXTWIDTH( $points ), 160, $points );
116 draw_highscore();
117
118 last;
119 }
120 }
121 }
122 elsif( 20 < $e->button_x && $e->button_x < 220
123 && 235 < $e->button_y && $e->button_y < 280) {
124 new_round();
125 }
126 else {
127 $background->blit( $app );
128 for my $x (0..14) {
129 for my $y (0..11) {
130 if( defined $balls{$x}{$y} ) {
131 $balls[$balls{$x}{$y}]->blit( $app, undef, [ 280 + $x * 25, 30 + $y * 25, 0, 0] );
132 }
133 }
134 }
135 SDLx::SFont::print_text($app, 250 - SDLx::SFont::SDL_TEXTWIDTH( $points ), 160, $points );
136 draw_highscore();
137
138 for(@controls) {
139 if($_->[0] < $e->button_x && $e->button_x < $_->[2]
140 && $_->[1] < $e->button_y && $e->button_y < $_->[3]
141 && defined $balls{$_->[4]}{$_->[5]}) {
142 $neighbours = {};
143 neighbours($_->[4], $_->[5], $neighbours);
144 draw_shape($neighbours);
145 last;
146 }
147 }
148 }
149 $last_click = $time;
150 }
151 } );
152  
153 $app->run();
154  
155 sub new_round {
156 $points = 0;
157 @controls = ();
158 %balls = ();
159 $neighbours = {};
160 @highscore = ();
161  
162 $background->blit( $app );
163 SDLx::SFont::print_text($app, 250 - SDLx::SFont::SDL_TEXTWIDTH( $points ), 160, $points );
164 draw_highscore();
165  
166 for my $x (0..14) {
167 for my $y (0..11) {
168 my $color = int(rand(5));
169 $balls[$color]->blit( $app, undef, [ 280 + $x * 25, 30 + $y * 25, 0, 0] );
170 push(@controls, [ 278 + $x * 25, 28 + $y * 25, 303 + $x * 25, 53 + $y * 25, $x, $y]);
171 $balls{$x}{$y} = $color;
172 }
173 }
174 }
175  
176 sub draw_highscore {
177 unless( scalar @highscore ) {
178 if(!-e "$HOME/highscore.dat" && open(FH, ">$HOME/highscore.dat")) {
179 print(FH "42\n");
180 close(FH);
181 }
182
183 if(open(FH, "<$HOME/highscore.dat")) {
184 @highscore = map{/(\d+)/; $1} <FH>;
185 close(FH);
186 }
187 }
188
189 my $line = 0;
190 my @score = reverse sort {$a <=> $b} (@highscore, $points);
191 my $points_drawn = 0;
192 while($line < 10 && $score[$line]) {
193 if($score[$line] == $points && !$points_drawn) {
194 $sfont_white->use;
195 $points_drawn = 1;
196 }
197 SDLx::SFont::print_text($app, 780 - SDLx::SFont::SDL_TEXTWIDTH( $score[$line] ), 60 + 25 * $line, $score[$line++] );
198 $sfont_blue->use;
199 }
200  
201 if(open(FH, ">$HOME/highscore.dat")) {
202 print(FH "$_\n") for @score;
203 close(FH);
204 }
205 }
206  
207 sub remove_selection {
208 my $n = shift;
209
210 my $count = 0;
211 for my $x (keys %$n) {
212 for my $y (keys %{$n->{$x}}) {
213 $balls{$x}{$y} = undef;
214 $count++;
215 }
216 }
217
218 return unless $count > 1;
219
220 $points += int(5 * $count + 1.5**$count);
221
222 for my $x (0..14) {
223 for my $y (0..11) {
224 $y = 11 - $y;
225 unless( defined $balls{$x}{$y} ) {
226 my $above = $y - 1;
227 while(!defined $balls{$x}{$above} && $above > 0) {
228 $above--;
229 }
230
231 $balls{$x}{$y} = $balls{$x}{$above};
232 $balls{$x}{$above} = undef;
233 }
234 }
235 }
236
237 for my $x (0..7) {
238 $x = 7 - $x;
239 my $y = 11;
240 unless( defined $balls{$x}{11} ) {
241 my $left = $x - 1;
242 while(!defined $balls{$left}{11} && $left > 0) {
243 $left--;
244 }
245
246 for $y (0..11) {
247 $y = 11 - $y;
248 $balls{$x}{$y} = $balls{$left}{$y};
249 $balls{$left}{$y} = undef;
250 }
251 }
252 }
253
254 for my $x (7..14) {
255 my $y = 11;
256 unless( defined $balls{$x}{11} ) {
257 my $right = $x + 1;
258 while(!defined $balls{$right}{11} && $right < 14) {
259 $right++;
260 }
261
262 for $y (0..11) {
263 $y = 11 - $y;
264 $balls{$x}{$y} = $balls{$right}{$y};
265 $balls{$right}{$y} = undef;
266 }
267 }
268 }
269 }
270  
271 sub draw_shape {
272 my $n = shift;
273 my %lines = ();
274
275 for my $x (keys %$n) {
276 for my $y (keys %{$n->{$x}}) {
277 $lines{278 + $x * 25}{28 + $y * 25}{303 + $x * 25}{28 + $y * 25}++;
278 $lines{278 + $x * 25}{53 + $y * 25}{303 + $x * 25}{53 + $y * 25}++;
279 $lines{278 + $x * 25}{28 + $y * 25}{278 + $x * 25}{53 + $y * 25}++;
280 $lines{303 + $x * 25}{28 + $y * 25}{303 + $x * 25}{53 + $y * 25}++;
281 }
282 }
283
284 for my $x1 (keys %lines) {
285 for my $y1 (keys %{$lines{$x1}}) {
286 for my $x2 (keys %{$lines{$x1}{$y1}}) {
287 for my $y2 (keys %{$lines{$x1}{$y1}{$x2}}) {
288 if($lines{$x1}{$y1}{$x2}{$y2} == 1) {
289 $app->draw_line([$x1, $y1], [$x2, $y2], 0x153C99FF);
290 }
291 }
292 }
293 }
294 }
295 }
296  
297 sub neighbours {
298 my ($x, $y, $n) = @_;
299
300 if(defined $balls{$x}{$y - 1} && $balls{$x}{$y - 1} == $balls{$x}{$y} && !$n->{$x}->{$y - 1}) {
301 $n->{$x}->{$y} = 1;
302 $n->{$x}->{$y - 1} = 1;
303 neighbours($x, $y - 1, $n);
304 }
305
306 if(defined $balls{$x}{$y + 1} && $balls{$x}{$y + 1} == $balls{$x}{$y} && !$n->{$x}->{$y + 1}) {
307 $n->{$x}->{$y} = 1;
308 $n->{$x}->{$y + 1} = 1;
309 neighbours($x, $y + 1, $n);
310 }
311
312 if(defined $balls{$x - 1}{$y} && $balls{$x - 1}{$y} == $balls{$x}{$y} && !$n->{$x - 1}->{$y}) {
313 $n->{$x}->{$y} = 1;
314 $n->{$x - 1}->{$y} = 1;
315 neighbours($x - 1, $y, $n);
316 }
317
318 if(defined $balls{$x + 1}{$y} && $balls{$x + 1}{$y} == $balls{$x}{$y} && !$n->{$x + 1}->{$y}) {
319 $n->{$x}->{$y} = 1;
320 $n->{$x + 1}->{$y} = 1;
321 neighbours($x + 1, $y, $n);
322 }
323 }
324  
325 if ($videodriver) {
326 $ENV{SDL_VIDEODRIVER} = $videodriver;
327 } else {
328 delete $ENV{SDL_VIDEODRIVER};
329 }