Devel-PerlySense

 view release on metacpan or  search on metacpan

t/data/project-lib/Game/UI.pm  view on Meta::CPAN

Default: 0

=cut

use Class::MethodMaker get_set => [ "soundsEnabled" ];





=head1 METHODS

=head2 new()

Create new UI.

=cut
sub new { my $pkg = shift;

    my $self = {};
    bless $self, $pkg;

    $self->offsetLeft(0);
    $self->offsetTop(0);
    $self->oLocationScore(undef);
    $self->oConsole(Win32::Console->new(STD_OUTPUT_HANDLE));
    $self->oKeyboard(Win32::Console->new(STD_INPUT_HANDLE));
    $self->soundsEnabled(0);

    return($self);
}





=head2 displayLawn($oLawn)

Initialize the screen and display stuff on the $oLawn

Return 1 on success, else 0.

=cut
sub displayLawn { my $self = shift;
    my ($oLawn) = @_;

    $self->oConsole->Cls();
    for my $row (0 .. ($oLawn->height - 1)) {
        $self->oConsole->FillChar('.', $oLawn->width, 0 + $self->offsetLeft, $row + $self->offsetTop);
        }

    $self->showScore(undef);

    return(1);
}





=head2 runMainMenu($oApplication)

Show the menu and accept user input. Run whatever user
action is selected.

Return 1 on success, else 0.

=cut
sub runMainMenu { my $self = shift;
    my ($oApplication) = @_;

    my %hAction = (
        "P"        => sub {
            $oApplication->runGame();
            return(1);
            },
        "Q"        => sub {
            $self->oConsole->Cls();
            return(0);
            },
        );

    $self->showMainMenu() or return(0);

    while(1) {
        if(my $rcAction = $hAction{ uc($self->getKeyPressChar()) }) {
            $rcAction->() or return(1);
            $self->showMainMenu() or return(0);
            }

        sleep(0.05);
        }

    return(1);
}





=head2 showMainMenu()

Show the menu on the screen.

Return 1 on success, else 0.

=cut
sub showMainMenu { my $self = shift;

    $self->oConsole->Cls();

    my $text =  qq{

             .: MENU :.                       .: HOW TO PLAY :.

             P - Play                         - F - Turn LEFT
             Q - Quit                         - J - Turn RIGHT

                                              - You are the white worm


               Please enlarge the console window before playing
};

    print $text;

    return(1);
}





=head2 displayObjectAt($oObject)

Display the $oObject at it's Locaton.

Return 1 on success, else 0.

=cut
sub displayObjectAt { my $self = shift;
    my ($oObject) = @_;

    my $i = 0;
    for my $oLocation (@{$oObject->raBodyLocation}) {
        my $char = $oObject->raBodyChar->[$i];        #Refactor: Move to worm
        $self->displayObjectBodyPartAt($oLocation, $char, $oObject);
        $i++;
        }

    return(1);
}






=head2 displayObjectBodyPartAt($oLocation, [$char = '.'], [$oObject])

Display the $char at the $oLocation. If $oObject is passed, 
the style may be influenced by it.

Return 1 on success, else 0.

=cut
my %hColorAttr = (
    "white" => $::FG_WHITE,



( run in 0.653 second using v1.01-cache-2.11-cpan-d8267643d1d )