| 1 | package Games::Tetris::Complete; | ||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Games::Tetris::Complete::Shape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Term::ReadKey; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Time::HiRes qw(usleep); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Term::Screen::Uni; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use threads; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use threads::shared; | ||
| 10 | use Thread::Semaphore; | ||
| 11 | our ( @ISA, @EXPORT ); | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | require Exporter; | ||
| 15 | @ISA = qw(Exporter); | ||
| 16 | @EXPORT = qw(play); # symbols to export on request | ||
| 17 | } | ||
| 18 | $| = 1; | ||
| 19 | |||
| 20 | our $VERSION = '0.03'; | ||
| 21 | |||
| 22 | # Globals | ||
| 23 | my $semaphore = Thread::Semaphore->new(); | ||
| 24 | my $print_cond : shared = 0; | ||
| 25 | my $GAME_OVER : shared = 0; | ||
| 26 | my %line_points = ( | ||
| 27 | 1 => 40, | ||
| 28 | 2 => 100, | ||
| 29 | 3 => 300, | ||
| 30 | 4 => 1200 | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub play { | ||
| 34 | my @args = @_ ? @_ : @ARGV ? @ARGV : (); | ||
| 35 | @args = ( width => $args[ 0 ], height => $args[ 1 ] ) | ||
| 36 | if @args == 2 and int $args[ 0 ]; | ||
| 37 | my $self : shared = shared_clone( __PACKAGE__->new( @args ) ); | ||
| 38 | $semaphore->down; # Block console until we get first input | ||
| 39 | my $console_thread = threads->create( \&console_thread, $self ); | ||
| 40 | print "Enter any key to begin..."; | ||
| 41 | get_input( 0 ); | ||
| 42 | $self->active_shape( random_shape() ); | ||
| 43 | $semaphore->up; | ||
| 44 | my $player_thread = threads->create( \&player_thread, $self ); | ||
| 45 | my $game_thread = threads->create( \&game_thread, $self ); | ||
| 46 | $player_thread->detach(); | ||
| 47 | $game_thread->join(); | ||
| 48 | # print "Joined game_thread.\n"; | ||
| 49 | { | ||
| 50 | lock( $print_cond ); | ||
| 51 | $print_cond = -1; | ||
| 52 | cond_signal( $print_cond ); | ||
| 53 | } | ||
| 54 | $console_thread->join(); | ||
| 55 | print "Final Score: " . commify( $self->score ) . "\n"; | ||
| 56 | } | ||
| 57 | |||
| 58 | # perlfaq5 (brian d foy) | ||
| 59 | sub commify { | ||
| 60 | local $_ = shift; | ||
| 61 | 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; | ||
| 62 | return $_; | ||
| 63 | } | ||
| 64 | |||
| 65 | #------------------------------------------------------------------------------# | ||
| 66 | use Moose; | ||
| 67 | |||
| 68 | $SIG{ __DIE__ } = sub { confess $_[ 0 ] }; | ||
| 69 | |||
| 70 | for ( qw/ width height / ) { | ||
| 71 | has $_ => ( | ||
| 72 | is => 'ro', | ||
| 73 | isa => 'Int', | ||
| 74 | required => 1, | ||
| 75 | default => $_ eq 'width' ? 12 : 20 | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | |||
| 79 | has 'score' => ( | ||
| 80 | traits => [ 'Number' ], | ||
| 81 | is => 'ro', | ||
| 82 | isa => 'Int', | ||
| 83 | default => 0, | ||
| 84 | handles => { | ||
| 85 | inc_score => 'add', | ||
| 86 | dec_score => 'sub', | ||
| 87 | set_score => 'set', | ||
| 88 | }, | ||
| 89 | ); | ||
| 90 | has 'level' => ( | ||
| 91 | is => 'rw', | ||
| 92 | isa => 'Num', | ||
| 93 | default => 0, | ||
| 94 | ); | ||
| 95 | |||
| 96 | has 'board' => ( | ||
| 97 | is => 'ro', | ||
| 98 | isa => 'GameGrid', | ||
| 99 | lazy_build => 1, | ||
| 100 | ); | ||
| 101 | |||
| 102 | sub BUILD { | ||
| 103 | my $self = shift; | ||
| 104 | $self->board; # Create board before sharing | ||
| 105 | } | ||
| 106 | |||
| 107 | has [ qw/ active_shape queued_shape / ] => ( | ||
| 108 | is => 'rw', | ||
| 109 | isa => 'Games::Tetris::Complete::Shape', | ||
| 110 | ); | ||
| 111 | |||
| 112 | around 'active_shape' => sub { | ||
| 113 | my ( $orig, $self, $shape ) = @_; | ||
| 114 | return $self->$orig() unless $shape; | ||
| 115 | lock( $GAME_OVER ); | ||
| 116 | |||
| 117 | # Update the board | ||
| 118 | my $board = $self->board; | ||
| 119 | my $shape_grid = $shape->grid; | ||
| 120 | my $shape_nx = $shape->nx; | ||
| 121 | my $x_offset = int( $self->width / 2 ) - int( $shape_nx / 2 ); | ||
| 122 | for my $y ( 0 .. $shape->ny - 1 ) { | ||
| 123 | my $shape_row = $shape_grid->[ $y ]; | ||
| 124 | |||
| 125 | # Note: can't splice shared arrays | ||
| 126 | for ( 0 .. $shape_nx - 1 ) { | ||
| 127 | my $shape_char = $shape_row->[ $_ ]; | ||
| 128 | # Game over if removed chars overlap shape chars | ||
| 129 | $GAME_OVER = 1 | ||
| 130 | if $board->[ $y ][ $x_offset + $_ ] ne ' ' | ||
| 131 | and $shape_char ne ' '; | ||
| 132 | $board->[ $y ][ $x_offset + $_ ] = $shape_char; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | # Set the shape location | ||
| 136 | $shape->ulx( $x_offset ); | ||
| 137 | $shape->uly( 0 ); | ||
| 138 | |||
| 139 | # Enqueue a new shape | ||
| 140 | $self->queued_shape( shared_clone( random_shape() ) ); | ||
| 141 | |||
| 142 | # Set the shape as the active shape | ||
| 143 | $self->$orig( shared_clone( $shape ) ); | ||
| 144 | }; | ||
| 145 | |||
| 146 | #------------------------------------------------------------------------------# | ||
| 147 | |||
| 148 | no Moose; | ||
| 149 | use Carp; | ||
| 150 | __PACKAGE__->meta->make_immutable; | ||
| 151 | |||
| 152 | sub _build_board { | ||
| 153 | my $self = shift; | ||
| 154 | my @board = | ||
| 155 | map { [ map ' ', 0 .. $self->width - 1 ]; } 0 .. $self->height - 1; | ||
| 156 | \@board; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _game_over { | ||
| 160 | lock( $GAME_OVER ); | ||
| 161 | $GAME_OVER; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub player_thread { | ||
| 165 | my $self = shift; | ||
| 166 | while ( 1 ) { | ||
| 167 | last if _game_over(); | ||
| 168 | if ( my $action = get_input( 0.5 ) ) { | ||
| 169 | $semaphore->down; | ||
| 170 | block_to_print() if $action->( $self ); | ||
| 171 | $semaphore->up; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | # print "Exiting player_thread.\n"; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub game_thread { | ||
| 178 | my $self = shift; | ||
| 179 | my $sleep_microseconds = 1_000_000; # 0.2 second(s) | ||
| 180 | my $speedup = 1.1; | ||
| 181 | # $self->level( 0.9 ); Testing only | ||
| 182 | while ( 1 ) { | ||
| 183 | usleep( $sleep_microseconds / ( $speedup * ( int $self->level + 1 ) ) ); | ||
| 184 | last if _game_over(); | ||
| 185 | $semaphore->down; | ||
| 186 | if ( $self->down( 1 ) ) { | ||
| 187 | block_to_print(); | ||
| 188 | } | ||
| 189 | else { | ||
| 190 | $self->next_shape; | ||
| 191 | } | ||
| 192 | $semaphore->up; | ||
| 193 | } | ||
| 194 | # print "Exiting game_thread.\n"; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub next_shape { | ||
| 198 | my $self = shift; | ||
| 199 | # Clear full lines | ||
| 200 | if ( my $cleared = $self->clear_full_lines ) { | ||
| 201 | my $level = $self->level; | ||
| 202 | $self->inc_score( $line_points{ $cleared } * ( int( $level ) + 1 ) ); | ||
| 203 | $self->level( $level += 0.1 * $cleared ); | ||
| 204 | } | ||
| 205 | # Start new shape | ||
| 206 | $self->active_shape( $self->queued_shape ); | ||
| 207 | block_to_print(); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub block_to_print { | ||
| 211 | lock( $print_cond ); | ||
| 212 | $print_cond++; | ||
| 213 | cond_signal( $print_cond ); | ||
| 214 | } | ||
| 215 | |||
| 216 | sub clear_full_lines { | ||
| 217 | my $self = shift; | ||
| 218 | my $board = $self->board; | ||
| 219 | my $width = $self->width; | ||
| 220 | my @cleared; | ||
| 221 | for ( 0 .. $self->height - 1 ) { | ||
| 222 | next if grep $_ eq ' ', @{ $board->[ $_ ] }; | ||
| 223 | push @cleared, $_; | ||
| 224 | } | ||
| 225 | return 0 unless @cleared; | ||
| 226 | |||
| 227 | # Animate! | ||
| 228 | my $sleep_microseconds = 50_000; | ||
| 229 | my $mid = int( $width / 2 ) + $width % 2 - 1; | ||
| 230 | $board->[ $_ ][ $mid ] = ' ' for @cleared; | ||
| 231 | unless ( $width % 2 ) { | ||
| 232 | $board->[ $_ ][ $mid + 1 ] = ' ' for @cleared; | ||
| 233 | } | ||
| 234 | block_to_print(); | ||
| 235 | usleep( $sleep_microseconds ); | ||
| 236 | for ( my $i = 1; $i < $mid; $i++ ) { | ||
| 237 | $board->[ $_ ][ $mid - $i ] = ' ' for @cleared; | ||
| 238 | $board->[ $_ ][ $mid + $i ] = ' ' for @cleared; | ||
| 239 | block_to_print(); | ||
| 240 | usleep( $sleep_microseconds ); | ||
| 241 | } | ||
| 242 | if ( @cleared == 4 ) { | ||
| 243 | # Hell yea tetris, do some random pattern | ||
| 244 | my @chars = ( '+', ' ' ); | ||
| 245 | for my $i ( 1, 2 ) { | ||
| 246 | for my $y ( @cleared ) { | ||
| 247 | # if ( $y == $cleared[ 0 ] or $y == $cleared[ 3 ] ) { | ||
| 248 | if ( $y + $i % 2 ) { | ||
| 249 | $board->[ $y ][ $_ ] = $_ % 2 ? '+' : ' ' | ||
| 250 | for 0 .. $width - 1; | ||
| 251 | } | ||
| 252 | else { | ||
| 253 | $board->[ $y ][ $_ ] = $_ % 2 ? ' ' : '+' | ||
| 254 | for 0 .. $width - 1; | ||
| 255 | } | ||
| 256 | block_to_print(); | ||
| 257 | usleep( $sleep_microseconds ); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | # Update The Board | ||
| 263 | $self->clear_line( $_ ) for @cleared; | ||
| 264 | |||
| 265 | scalar @cleared; | ||
| 266 | } ## end sub clear_full_lines | ||
| 267 | |||
| 268 | sub clear_line { | ||
| 269 | my ( $self, $y ) = @_; | ||
| 270 | my $board = $self->board; | ||
| 271 | my $width = $self->width; | ||
| 272 | for my $y ( reverse 0 .. $y - 1 ) { | ||
| 273 | $board->[ $y + 1 ][ $_ ] = $board->[ $y ][ $_ ] for 0 .. $width - 1; | ||
| 274 | } | ||
| 275 | # Clear first line | ||
| 276 | $board->[ 0 ][ $_ ] = ' ' for 0 .. $width - 1; | ||
| 277 | } | ||
| 278 | |||
| 279 | { | ||
| 280 | my %input_response = ( | ||
| 281 | 'j' => \&left, | ||
| 282 | 'k' => \&down, | ||
| 283 | 'l' => \&right, | ||
| 284 | 'i' => \&rotate_left, | ||
| 285 | 'a' => \&left, | ||
| 286 | 's' => \&down, | ||
| 287 | 'd' => \&right, | ||
| 288 | 'w' => \&rotate_left, | ||
| 289 | 'q' => \&quit, | ||
| 290 | ' ' => \&drop, | ||
| 291 | ); | ||
| 292 | |||
| 293 | sub get_input { | ||
| 294 | my $timeout = @_ ? shift : 0; | ||
| 295 | ReadMode 'cbreak'; | ||
| 296 | my $action; | ||
| 297 | my $got = ReadKey $timeout; | ||
| 298 | # print "Saw key: '$got'\n"; | ||
| 299 | $action = $input_response{ $got } if defined $got; | ||
| 300 | ReadMode 'normal'; | ||
| 301 | $action; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | sub drop { | ||
| 306 | my $self = shift; | ||
| 307 | my $lines = 0; | ||
| 308 | while ( $self->down ) { | ||
| 309 | $lines++; | ||
| 310 | } | ||
| 311 | $self->inc_score( $lines * 2 ); | ||
| 312 | $self->next_shape; | ||
| 313 | $lines; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub rotate_left { | ||
| 317 | my $self = shift; | ||
| 318 | my $board = $self->board; | ||
| 319 | my $shape = $self->active_shape; | ||
| 320 | my $shape_grid = $shape->grid; | ||
| 321 | my ( $nx, $ny ) = ( $shape->nx, $shape->ny ); | ||
| 322 | my ( $ulx, $uly ) = ( $shape->ulx, $shape->uly ); | ||
| 323 | my ( $height, $width ) = ( $self->height, $self->width ); | ||
| 324 | |||
| 325 | # Return if we'd run into a border | ||
| 326 | return if $ny > $nx and $ulx == 0 || $ulx + $nx == $width; | ||
| 327 | return if $nx > $ny and $uly + $ny == $height; | ||
| 328 | |||
| 329 | # Build new rotated shape | ||
| 330 | my @new_grid; | ||
| 331 | for my $x ( reverse 0 .. $nx - 1 ) { | ||
| 332 | push @new_grid, [ map $shape_grid->[ $_ ][ $x ], 0 .. $ny - 1 ]; | ||
| 333 | } | ||
| 334 | my $new_shape = Games::Tetris::Complete::Shape->new( | ||
| 335 | grid => \@new_grid, | ||
| 336 | char => $shape->char, | ||
| 337 | ulx => $ulx - ( $ny - $nx ), | ||
| 338 | uly => $uly + ( $ny - $nx ), | ||
| 339 | ); | ||
| 340 | |||
| 341 | # Return if new covered points overlap border or another shape | ||
| 342 | my @covered = $shape->covered_points; | ||
| 343 | my @covered_new = $new_shape->covered_points; | ||
| 344 | return if grep { | ||
| 345 | my ( $y, $x ) = @$_; | ||
| 346 | $y < 0 || $x < 0 || $y >= $height || $x >= $width | ||
| 347 | } @covered_new; | ||
| 348 | return if grep { | ||
| 349 | my ( $y, $x ) = @$_; | ||
| 350 | $board->[ $y ][ $x ] ne ' ' | ||
| 351 | # Don't count points covered by original shape | ||
| 352 | and !grep { $y == $_->[ 0 ] and $x == $_->[ 1 ] } @covered | ||
| 353 | } @covered_new; | ||
| 354 | |||
| 355 | # Update board and store | ||
| 356 | my $char = $shape->char; | ||
| 357 | $board->[ $_->[ 0 ] ][ $_->[ 1 ] ] = ' ' for @covered; | ||
| 358 | $board->[ $_->[ 0 ] ][ $_->[ 1 ] ] = $char for @covered_new; | ||
| 359 | |||
| 360 | $shape->grid( shared_clone \@new_grid ); | ||
| 361 | $shape->ulx( $ulx - ( $ny - $nx ) ); | ||
| 362 | $shape->uly( $uly + ( $ny - $nx ) ); | ||
| 363 | $shape->nx( $ny ); | ||
| 364 | $shape->ny( $nx ); | ||
| 365 | |||
| 366 | 1; | ||
| 367 | } ## end sub rotate_left | ||
| 368 | |||
| 369 | sub left { shift->horizontal( 0 ) } | ||
| 370 | sub right { shift->horizontal( 1 ) } | ||
| 371 | |||
| 372 | sub horizontal { | ||
| 373 | my ( $self, $right ) = @_; | ||
| 374 | my $shape = $self->active_shape; | ||
| 375 | my $board = $self->board; | ||
| 376 | my ( $ulx, $uly ) = ( $shape->ulx, $shape->uly ); | ||
| 377 | my ( $shape_nx, $shape_ny ) = ( $shape->nx, $shape->ny ); | ||
| 378 | my $shape_grid = $shape->grid; | ||
| 379 | |||
| 380 | # Check if right/left column overlaps next column or end of board | ||
| 381 | return if !$right && !$ulx or $right && $ulx + $shape_nx == $self->width; | ||
| 382 | return | ||
| 383 | if grep { | ||
| 384 | $shape_grid->[ $_ ][ $right ? ( $shape_nx - 1 ) : 0 ] ne ' ' | ||
| 385 | and $board->[ $uly + $_ ][ | ||
| 386 | $right | ||
| 387 | ? ( $ulx + $shape_nx ) | ||
| 388 | : ( $ulx - 1 ) | ||
| 389 | ] ne ' ' | ||
| 390 | } 0 .. $shape_ny - 1; | ||
| 391 | |||
| 392 | for my $i ( 0 .. $shape_nx ) { | ||
| 393 | if ( $i ) { | ||
| 394 | for my $y ( 0 .. $shape_ny - 1 ) { | ||
| 395 | my $char = $shape_grid->[ $y ][ $i - 1 ]; | ||
| 396 | # Update board | ||
| 397 | $board->[ $uly + $y ][ | ||
| 398 | $right | ||
| 399 | ? ( $ulx + $i ) | ||
| 400 | : ( $ulx + $i - 2 ) | ||
| 401 | ] | ||
| 402 | = $char | ||
| 403 | # Keep right/left pixels the same when not blank | ||
| 404 | unless $char eq ' ' | ||
| 405 | and ( $right && $i == $shape_nx or !$right && $i == 1 ); | ||
| 406 | } | ||
| 407 | } | ||
| 408 | else { | ||
| 409 | # Blank left/right col | ||
| 410 | $board->[ $uly + $_ ][ $right ? $ulx : ( $ulx + $shape_nx - 1 ) ] = | ||
| 411 | ' ' | ||
| 412 | for 0 .. $shape_ny - 1; | ||
| 413 | } | ||
| 414 | } | ||
| 415 | $right ? $shape->inc_ulx : $shape->dec_ulx; | ||
| 416 | |||
| 417 | 1; | ||
| 418 | } ## end sub horizontal | ||
| 419 | |||
| 420 | sub down { | ||
| 421 | my ( $self, $dont_inc_score ) = @_; | ||
| 422 | my $shape = $self->active_shape; | ||
| 423 | my $board = $self->board; | ||
| 424 | my ( $ulx, $uly ) = ( $shape->ulx, $shape->uly ); | ||
| 425 | my ( $shape_nx, $shape_ny ) = ( $shape->nx, $shape->ny ); | ||
| 426 | my $shape_grid = $shape->grid; | ||
| 427 | |||
| 428 | # Check if last shape row overlaps beneath current location or end of board | ||
| 429 | return if $uly + $shape->ny == $self->height; | ||
| 430 | return if grep { | ||
| 431 | my ( $y1, $x1 ) = ( $_->[ 0 ] + 1, $_->[ 1 ] ); | ||
| 432 | !$shape->covers( $y1, $x1 ) and $board->[ $y1 ][ $x1 ] ne ' ' | ||
| 433 | } $shape->covered_points; | ||
| 434 | |||
| 435 | for my $i ( 0 .. $shape_ny ) { | ||
| 436 | if ( $i ) { | ||
| 437 | my $shape_row = $shape_grid->[ $i - 1 ]; | ||
| 438 | for ( 0 .. $shape_nx - 1 ) { | ||
| 439 | my $char = $shape_row->[ $_ ]; | ||
| 440 | # Keep beneath pixels the same when not blank | ||
| 441 | my ( $y, $x ) = ( $uly + $i, $ulx + $_ ); | ||
| 442 | $board->[ $y ][ $x ] = $char | ||
| 443 | unless $board->[ $y ][ $x ] ne ' ' | ||
| 444 | and !$shape->covers( $y, $x ); | ||
| 445 | } | ||
| 446 | } | ||
| 447 | else { | ||
| 448 | # Blank first row | ||
| 449 | $board->[ $uly ][ $_ ] = ' ' for $ulx .. $ulx + $shape_nx - 1; | ||
| 450 | } | ||
| 451 | } | ||
| 452 | $shape->inc_uly; | ||
| 453 | $self->inc_score( 1 ) unless $dont_inc_score; | ||
| 454 | 1; | ||
| 455 | } ## end sub down | ||
| 456 | |||
| 457 | sub clear_screen { | ||
| 458 | my $console = shift; | ||
| 459 | $console->clrscr(); | ||
| 460 | } | ||
| 461 | |||
| 462 | { | ||
| 463 | # Output Stuff | ||
| 464 | |||
| 465 | my $console; | ||
| 466 | |||
| 467 | sub console_thread { | ||
| 468 | my $self = shift; | ||
| 469 | $console = Term::Screen::Uni->new() or die "couldn't make console"; | ||
| 470 | |||
| 471 | # Intro Screen | ||
| 472 | clear_screen( $console ); | ||
| 473 | $self->print_intro; | ||
| 474 | |||
| 475 | $semaphore->down; | ||
| 476 | clear_screen( $console ); | ||
| 477 | $self->print_board; | ||
| 478 | $semaphore->up; | ||
| 479 | while ( 1 ) { | ||
| 480 | lock( $print_cond ); | ||
| 481 | cond_wait( $print_cond ) until $print_cond; | ||
| 482 | last if $print_cond == -1; | ||
| 483 | $print_cond--; | ||
| 484 | $console->at( 0, 0 ); | ||
| 485 | $self->print_board; | ||
| 486 | } | ||
| 487 | # print "Exiting console_thread.\n"; | ||
| 488 | } | ||
| 489 | |||
| 490 | my $INSTRUCTIONS = <<EOI; | ||
| 491 | |||
| 492 | KEYBOARD CONFIGURATION: | ||
| 493 | |||
| 494 | LEFT - a / j | ||
| 495 | DOWN - s / k | ||
| 496 | RIGHT - d / l | ||
| 497 | ROTATE - w / i | ||
| 498 | DROP - space | ||
| 499 | |||
| 500 | QUIT - q | ||
| 501 | |||
| 502 | EOI | ||
| 503 | |||
| 504 | sub print_board { | ||
| 505 | my ( $self ) = @_; | ||
| 506 | my $board = $self->board; | ||
| 507 | my $level = $self->level; | ||
| 508 | my $width = $self->width; | ||
| 509 | printf "NEXT LEVEL: %2d\n", 10 - $level * 10 % 10; | ||
| 510 | print '+' . '-' x $width . "+\n"; | ||
| 511 | for my $y ( 0 .. $self->height - 1 ) { | ||
| 512 | print '|' . join( '', @{ $board->[ $y ] } ) . "|\n"; | ||
| 513 | } | ||
| 514 | print '+' . '-' x $width . "+\n"; | ||
| 515 | # print $INSTRUCTIONS; | ||
| 516 | my $col = $width + 4; | ||
| 517 | $console->at( 2, $col ); | ||
| 518 | printf "Level: %02d", $level; | ||
| 519 | $console->at( 4, $col ); | ||
| 520 | printf "Score: %06d", $self->score; | ||
| 521 | $console->at( 8, $col ); | ||
| 522 | print " Next:"; | ||
| 523 | my $queued_shape = $self->queued_shape; | ||
| 524 | my $grid_next = $queued_shape->grid; | ||
| 525 | my $row = 7; | ||
| 526 | $col = $width + 12; | ||
| 527 | |||
| 528 | if ( $queued_shape->ny <= 2 ) { | ||
| 529 | $console->at( $row, $col ); | ||
| 530 | print ' ' x 4; | ||
| 531 | $row++; | ||
| 532 | } | ||
| 533 | for ( 0 .. 3 ) { | ||
| 534 | my $grid_row = $grid_next->[ $_ ] || []; | ||
| 535 | $console->at( $row, $col ); | ||
| 536 | print join '', | ||
| 537 | map defined $grid_row->[ $_ ] ? $grid_row->[ $_ ] : ' ', | ||
| 538 | 0 .. 3; | ||
| 539 | $row++; | ||
| 540 | } | ||
| 541 | $console->at( $self->height + 4, 0 ); | ||
| 542 | } ## end sub print_board | ||
| 543 | |||
| 544 | sub print_intro { | ||
| 545 | my $self = shift; | ||
| 546 | my $msg = 'TETRIS'; | ||
| 547 | my ( $width, $height ) = ( $self->width, $self->height ); | ||
| 548 | print '+' . '-' x $width . "+\n"; | ||
| 549 | for my $y ( 0 .. $height - 1 ) { | ||
| 550 | if ( $y == int( $height / 2 ) ) { | ||
| 551 | my $l1 = ( $width - length $msg ) / 2; | ||
| 552 | print '|' | ||
| 553 | . ' ' x $l1 | ||
| 554 | . $msg | ||
| 555 | . ' ' x ( $width - $l1 - length( $msg ) + $width % 2 ) | ||
| 556 | . "|\n"; | ||
| 557 | } | ||
| 558 | else { | ||
| 559 | print "|" . ' ' x $width . "|\n"; | ||
| 560 | } | ||
| 561 | } | ||
| 562 | print '+' . '-' x $width . "+\n"; | ||
| 563 | print $INSTRUCTIONS; | ||
| 564 | } | ||
| 565 | } | ||
| 566 | |||
| 567 | sub quit { | ||
| 568 | lock( $GAME_OVER ); | ||
| 569 | $GAME_OVER = 1; | ||
| 570 | return; | ||
| 571 | } | ||
| 572 | |||
| 573 | { | ||
| 574 | my @shapes = ( | ||
| 575 | [ ' +', '++', '+ ' ], | ||
| 576 | [ '+ ', '++', ' +' ], | ||
| 577 | [ '++', '++' ], | ||
| 578 | [ '+' x 4 ], | ||
| 579 | [ ' + ', '+++' ], | ||
| 580 | [ '++', '+ ', '+ ' ], | ||
| 581 | [ '++', ' +', ' +' ], | ||
| 582 | ); | ||
| 583 | # @shapes = ( [ '+' x 4 ] ); This is cheating | ||
| 584 | |||
| 585 | sub random_shape { | ||
| 586 | Games::Tetris::Complete::Shape->new( | ||
| 587 | grid => $shapes[ int rand @shapes ] ); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | |||
| 591 | 1; | ||