LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-10-2003, 08:21 AM   #1
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Rep: Reputation: 30
Trouble moving characters with C in a simple CLI program


Hey all,
I'm trying to get a hang of programming with C in CLI.And the problem I'm working on is moving a character across the screen like : You press "a" and "@" moves up a line.And the "@" 's previous position is clear.I could probably do it by clearing the screen everytime the character is moved.....but its very hard on the eyes.If somebody could post a snippet of code showing how to do this...or give me a good link to a page explaining how then I would greatly appreciate it.

Thanks in advance,

WindowsBurner
 
Old 12-10-2003, 08:49 AM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
You won't be able to do this using just the C standard library. I think your best bet would be to use the ncurses library.

Alex
 
Old 12-10-2003, 12:40 PM   #3
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
Thanks ... I'll try learning ncurses and see if it works
 
Old 12-11-2003, 02:39 AM   #4
nibbler
Member
 
Registered: Oct 2003
Location: Croatia
Distribution: Fedora 4
Posts: 46

Rep: Reputation: 15
try this is you need help, don't forget to compile with -lcurses.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>

int main()
{

	if ((initscr())==NULL)
	{
		perror("initsrc()");
		return(EXIT_FAILURE);		
	}	
	noecho();
	int maxx,maxy,curx,cury;
	char ch;
	getmaxyx(stdscr,maxy,maxx);
	curx=maxx/2;
	cury=maxy/2;
	mvaddch(cury,curx,'@'); 
	do
	{
		ch=getch();
		mvaddch(cury,curx,' ');
		if (ch=='a' && curx>1) curx--;
		if (ch=='s' && cury<maxy-2) cury++;
		if (ch=='w' && cury>1) cury--;
		if (ch=='d' && curx<maxx-2) curx++;
		mvaddch(cury,curx,'@');
		refresh();
	}while (ch!=27); // ESC to exit
	endwin();		
	return(EXIT_SUCCESS);
}
 
Old 12-11-2003, 08:00 AM   #5
WindowsBurner
Member
 
Registered: Nov 2003
Location: In chaos
Distribution: OpenSuse
Posts: 293

Original Poster
Rep: Reputation: 30
Thanks nibbler ! I was working on writing my own engine in ncurses...but I never actually learned it.... I just glanced at it a bit... not only will this help me with my rpg and text editor but looking through your code is helping me to understand C even better.

Thanks again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to tag photo's in cli shoekstr Linux - Software 2 09-02-2005 06:03 AM
simple CLI tool for agregated total received on eth0 (simpler than bandwidth) Emmanuel_uk Linux - Newbie 3 08-17-2005 03:29 PM
music streaming program for cli linuxhippy Linux - Software 2 11-11-2004 11:36 AM
simple CLI question about size of HD? Lleb_KCir Linux - General 4 06-10-2004 09:00 PM
Simple Newb Question: How do I check what version of Redhat is Installed from the CLI praefex Linux - General 1 01-27-2004 07:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration