-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKey_Strokes.c_backup
executable file
·127 lines (110 loc) · 2.59 KB
/
Key_Strokes.c_backup
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#define SIZE 51
#define UP 183
#define DOWN 184
#define RIGHT 185
#define LEFT 186
int scan_keyboard(void);
void wait();
int kbhit();
void move(char);
int get_new_point(int, char);
int head = ((SIZE / 2) - 1) * SIZE + (SIZE / 2) + 2;
int tail = ((SIZE / 2) - 1) * SIZE + (SIZE / 2) - 2;
int board[SIZE][SIZE];
int main()
{
int i = 0;
int j = 0;
char direction = 'r';
char new_direction;
int new_value = 0;
int key_pressed = 0;
// Initialising Board
for(i = 0; i < SIZE; i++) {
for(j = 0; j < SIZE; j++) {
board[i][j] = 0;
}
}
// Initially Snake
board[25][27] = 2; // Snake's Head
board[25][23] = board[25][24] = board[25][25] = board[25][26] = 1; // Snake's body
system("stty raw -echo");
printf("enter 'q' to quit \n");
for (i = 0; (direction != 'q'); i++) {
wait();
//new_value = 0;
if (kbhit()) {
//direction = scan_keyboard();
key_pressed = scan_keyboard();
if(key_pressed == 27) {
key_pressed += scan_keyboard();
if(key_pressed == 118) {
key_pressed += scan_keyboard();
if(key_pressed == UP && direction != 'd')
direction = 'u';
else if(key_pressed == DOWN && direction != 'u')
direction = 'd';
else if(key_pressed == RIGHT && direction != 'l')
direction = 'r';
else if(key_pressed == LEFT && direction != 'r')
direction = 'l';
}
}
else if (key_pressed == 81 || key_pressed == 113)
direction = 'q';
}
move(direction);
}
system("stty cooked echo");
return 0;
}
void move(char ch)
{
printf("\r");
if(ch == 'U' || ch == 'u')
printf("Up\n");
else if (ch == 'D' || ch == 'd')
printf("Down\n");
else if (ch == 'R' || ch == 'r')
printf("Right\n");
else if (ch == 'L' || ch == 'l')
printf("Left\n");
fflush(stdout);
return;
}
int get_new_point(int pos, char dir)
{
int ans = 0;
return ans;
}
// Another one!
int scan_keyboard(void)
{
struct termios oldstuff;
struct termios newstuff;
int inch;
tcgetattr(STDIN_FILENO, &oldstuff);
newstuff = oldstuff; /* save old attributes */
newstuff.c_lflag &= ~(ICANON | ECHO); /* reset "canonical" and "echo" flags*/
tcsetattr(STDIN_FILENO, TCSANOW, &newstuff); /* set new attributes */
inch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldstuff); /* restore old attributes */
return inch;
}
void wait()
{
clock_t now = clock();
while((clock() - now) < (0.07 * CLOCKS_PER_SEC));
return;
}
int kbhit()
{
int i;
ioctl(0, FIONREAD, &i);
return i; /* return a count of chars available to read */
}