Skip to content
Browse files

initial

  • Loading branch information...
0 parents commit d5780d55d26e9386f614a347e41649911efb81ef @FROGGS committed Mar 20, 2011
BIN background.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN blue.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
285 bubble_breaker.pl
@@ -0,0 +1,285 @@
+#!perl
+
+use strict;
+use warnings;
+
+use Data::Dumper;
+use Time::HiRes;
+
+use SDL;
+use SDL::Events;
+use SDL::Mouse;
+use SDL::Video;
+use SDL::VideoInfo;
+use SDL::Surface;
+use SDLx::App;
+use SDLx::Surface;
+use SDLx::Text;
+
+# initializing video and retrieving current video resolution
+SDL::init(SDL_INIT_VIDEO);
+my $video_info = SDL::Video::get_video_info();
+my $screen_w = $video_info->current_w;
+my $screen_h = $video_info->current_h;
+$ENV{SDL_VIDEO_CENTERED} = 'center';
+my $app = SDLx::App->new( width => 800, height => 352,
+ depth => 32, title => "BubbleBreaker", color => 0x000000FF,
+ flags => SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_NOFRAME,
+ init => 0, eoq => 1, delay => 20 );
+my $last_click = Time::HiRes::time;
+my $label = SDLx::Text->new( color => [ 0x15, 0x3C, 0x99 ], size => 30, h_align => 'right' );
+
+
+# ingame states
+my $points = 0;
+my @controls = ();
+my %balls = ();
+my $neighbours = {};
+my @highscore = ();
+
+# images
+my $background = SDLx::Surface->load('background.png');
+my @balls = (
+ SDLx::Surface->load( 'red.png' ),
+ SDLx::Surface->load( 'green.png' ),
+ SDLx::Surface->load( 'yellow.png' ),
+ SDLx::Surface->load( 'pink.png' ),
+ SDLx::Surface->load( 'blue.png' )
+);
+
+new_round();
+
+$app->add_show_handler( sub { $app->update } );
+$app->add_event_handler( sub {
+ my $e = shift;
+
+ if($e->type == SDL_KEYDOWN && $e->key_sym == SDLK_ESCAPE) {
+ $app->stop;
+ }
+
+ elsif ($e->type == SDL_MOUSEBUTTONDOWN && $e->button_button == SDL_BUTTON_LEFT) {
+ my $time = Time::HiRes::time;
+ if ($time - $last_click < 0.3) {
+ for(@controls) {
+ if($_->[0] < $e->button_x && $e->button_x < $_->[2]
+ && $_->[1] < $e->button_y && $e->button_y < $_->[3]) {
+ remove_selection($neighbours);
+ $neighbours = {};
+
+ $background->blit( $app );
+ for my $x (0..14) {
+ for my $y (0..11) {
+ if( defined $balls{$x}{$y} ) {
+ $balls[$balls{$x}{$y}]->blit( $app, undef, [ 280 + $x * 25, 30 + $y * 25, 0, 0] );
+ }
+ }
+ }
+ $label->write_xy($app, 250, 160, $points);
+ draw_highscore();
+
+ last;
+ }
+ }
+ }
+ elsif( 20 < $e->button_x && $e->button_x < 220
+ && 235 < $e->button_y && $e->button_y < 280) {
+ new_round();
+ }
+ else {
+ $background->blit( $app );
+ for my $x (0..14) {
+ for my $y (0..11) {
+ if( defined $balls{$x}{$y} ) {
+ $balls[$balls{$x}{$y}]->blit( $app, undef, [ 280 + $x * 25, 30 + $y * 25, 0, 0] );
+ }
+ }
+ }
+ $label->write_xy($app, 250, 160, $points);
+ draw_highscore();
+
+ for(@controls) {
+ if($_->[0] < $e->button_x && $e->button_x < $_->[2]
+ && $_->[1] < $e->button_y && $e->button_y < $_->[3]
+ && defined $balls{$_->[4]}{$_->[5]}) {
+ $neighbours = {};
+ neighbours($_->[4], $_->[5], $neighbours);
+ draw_shape($neighbours);
+ last;
+ }
+ }
+ }
+ $last_click = $time;
+ }
+} );
+
+$app->run();
+
+sub new_round {
+ $points = 0;
+ @controls = ();
+ %balls = ();
+ $neighbours = {};
+ @highscore = ();
+
+ $background->blit( $app );
+ $label->write_xy($app, 250, 160, $points);
+ draw_highscore();
+
+ for my $x (0..14) {
+ for my $y (0..11) {
+ my $color = int(rand(5));
+ $balls[$color]->blit( $app, undef, [ 280 + $x * 25, 30 + $y * 25, 0, 0] );
+ push(@controls, [ 278 + $x * 25, 28 + $y * 25, 303 + $x * 25, 53 + $y * 25, $x, $y]);
+ $balls{$x}{$y} = $color;
+ }
+ }
+}
+
+sub draw_highscore {
+ unless( scalar @highscore ) {
+ if(!-e 'highscore.dat' && open(FH, '>highscore.dat')) {
+ print(FH "42\n");
+ close(FH);
+ }
+
+ if(open(FH, '<highscore.dat')) {
+ @highscore = map{/(\d+)/; $1} <FH>;
+ close(FH);
+ }
+ }
+
+ my $line = 0;
+ my @score = reverse sort {$a <=> $b} (@highscore, $points);
+ my $points_drawn = 0;
+ while($line < 10 && $score[$line]) {
+ if($score[$line] == $points && !$points_drawn) {
+ $label->color([0xFF, 0xFF, 0xFF]);
+ $points_drawn = 1;
+ }
+ $label->write_xy($app, 780, 60 + 25 * $line, $score[$line++]);
+ $label->color([0x15, 0x3C, 0x99]);
+ }
+
+ if(open(FH, '>highscore.dat')) {
+ print(FH "$_\n") for @score;
+ close(FH);
+ }
+}
+
+sub remove_selection {
+ my $n = shift;
+
+ my $count = 0;
+ for my $x (keys %$n) {
+ for my $y (keys %{$n->{$x}}) {
+ $balls{$x}{$y} = undef;
+ $count++;
+ }
+ }
+
+ return unless $count > 1;
+
+ $points += int(5 * $count + 1.5**$count);
+
+ for my $x (0..14) {
+ for my $y (0..11) {
+ $y = 11 - $y;
+ unless( defined $balls{$x}{$y} ) {
+ my $above = $y - 1;
+ while(!defined $balls{$x}{$above} && $above > 0) {
+ $above--;
+ }
+
+ $balls{$x}{$y} = $balls{$x}{$above};
+ $balls{$x}{$above} = undef;
+ }
+ }
+ }
+
+ for my $x (0..7) {
+ $x = 7 - $x;
+ my $y = 11;
+ unless( defined $balls{$x}{11} ) {
+ my $left = $x - 1;
+ while(!defined $balls{$left}{11} && $left > 0) {
+ $left--;
+ }
+
+ for $y (0..11) {
+ $y = 11 - $y;
+ $balls{$x}{$y} = $balls{$left}{$y};
+ $balls{$left}{$y} = undef;
+ }
+ }
+ }
+
+ for my $x (7..14) {
+ my $y = 11;
+ unless( defined $balls{$x}{11} ) {
+ my $right = $x + 1;
+ while(!defined $balls{$right}{11} && $right < 14) {
+ $right++;
+ }
+
+ for $y (0..11) {
+ $y = 11 - $y;
+ $balls{$x}{$y} = $balls{$right}{$y};
+ $balls{$right}{$y} = undef;
+ }
+ }
+ }
+}
+
+sub draw_shape {
+ my $n = shift;
+ my %lines = ();
+
+ for my $x (keys %$n) {
+ for my $y (keys %{$n->{$x}}) {
+ $lines{278 + $x * 25}{28 + $y * 25}{303 + $x * 25}{28 + $y * 25}++;
+ $lines{278 + $x * 25}{53 + $y * 25}{303 + $x * 25}{53 + $y * 25}++;
+ $lines{278 + $x * 25}{28 + $y * 25}{278 + $x * 25}{53 + $y * 25}++;
+ $lines{303 + $x * 25}{28 + $y * 25}{303 + $x * 25}{53 + $y * 25}++;
+ }
+ }
+
+ for my $x1 (keys %lines) {
+ for my $y1 (keys %{$lines{$x1}}) {
+ for my $x2 (keys %{$lines{$x1}{$y1}}) {
+ for my $y2 (keys %{$lines{$x1}{$y1}{$x2}}) {
+ if($lines{$x1}{$y1}{$x2}{$y2} == 1) {
+ $app->draw_line([$x1, $y1], [$x2, $y2], 0x153C99FF);
+ }
+ }
+ }
+ }
+ }
+}
+
+sub neighbours {
+ my ($x, $y, $n) = @_;
+
+ if(defined $balls{$x}{$y - 1} && $balls{$x}{$y - 1} == $balls{$x}{$y} && !$n->{$x}->{$y - 1}) {
+ $n->{$x}->{$y} = 1;
+ $n->{$x}->{$y - 1} = 1;
+ neighbours($x, $y - 1, $n);
+ }
+
+ if(defined $balls{$x}{$y + 1} && $balls{$x}{$y + 1} == $balls{$x}{$y} && !$n->{$x}->{$y + 1}) {
+ $n->{$x}->{$y} = 1;
+ $n->{$x}->{$y + 1} = 1;
+ neighbours($x, $y + 1, $n);
+ }
+
+ if(defined $balls{$x - 1}{$y} && $balls{$x - 1}{$y} == $balls{$x}{$y} && !$n->{$x - 1}->{$y}) {
+ $n->{$x}->{$y} = 1;
+ $n->{$x - 1}->{$y} = 1;
+ neighbours($x - 1, $y, $n);
+ }
+
+ if(defined $balls{$x + 1}{$y} && $balls{$x + 1}{$y} == $balls{$x}{$y} && !$n->{$x + 1}->{$y}) {
+ $n->{$x}->{$y} = 1;
+ $n->{$x + 1}->{$y} = 1;
+ neighbours($x + 1, $y, $n);
+ }
+}
BIN green.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN pink.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN red.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN yellow.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d5780d5

Please sign in to comment.
Something went wrong with that request. Please try again.