Revisions
-
tene revised this gist
Jul 17, 2011 . 1 changed file with 36 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,28 +1,48 @@ #!/usr/bin/perl -w # Use Moose for the Player class, and other slight improvements. package Player; use v5.10; use strict; use Moose; has 'health' => (is => 'rw', isa => 'Int', default => 100); has 'x' => (is => 'rw', isa => 'Int'); has 'y' => (is => 'rw', isa => 'Int'); sub walk { my ($self, $ch) = @_; my %map = ( h => [0,-1], j => [1,0], k => [-1,0], l => [0,1], ); my ($y, $x) = @{$map{$ch}}; $self->position($self->y + $y, $self->x + $x); } sub position { my ($self, $y, $x) = @_; if (defined $y and defined $x) { $self->y($y); $self->x($x); } return ($self->y, $self->x); } package main; use v5.10; use strict; use Curses; initscr; my $p = Player->new(x=>5, y=>10); while(1) { addch($p->position,"@"); move($p->position); refresh; $p->walk(getch); clear; } -
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ #!/usr/bin/perl -w use strict; use Curses; my %player = ( 'health' => 100, 'x' => 5, 'y' => 5, ); sub walking { my $_ = getch; if (/h/) { $player{x}--; }; if (/j/) { $player{y}++; }; if (/k/) { $player{y}--; }; if (/l/) { $player{x}++; }; } initscr; while(1) { addch($player{y},$player{x},"@"); move($player{y},$player{x}); refresh; &walking; clear; } endwin;