Thread: Quick help on bool or while? Sample code...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Quick help on bool or while? Sample code...

    Ahh driving me crazy, I haven't done any coding in awhile, used to code for muds back in the day so I kind of know what I need here, like a looping function or a while statement thing. Basically I want it to do things while it does another thing. This sample program is a quick thing I threw together that moves a symbol around the screen by using wasd or cursor keys. I would like it to do something like say print a number in the corner and increment by 1 every second even if I'm not doing anything. Kind of like how muds do things behind the scenes while you sit still. I can't for the life of me think of how to do that, or maybe I'm taking the wrong approach.

    Heres what I have so far:

    Code:
    #include <stdlib.h>
    #include <conio.h>
    
    void main(void);
    void main_move(void);
    
    int choose, x, xa, y, ya;
    
    enum
    {
      KEY_ESC     = 27,
      ARROW_UP    = 256 + 72,
      ARROW_DOWN  = 256 + 80,
      ARROW_LEFT  = 256 + 75,
      ARROW_RIGHT = 256 + 77,
    
    };
    
    static int get_code(void)
    {
      int ch = getch();
    
      if ( ch == 0 || ch == 224 )
        ch = 256 + getch();
    
      return ch;
    }
    
    
    void main(void)
    {
    	x = 40;
    	y = 13;
    	xa = x;
    	ya = y;
    
    	clrscr();
    	main_move();
    	clrscr();
    	exit(0);
    
    }
    
    
    void main_move(void)
    {
    	clrscr();
    	main_no:
    	gotoxy(xa,ya);
    	textcolor(WHITE);
    	textbackground(BLACK);
    	cprintf(" ");
    	gotoxy(x,y);
    	textcolor(WHITE);
    	textbackground(BLACK);
    	cprintf("");
    	_setcursortype(_NOCURSOR);
    	choose = get_code();
    	switch (choose)
    		{
    		   case 'q':
    		   case 'Q':
    		   case 27 : _setcursortype(_NORMALCURSOR); clrscr(); return;
    		   case 'w':
    		   case 'W':
    		   case ARROW_UP : if(y>2) {--y; ya= y+1;xa=x;} else {y=2; cprintf("\a");}	goto main_no;
    		   case 's':
    		   case 'S':
    		   case ARROW_DOWN : if(y<24){++y; ya= y-1;xa=x;} else {y=24; cprintf("\a");}	goto main_no;
    		   case 'a':
    		   case 'A':
    		   case ARROW_LEFT : if(x>1) {--x; xa= x+1;ya=y;} else {x=1; cprintf("\a");}	goto main_no;
    		   case 'd':
    		   case 'D':
    		   case ARROW_RIGHT : if(x<80){++x; xa= x-1;ya=y;} else {x=80;cprintf("\a");}	goto main_no;
    		   default : goto main_no;
    		}
    
    }
    Its quick and dirty, prints a smiley to the middle of the screen, it moves with cursors or wasd keys, and if you reach the boundry of the screen it prevents you from moving past. This is a stripped down version of the full setup I have, and need a way to regenerate a players hps/mana while they stand still and not require them to move, I have the regen working when they move now but I need the "real" version so to speak. Also i'm sure you'll laugh at this but i'm using Borlands Turbo C++ 3.0 for Dos to do all of this, I don't have a copy of Visual Studio yet, but thats on my wish list for the future.

    Thanks for any ideas or help!
    Last edited by Striph; 01-10-2006 at 01:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get sample shell code to work...
    By jcafaro10 in forum C Programming
    Replies: 6
    Last Post: 07-07-2010, 08:04 AM
  2. Why doesn't this example work for me?
    By xixpsychoxix in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 08:25 PM
  3. looking for book with sample code
    By stella in forum C++ Programming
    Replies: 5
    Last Post: 06-25-2003, 10:48 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM