Learning Curses by Example

Saturday, October 4th, 2008 | Resources

David Brady, this is for you. And for me.

When you install PDCurses is comes with some demo programs, which to me is better than a tutorial. Somehow I got a hold of some other demo programs that I thought came with PDCurses but apparently didn’t (the fact that they all #included ncurses should have been a clue) and now I don’t know where they came from. Specifically I found ballgame.c to be most instructive when writing shox (which I’ve re-written with more useful comments). Rain.c is also informative and fun to look at too. The rest were less informative, but some were kind of fun like firework.c, shuffle.c, tt.c (typing tutor), worm.c (not the game), and xmax.c. If anyone can tell me where these came from I’d be grateful.

When I write my curses book I’m going to reference Norman Matloff’s online book about Curses, but mostly because it’s so square.

So until I make a curses tutorial these examples will have to do.

Share and Enjoy:
  • StumbleUpon
  • Google
  • del.icio.us
  • Facebook
  • Digg
  • Slashdot
  • Fark
  • Live
  • Spurl
  • TwitThis
  • E-mail this story to a friend!
  • Reddit

Tags:

4 Comments to Learning Curses by Example

Ido Yehieli
October 11, 2008

Here’s another example for you, this is pretty much all the curses knowledge you need to write a roguelike: http://pastebin.com/f76161be3

I guess you can make it even smaller by replacing the map with a really simple random generator :)

Joe
October 22, 2008

So after looking at that miniroguelike I just had to mess with it. I added a random map maker (extremely simple and that sometimes makes mazes where the ending is unattainable) and a winning condition in getting to the far right side. And know what/ This little toy became a game. Know what else? The modified version is actually almost 200k shorter and smaller than the original, tho perhaps a little less comprehensible. better commenting would help, I’ll bet.
#include <curses.h>
#include <time.h>
#include <stdlib.h>
main() {
  char map[10][15];
  keypad(initscr(),1);
  int y=1;
  int x=1;
  int c;
  srand(time(NULL));
  for(int yy=0;yy&lt10;yy++)
  for(int xx=0;xx&lt15;xx++)
    map[yy][xx]=((yy!=0)&&(yy!=9)&&(xx!=0)&&(xx!=14)&&(rand()%4))?' ':'*';
  map[rand()%8+1][14] = ' ';
  while((x!=14) && ('q'!=(c=getch()))){
  for(int yy=0;yy&lt10;yy++)
    for(int xx=0;xx&lt15;xx++)
      mvaddch(yy,xx,map[yy][xx]);
    if(KEY_UP==c && ' '==map[y-1][x]) y--;
    if(KEY_DOWN==c && ' '==map[y+1][x]) y++;
    if(KEY_LEFT==c && ' '==map[y][x-1]) x--;
    if(KEY_RIGHT==c && ' '==map[y][x+1]) x++;
    mvaddch(y,x,'@');
  }
}

Ido Yehieli
October 23, 2008

Maybe you should run it through some processor that will allow you to post lesser-than/greater-than signs and non-breaking spaces.

Joe
October 23, 2008

Hu? Oh, dang it. I keep forgetting about dang blasted HTML symbols. Man and fixing it is a pain in the patooti. But that’s alright. I need to do that with my tutorial examples too.

Hey, this is a groovy tool that automatically HTMLizes the code, save for the non-breaking spaces, but those are easy enough to add. I’ve used it to fix the source code examples in the getting started section. Better bookmark that.

Leave a comment

Support Cymon's Games

Search

Sponsored Links

Cymon's Amazon Store