Skip to content

Instantly share code, notes, and snippets.

@tene
Forked from djanatyn/curses.pl
Created July 17, 2011 00:08
Show Gist options
  • Save tene/1086959 to your computer and use it in GitHub Desktop.
Save tene/1086959 to your computer and use it in GitHub Desktop.
roguelike
#!/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;
}
endwin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment