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

This is a discussion on Quick help on bool or while? Sample code... within the C Programming forums, part of the General Programming Boards category; Ahh driving me crazy, I haven't done any coding in awhile, used to code for muds back in the day ...

  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!
    Attached Files Attached Files
    Last edited by Striph; 01-10-2006 at 12:55 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    void main(void)
    Use int main().

    You can use a loop instead of that goto.

    Code:
    cprintf("");
    That's not a printable character -- use octal notation (like \123) instead, or putchar:
    Code:
    putchar(48);
    putchar(0xff);
    putchar(011);
    Code:
    _setcursortype(_NOCURSOR);
    Are you using DJGPP?

    And tolower()/toupper() (in <ctype.h>) can save you from case 'A': case 'a':.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Quote Originally Posted by dwks
    Code:
    void main(void)
    Use int main().
    Ok, will do.


    Code:
    _setcursortype(_NOCURSOR);
    Are you using DJGPP?
    Not using DJPP, using Turbo C++ 3 for Dos. Normally the cursor draws the smiley and positions the blinking cursor next to it, i set it of so it doesn't look like that.

    I changed it around to where I have a for loop printing a number to infinite then go to the move function, which has another print function so I see its getting that far, and its drawing the smiley, but its not processing any input after that so you can't move.

    Code:
    #include <stdlib.h>
    #include <conio.h>
    
    void main_move(void);
    
    int choose, x, xa, y, ya, z, b, a;
    
    int main(void)
    {
    	x = 40;
    	y = 13;
    	z = 0;
    	b = 1;
    	a = 0;
    	xa = x;
    	ya = y;
    
    	clrscr();
    	while(z!=1) {
    	   gotoxy(1,1);
    	   printf("%d",b);
    	   b++;
    	main_move(); }
    
    	clrscr();
    	exit(0);
    
    }
    
    
    void main_move(void)
    {
    	gotoxy(5,5);
    	printf("%d",a);
    	a++;
    	gotoxy(xa,ya);
    	textcolor(WHITE);
    	textbackground(BLACK);
    	cprintf(" ");
    	gotoxy(x,y);
    	textcolor(WHITE);
    	textbackground(BLACK);
    	cprintf("");
    	_setcursortype(_NOCURSOR);
    	choose = getc();
    	switch (choose)
    		{
    		   case 'q':
    		   case 'Q':
    		   case 27 : _setcursortype(_NORMALCURSOR); clrscr(); z = 1;
    		   case 'w':
    		   case 'W':
    		   case 72 : if(y>2) {--y; ya= y+1;xa=x;} else {y=2; cprintf("\a");} break;
    		   case 's':
    		   case 'S':
    		   case 80 : if(y<24){++y; ya= y-1;xa=x;} else {y=24; cprintf("\a");} break;
    		   case 'a':
    		   case 'A':
    		   case 75 : if(x>1) {--x; xa= x+1;ya=y;} else {x=1; cprintf("\a");} break;
    		   case 'd':
    		   case 'D':
    		   case 77 : if(x<80){++x; xa= x-1;ya=y;} else {x=80;cprintf("\a");} break;
    		}
    
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    I got it figured out, the input on the movement if I had it set to getch would prevent the for loop from processing. So I put a if(kbhit()) { before getch and switch statement, works flawlessly now.

    Thanks for the ideas.

    Now on to getting it to run slowly instead of an infinite speed! lol
    Last edited by Striph; 01-10-2006 at 02:49 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Now on to getting it to run slowly instead of an infinite speed! lol
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Thanks again Dwks!

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, 03:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21