Thread: move()

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    move()

    i have been up all night tring to creat something and finally i have created a box.. but i was wondering if you can help me out on how to make it on KEYs like for example if i was to type in the arrow key up
    then i want the box to move up... i would like to know if there is anyone that can help me. ? thanx

    Code:
    int main()
    {
      WINDOW *my_win;
      int ch, x, highlight = 1;
      int lines = 10, cols = 40;
      int height = 10 / 2, width = 10 / 2;
      initscr();
      cbreak();
      noecho();
      keypad(my_win, TRUE);
    
      my_win = newwin(lines , cols, height, width);
      box(my_win, 0,0);
    
       ch = wgetch(my_win);
                    if ( ch == KEY_LEFT)
                     mvprintw(0,0,"moving",my_win);
                     wmove(my_win, 0,0);
    }
        wrefresh(my_win);
        sleep(4);
        endwin();
        return 0;
      }
    thanx

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Hi xlordt,

    I don't know what operating system and dev environment you are using but I guess it is Linux ?!? Well, I tried you run your program on my Lunix system and it doesn't work. Why? I think it's because you try to move a window with the wmove (move cursor) function in stead of the mvwin function. I'm not completely sure about this because I can't find the manual pages on my PC about this topic. Here is the code of my version:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <curses.h>
    
    int main(int argc, char *argv[])
    {
    	int ch = 0;
    	int x = 10; 
    	int y = 10;
    	int xlen = 40;
    	int ylen = 10;
    	char msg[1024];
    	WINDOW *win = NULL;
    
    	initscr();
    	if(cbreak() == ERR || noecho() == ERR || nonl() == ERR)
    	{
    		printf("Error initializing window\n");
    		endwin();
    		return -1;
    	}
    
    	if((win = newwin(ylen, xlen, y, x)) == NULL)
    	{
    		printf("Error creating window\n");
    		endwin();
    		return -1;
    	}
    
    	curs_set(0); /* make cursor invisible */
    
    	box(win, 0, 0);
    
    	wattron(win, A_BLINK);
    	mvwprintw(win, 2, 2, "Press arrow keys to move window");
    	wattroff(win, A_BLINK);
    
    	wattron(win, A_REVERSE);
    	mvwprintw(win, ylen-1, 1, " Press 'q' to quit ");
    	wattroff(win, A_REVERSE);
    
    	keypad(win, TRUE);
    
    	while((ch = wgetch(win)) != 'q')
    	{
    		int tmpx = x;
    		int tmpy = y;
    
    		/* fill msg with len-2 spaces */
    		sprintf(msg, "%*s", xlen - 2, "");
    
    		/* clear previous line (You pressed...) */
    		mvwprintw(win, 4, 1, msg);
    
    		switch(ch)
    		{
    			case KEY_LEFT: 	tmpx--;
    					break;
    
    			case KEY_RIGHT: tmpx++;
    					break;
    
    			case KEY_UP: 	tmpy--;
    					break;
    
    			case KEY_DOWN: 	tmpy++;
    					break;
    
    			default: 	/* this can go wrong if key-stroke */
    					/* contains new-line char (e.q. F2?) */
    					sprintf(msg, "You pressed: [%c]", ch);
    					mvwprintw(win, 4, 2, msg);
    					break;
    		}
    		clear();
    		if(mvwin(win, tmpy, tmpx) == 0)
    		{
    			/* move was ok, change x and y position */
    			x = tmpx;
    			y = tmpy;
    		}
    		else
    		{
    			/* window out of screen range, display old values */
    			mvwin(win, y, x);
    		}
    		refresh();
    	}
    
    	clear();
    	refresh();
    	endwin();
    	return 0;
    }
    Maybe it's usefull for you, maybe not. But I can tell you it was a good practice for me.

    Cheers,
    Monster

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    thanx it is also a good pracetice for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe Comp Move help
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2008, 11:05 AM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM