Simple moving characters

ive done a bunch on my little program so far. ive also had some help from another youtuber who watched my video on it. but right now im looking to have a grashical design rather than the words. id like to have it show up with just a little [] as the character and you move him around using wasd like in my program. the you went crazy part can be taken out btw. also i need it to work on ubuntu 13.04 thanks :D. i forgot to mention, i have done alot of research on these things and they all only seem to work on windows. even when i would copy and paste a code someone made right in. a lot of people use conio.h and that doesnt work.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
#include <termios.h>
#include <unistd.h>
int getch(void)
{
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = cin.get();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
int main()
{
cout << string(50, '\n');
begin:
char letter; //char sets it to  letter. the letter is defined as 'l'. if using an integer do int instead of char.
cout<<"wasd: ";
letter=getch(); //instead of cin letter
  {
    {
    if(letter=='w') {
    cout << string(50, '\n');
    cout<<"You went forward\n";
    goto begin;
    }
    if(letter=='a') {
    cout << string(50, '\n');
    cout<<"You went left\n";
    goto begin;
    }
    if(letter=='s') {
    cout << string(50, '\n');
    cout<<"You went back\n";
    goto begin;
    }
    if(letter=='d') {
    cout << string(50, '\n');
    cout<<"You went right\n";
    goto begin;
    }
    else {
    cout << string(50, '\n');
    cout<<"You went crazy!\n";
    return 0;
    }
}
}
}
Last edited on
If you're looking for Linux-specific answers, I suggest you move this thread to the UNIX/Linux programming subforum.
ok sounds good. ill do so thanks :)
I've reformatted your code so I could see it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
#include <termios.h>
#include <unistd.h>

int getch(void)
{
	struct termios oldt,
	newt;
	int ch;

	tcgetattr( STDIN_FILENO, &oldt );

	newt = oldt;
	newt.c_lflag &= ~( ICANON | ECHO );
	tcsetattr( STDIN_FILENO, TCSANOW, &newt );

	ch = cin.get();
	tcsetattr( STDIN_FILENO, TCSANOW, &oldt );

	return ch;
}

int main()
{
	cout << string(50, '\n');
begin:
	char letter; //char sets it to  letter. the letter is defined as 'l'. if using an integer do int instead of char.
	cout<<"wasd: ";
	letter=getch(); //instead of cin letter
	{
		{
			if(letter=='w') {
				cout << string(50, '\n');
				cout<<"You went forward\n";
				goto begin;
			}
			if(letter=='a') {
				cout << string(50, '\n');
				cout<<"You went left\n";
				goto begin;
			}
			if(letter=='s') {
				cout << string(50, '\n');
				cout<<"You went back\n";
				goto begin;
			}
			if(letter=='d') {
				cout << string(50, '\n');
				cout<<"You went right\n";
				goto begin;
			}
			else {
				cout << string(50, '\n');
				cout<<"You went crazy!\n";
				return 0;
			}
		}
	}
}


I ran it, and it works. But I suggest you look at ncurses. That's a terminal based system that will allow you to write anywhere on the screen.

For example, there's an ncurses snake game here:
https://code.google.com/p/nsnake/

For example, main() should look familiar to you.
https://code.google.com/p/nsnake/source/browse/src/main.c
Last edited on
thanks kb2. this topic is kinda old now and ive found my own way around it which is very nooby but it works for me. i have looked at ncurses a little it because i needed the beep noise for a little timer i was making. i however dont know much else about it and i assume i probably should look into it more based on what u said. i still have a lot to learn :)
i have realized the only way to do stuff like i asked is with ncurses. or at least ncurses is the best way to do this. anyone who reads this, look up ncurses. you can move the cursor around using move(y,x) and to move it relative to where the cursor is currently at use something like move(y-1,x). hope this helps
Topic archived. No new replies allowed.