Skip to content
Browse files

added c curses version by jab and perl version by david damerell

  • Loading branch information...
1 parent 6e73180 commit f161522f38eeb5b4a793c98375fef5b772aa3f67 @nrkn committed Feb 17, 2011
Showing with 148 additions and 0 deletions.
  1. 0 c/{ → conio}/README
  2. 0 c/{ → conio}/SimpleRL.c
  3. +3 −0 c/curses/README
  4. +33 −0 c/curses/SimpleRL.c
  5. +17 −0 perl/README
  6. +95 −0 perl/SimpleRL.pl
View
0 c/README → c/conio/README
File renamed without changes.
View
0 c/SimpleRL.c → c/conio/SimpleRL.c
File renamed without changes.
View
3 c/curses/README
@@ -0,0 +1,3 @@
+By jab
+
+http://groups.google.com/group/rec.games.roguelike.development/browse_thread/thread/48e8a057ad492219/6f06536dd5bee67f
View
33 c/curses/SimpleRL.c
@@ -0,0 +1,33 @@
+#include <curses.h>
+const char *map =
+ "#### ####\n"
+ "# #### #\n"
+ "# #\n"
+ "## ##\n"
+ " # # \n"
+ " # # \n"
+ "## ##\n"
+ "# #\n"
+ "# #### #\n"
+ "#### ####\n";
+int px = 1, py = 1;
+void move_to(int x, int y) { if (map[y * 11 + x] == ' ') { px = x; py = y; } }
+void simple_rl(void)
+{
+ int key;
+ initscr(); noecho(); curs_set(0); keypad(stdscr, 1);
+ mvaddstr(0, 0, map); mvaddch(py, px, '@');
+ while ((key = getch()) != 'q') {
+ mvaddch(py, px, ' ');
+ switch (key) {
+ case KEY_UP: move_to(px, py - 1); break;
+ case KEY_DOWN: move_to(px, py + 1); break;
+ case KEY_LEFT: move_to(px - 1, py); break;
+ case KEY_RIGHT: move_to(px + 1, py); break;
+ }
+ mvaddch(py, px, '@');
+ }
+ endwin();
+}
+
+int main(void) { simple_rl(); return 0; }
View
17 perl/README
@@ -0,0 +1,17 @@
+By David Damerell
+
+Copyright (C) 2011 David Damerell
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
View
95 perl/SimpleRL.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/perl -w
+# Copyright (C) 2011 David Damerell
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+use Curses;
+if (initscr()) {
+ if (($COLS<20) || ($LINES <20)) {
+ die "Please use at least a 20x20 display";
+ }
+} else {
+
+ die "Curses support not available?";
+}
+
+curs_set(0);cbreak;noecho;
+
+@maplines=("#### ####",
+ "# #### #",
+ "# #",
+ "## ##",
+ "# #",
+ "# #",
+ "## ##",
+ "# #",
+ "# #### #",
+ "#### ####");
+
+$xmax=0;
+foreach $line (0..$#maplines) {
+ @{$map[$line]}=split //,$maplines[$line];
+ $xmax=length($maplines[$line]) if length($maplines[$line]) > $xmax;
+}
+
+$ymax=$#maplines; $xmax--;
+
+$player{'x'}=$player{'y'}=2;
+%directions=('h',[ -1,0 ],'j',[ 0,1 ],'k',[ 0,-1 ],'l',[ 1,0 ],
+ 'y',[ -1,-1 ],'u',[ 1,-1 ],'b',[ -1,1 ],'n',[ 1,1 ]);
+
+&redraw_map;
+
+while ($c = getch) {
+ if (defined($directions{$c})) {
+ $oldx=$player{'x'}; $oldy=$player{'y'};
+ $newx=$player{'x'}+$directions{$c}[0];
+ $newy=$player{'y'}+$directions{$c}[1];
+ if ($map[$newy][$newx] eq ' ') {
+ $oldx=$player{'x'}; $oldy=$player{'y'};
+ $player{'x'}=$newx; $player{'y'}=$newy;
+ &redraw ($oldx, $oldy); &redraw ($newx,$newy); refresh;
+ } else {
+ # you bumped into a wall
+ }
+ } elsif ($c eq 'r') {
+ &redraw_map;
+ } elsif ($c eq 'q') {
+ endwin; exit;
+ } else {
+ # complain, unknown keypress
+ }
+
+}
+
+sub redraw_map {
+ foreach $x (0..$xmax) {
+ foreach $y (0..$ymax) {
+ &redraw ($x,$y);
+ }
+ }
+ refresh;
+
+}
+
+sub redraw {
+ my $x=shift; my $y=shift;
+ if ($x == $player{'x'} and $y ==$player{'y'}) {
+ attrset (A_BOLD | A_REVERSE);
+ addch ($y, $x, '@');
+ attrset (A_NORMAL);
+ } else {
+ addch ($y, $x, $map[$y][$x]);
+ }
+}

0 comments on commit f161522

Please sign in to comment.
Something went wrong with that request. Please try again.