Simple Game

I started writing this classic snake game today that runs on the console.
It's principle is same with the classic snake. (ie eat a fruit and the snake grows bigger)
I was still working on the movement and control when I hit a brick wall.
The keys (w,a,s,d) are used for the controls and I'm using the getche() function to get the input and I implemented a timer using <time.h> but the problem is.. even when the timer is up, the console still waits for an input. Is ther e a way to fix this please?

Here is my entire source code.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#define MATX 17
#define MATY 6 //matx and maty represent the game board.
#include <iostream>
#include <conio.h>
#include <time.h> 
using namespace std;


struct XY	{
	int x=0;
	int y=0;
};
void initialize(char mat[][MATX])
{
	for(int i=0;i<MATY;i++)	
		for(int j=0;j<MATX;j++)
			mat[i][j]=NULL;
}

void move(char mat[][MATX],XY &p)
{
	initialize(mat);
	mat[p.y][p.x]='p';
}
void display(char mat[][MATX],XY &p)
{
	system("cls");
	move(mat,p);
	for(int i=0;i<MATY;i++)	{
		for(int j=0;j<MATX;j++)
			cout<<mat[i][j];
		cout<<endl;
	}
}


int main()
{
	XY p;
	char mat[MATY][MATX];
	 
    
	
	system("color 5f");  //for windows OS
	initialize(mat);
	mat[0][0]='P';
	
	
	char keypress;
	display(mat,p);
	keypress=getch();
	while(true)
	{		
		switch(keypress)
		{
			case 'a':p.x--;
					 if(p.x<0)
					 	p.x=MATX-1; break;
			case 's':p.y=(p.y+1)%MATY; break;
			case 'd':p.x=(p.x+1)%MATX;  break;
			case 'w':p.y--; 
					 if(p.y<0)
					 	p.y=MATY-1; break;
		}
		
		display(mat,p);
		cout<<p.x<<" "<<p.y<<endl;
		clock_t ttt=clock()+1000;
		if(ttt<clock() || (keypress = getche() ) != 'b')//this is the timer but I want it to stop expecting input after the timer is up
			asm(
                                "NOP"
                        );
	}
		
		

	return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>
 
char getche_nowait() // 'a' -> 'z' characters only
{
      for(int i = 'a'; i <= 'z'; ++i)
      if(GetAsyncKeyState(VK_A + (i - 'a')))
      {
            std::cout << (char)i; 
            Sleep (75);
            return (char)i;
      }
      Sleep(1); // Edited
      return 0; // Character input not supported or no input
}


if(ttt< clock() || ((keypress = getche_nowait()) ? keypress : 'b') != 'b') // this is the timer but I want it to stop expecting after the timer is up
Last edited on
@Half Life G Man

Thanks for the reply. But, I'm still having a problem with the function:
error: 'VK_A' was not declared in this scope

Do I need to include any other library?
And also, I'm trying to stay away from platform specific programming. Do you know of a way to do it with a cross platform library?
If your complier recognizes GetAsyncKeyState() but doesn't know what VK_A is, use this :

#define VK_A 0x41

Or replace (VK_A) with 0x41 :

if(GetAsyncKeyState(0x41 + (i - 'a')))

Maybe it is possible, but I can't imagine a way to do it with cross platform library. It is very hard to find a standard input function that is specialized in catching the human input the instant moment when the function is called, without having to pause the program to wait for a proper human input. You should do some research and figure out how the same program feature is done in different platforms (Linux, Mac, etc) using different methods. And if you can somehow find a universal way for this, then good for you.
Thank you so much for your reply. It has really helped me.
Since you're doing this in the console, maybe see if ncurses/pdcurses would help. I believe there is a way to make their version of getch() non-blocking by calling nodelay() ahead of time.

They also offer a number of printing functions that may be of interest to you since you're trying to use your console for fancier stuff than simple reading and writing text.

-Albatross
Topic archived. No new replies allowed.