#!/usr/bin/perl #This script is released into the public domain. #No warranty is provided. use Curses; use Dumpvalue; $version = 'PerlGDML - version 0.1.2 Alpha'; $dump = 1; $SIG{ALRM} = sub { npc(); mapdraw(); }; initscr; noecho; curs_set(0); getmaxyx($max_y,$max_x); start_color; assume_default_colors(COLOR_WHITE,COLOR_BLACK); init_pair(1,COLOR_YELLOW,COLOR_BLACK); init_pair(2,COLOR_WHITE,COLOR_RED); init_pair(3,COLOR_WHITE,COLOR_YELLOW); @potential = (A..Z); $power = 10; $powerlevel = 1; $maxhp = 10; $hp = $maxhp; $ijj = " "; $ikk = " "; $ill = " "; $gold = 0; $by = int rand ( $max_y-2 ) + 3; $bx = int rand ( $max_x ); #MAIN mapgen(); while ( $hp > 0 ) { alarm 1; input(); npc(); mapdraw(); } endwin; if ($dump) { print "\nPOTENTIAL\n"; $ref = \@potential; Dumpvalue->new->dumpValue( $ref ); print "\nACTIVE\n"; $ref = \@active; Dumpvalue->new->dumpValue( $ref ); } print "\n$version\n\nGAME OVER\nYour PowerLevel was $powerlevel and you collected $gold gold.\n\n\n"; sub mapgen { @active = ( { char => ' ', name => 'eraser', y => 0, x => 0 }, { char => '@', name => 'player', y => int ($max_y / 2), x => int ($max_x / 2) }, { char => '?', name => 'test', y => (int rand($max_y-2))+3, x => int rand($max_x) } ); @maparray = (); for ($ii = 0; $ii < ($max_y - 3) * $max_x; $ii++) { $nextm = int rand(4); if (!$nextm) { push @maparray, '#'; } else { push @maparray, ' '; } } for ($kk = 0;$kk < @potential;$kk++) { $nextc = int rand(2); if ($nextc) { push @active, { char => $potential[$kk], name => $potential[$kk], y => (int rand($max_y-2))+3, x => int rand($max_x) }; } } mapdraw(); } sub mapdraw { my $string = join(" ",@maparray); addstring(3,0,$string); attron(COLOR_PAIR(2)); for ($ll = 2;$ll < @active;$ll++) { addstring($active[$ll]{y},$active[$ll]{x},$active[$ll]{char}); } attroff(COLOR_PAIR(2)); attron(COLOR_PAIR(1)); addstring($active[1]{y},$active[1]{x},$active[1]{char}); attroff(COLOR_PAIR(1)); attron(COLOR_PAIR(3)); addstring($by,$bx,'$'); attroff(COLOR_PAIR(3)); $powerlevel = int $power/10; addstring(0,0,"PowerLevel:$powerlevel "); addstring(1,0,"HP:$hp / $maxhp ----- Gold:$gold "); } sub input { alarm 1; $key = getchar; alarm 0; if ($key eq 'w' or $key eq 'a' or $key eq 's' or $key eq 'd') {$power++;} if ($key eq 'p') { last; } elsif ($key eq 'w') { walk(1,'up'); } elsif ($key eq 'a') { walk(1,'left'); } elsif ($key eq 's') { walk(1,'down'); } elsif ($key eq 'd') { walk(1,'right'); } elsif ($key eq 'o') { mapgen(); } elsif ($key eq 'h') { menu(); } elsif ($key eq 'j') {} elsif ($key eq 'k') {} elsif ($key eq 'l') {} # if ($active[1]{y} == $by and $active[1]{x} == $bx) { $gold++; $by = int rand ( $max_y-2 ) + 3; $bx = int rand ( $max_x ); } } sub npc { for ($npcid = 2;$npcid < @active;$npcid++) { $nextnpc = int rand (5); if ($nextnpc == 1 or $nextnpc == 2) { #move in Y if ($active[1]{y} > $active[$npcid]{y}) { walk($npcid,'down'); } elsif ($active[1]{y} < $active[$npcid]{y}) { walk($npcid,'up'); } else { if ($nextnpc == 1) {walk($npcid,'down');} if ($nextnpc == 2) {walk($npcid,'up');} } } if ($nextnpc == 3 or $nextnpc == 4) { #move in X if ($active[1]{x} > $active[$npcid]{x}) { walk($npcid,'right'); } elsif ($active[1]{x} < $active[$npcid]{x}) { walk($npcid,'left'); } else { if ($nextnpc == 3) {walk($npcid,'right');} if ($nextnpc == 4) {walk($npcid,'left');} } } if ($nextnpc == 0) { $nextnpc = int rand (5); if ($nextnpc == 1) {walk($npcid,'down');} if ($nextnpc == 2) {walk($npcid,'up');} if ($nextnpc == 3) {walk($npcid,'right');} if ($nextnpc == 4) {walk($npcid,'left');} } if ($active[$npcid]{y} == $active[1]{y} and $active[$npcid]{x} == $active[1]{x}) { $hp--; } } } sub walk { if ($_[1] eq 'up') { if (($active[$_[0]]{y} > 3) and ((inch($active[$_[0]]{y}-1,$active[$_[0]]{x}) ne '#') or ! $_[0])) { addstring($active[$_[0]]{y},$active[$_[0]]{x},' '); $active[$_[0]]{y}--; } } elsif ($_[1] eq 'down') { if (($active[$_[0]]{y} < $max_y - 1) and ((inch($active[$_[0]]{y}+1,$active[$_[0]]{x}) ne '#') or ! $_[0])) { addstring($active[$_[0]]{y},$active[$_[0]]{x},' '); $active[$_[0]]{y}++; } } elsif ($_[1] eq 'left') { if (($active[$_[0]]{x} > 0) and ((inch($active[$_[0]]{y},$active[$_[0]]{x}-1) ne '#') or ! $_[0])) { addstring($active[$_[0]]{y},$active[$_[0]]{x},' '); $active[$_[0]]{x}--; } } elsif ($_[1] eq 'right') { if (($active[$_[0]]{x} < $max_x - 1) and ((inch($active[$_[0]]{y},$active[$_[0]]{x}+1) ne '#') or ! $_[0])) { addstring($active[$_[0]]{y},$active[$_[0]]{x},' '); $active[$_[0]]{x}++; } } } sub menu { alarm 0; $local_win = newwin($max_y/2,$max_x/2,$max_y/4,$max_x/4); $local_win->border('|', '|', '-', '-', '+', '+', '+', '+'); $local_win->addstring(2,6,$version); $local_win->refresh(); while(){ $key = getchar; if ($key eq 'h') { $local_win->delwin(); mapdraw(); last; } } }