Moving an ASCII character left or right on the console when a or d are pressed

I have been trying to find a solution but nothing works. My code is very simple. Using getch() in a loop, when 'a' is pressed move "*****" left. When d is pressed, move right instead. when f is pressed, its game over. With my current code the ***** bar moves left no matter what key is pressed, even other than a, d or f. If someone can explain why it does that and how to correct it pls, i would really appreciate.

Another question: is there a way to make this loop play 30 times per second without pausing the action? What i want is to create a very simple game in ASCII characters where everything in the loop will be done 30 times per second to kinda simulate a frame rate.v

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
#include<iostream>
#include<conio.h>
#include<iomanip>
#include"e:\\C++\\cvm.h"
using namespace std;


void main(void)
{
	char DirectionBarre;
	int BarreX;
	bool GameOver;

	BarreX = 50;
	
	gotoxy(BarreX, 29);
	cout << "*****";
	
	GameOver = false;

		while (!GameOver)
		{
			DirectionBarre = _getch();
			
			if (DirectionBarre=='a')
			{
				gotoxy(BarreX, 29);
				cout << "     "; //used to erase the ***** bar
				BarreX--;
				gotoxy(BarreX, 29);
				cout << "*****";
			}
			else if (DirectionBarre == 'd')
			{
				gotoxy(BarreX, 29);
				cout << "     "; //used to erase the ***** bar
				BarreX++;
				gotoxy(BarreX, 29);
				cout << "*****";
			}
			else if (DirectionBarre == 'f')
			{
				GameOver = true;
			}
		}
		gotoxy(40, 14);
		cout << "GAME OVER";

}

Last edited on
forget it
Last edited on
Tks for all the tips! Sry, the mistake wasnt the ==. This is a quick retype of what i did at school and back there the == where properly done. It still moves the ***** bar only to the left no matter what key i press. I suspect even if i use getch without echo it still create something on the console (an empty space?) and thats whats moving the bar left. Would explain why it moves when i press other keys than a or d.

Sleep pause the action, this is not what i want to do. What i want to do is take the time at a certain point (in ms) and do the loop only when Time+=33.3334ms. I would probably use a double instead of a float so i can have as many .333333333 decimals before rounding to 4 to avoid frame skipping. My problem is the only way i know to take the time returns it in seconds instead of ms.

Last edited on
Here I did some edits do demonstrate how what you want can be done. The program is SUPPOSED to move the characters around every 250milliseconds (default is left) and if it detects that the user had pressed a key, it will change the direction to corresponding direction. If user presses f it breaks (Note that because of how the if statements are framed, pressing on any other key will pause the characters from moving).

However I do not have a compiler that is compatible with the edits I made myself so I can't be sure that it works.

Anyways try it out and let me know.

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
#include<iostream>
#include<conio.h>
#include<iomanip>
#include<dos.h> // or windows.h, depends on your compiler
#include"e:\\C++\\cvm.h"
using namespace std;


int MAX_X = 90;
int MIN_X = 1;

void printBarrel(const int &x) {
	gotoxy(BarreX, 29);
	cout << "*****";
}


void main(void)
{
	char Direction = 'a';
	int BarrelX;
	bool GameOver;

	BarreX = 50;
	
	GameOver = false;
	
	while (!GameOver) {
		if(kbhit()) // if something in buffer
			Direction = getch(); // get char
		while(kbhit()); // clear buffer
	
		sleep(1000/4); // frame rate: 4 per second.
		
		if(Direction == 'a')
			BarrelX--;
		else if(Direction == 'd')
			BarrelX++;
		else if(Direction == 'f')
			break;
			
		clrscr(); // change to system("cls") not supported in compiler
		printBarrel(BarrelX);
		
	}
	

	gotoxy(40, 14);
	cout << "GAME OVER";

}


By the way, you should really get a modern compiler. If you are keen on wanting to make small console games like this using conio.h then you can download Miscrosoft Visual Studio. They have <conio.h> and all the functions that come with it (note that you need to use _ before the identifiers because these are non standard functions so getch() would be _getch()).
By the way, if your compiler has dos.h instead of windows.h you should be careful about sleep(). Sleep() on my school' compiler took argument in seconds whereas there was another function called delay() that took argument in milliseconds.

So use delay() if that's the one taking argument in milliseconds.
sleep and Sleep are different as well, or were ...
Last edited on
Topic archived. No new replies allowed.