-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.c
More file actions
188 lines (172 loc) · 4.87 KB
/
Copy pathsnake.c
File metadata and controls
188 lines (172 loc) · 4.87 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "queue.h"
#define WIDTH 50
#define HEIGHT 20
#define UP 'w'
#define DOWN 's'
#define LEFT 'a'
#define RIGHT 'd'
#define EXIT 'q'
#define PAUSE ' '
int score, fruitX, fruitY, gameOver;
Queue *psnake;
void ttyMode(int enable)
{
struct termios ttystate;
tcgetattr(STDIN_FILENO, &ttystate);
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
if (enable) {
ttystate.c_lflag &= ~(ICANON | ECHO);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
}
else {
ttystate.c_lflag |= ICANON | ECHO;
fcntl(STDIN_FILENO, F_SETFL, flags & ~O_NONBLOCK);
}
tcsetattr(STDIN_FILENO,TCSANOW, &ttystate);
}
void setUp ()
{
enum direction dir = STOP;
int x = WIDTH / 2;
int y = HEIGHT / 2;
initQueue(psnake);
enQueue(psnake, x, y, dir);
fruitX = rand() % (WIDTH - 2) + 1;
fruitY = rand() % (HEIGHT - 2) + 1;
}
void fieldDraw()
{
system("clear");
for (int i = 0; i < WIDTH; i++)
printf("#");
printf ("\n");
for (int i = 0; i < HEIGHT - 1; i++) {
printf("#");
for (int j = 0; j < WIDTH - 2; j++) {
int tailPrinted = 0;
Node *pnode = psnake->front;
while (pnode != NULL) {
if (pnode->tailX == j && pnode->tailY == i) {
if (pnode == psnake->front)
printf("O");
else
printf("o");
tailPrinted = 1;
break;
}
pnode = pnode->next;
}
if (!tailPrinted) {
if (fruitX == j && fruitY == i)
printf("F");
else
printf(" ");
}
}
printf("#\n");
}
for (int i = 0; i < WIDTH; i++)
printf("#");
printf("\nGlobal score: %d\n", score);
}
void inputLogic()
{
char ch;
if (read(STDIN_FILENO, &ch, 1) > 0) {
switch (ch) {
case LEFT:
if (psnake->front->tailDir != RIGHT_DIR)
psnake->front->tailDir = LEFT_DIR;
break;
case RIGHT:
if (psnake->front->tailDir != LEFT_DIR)
psnake->front->tailDir = RIGHT_DIR;
break;
case UP:
if (psnake->front->tailDir != DOWN_DIR)
psnake->front->tailDir = UP_DIR;
break;
case DOWN:
if (psnake->front->tailDir != UP_DIR)
psnake->front->tailDir = DOWN_DIR;
break;
case PAUSE:
psnake->front->tailDir = STOP;
break;
case EXIT:
gameOver = 1;
psnake->front->tailDir = STOP;
break;
default:
break;
}
}
}
void snakeLogic()
{
int oldX = psnake->front->tailX;
int oldY = psnake->front->tailY;
enum direction oldDir = psnake->front->tailDir;
switch(psnake->front->tailDir) {
case LEFT_DIR:
psnake->front->tailX--;
break;
case RIGHT_DIR:
psnake->front->tailX++;
break;
case UP_DIR:
psnake->front->tailY--;
break;
case DOWN_DIR:
psnake->front->tailY++;
break;
default:
break;
}
if (psnake->front->tailDir == LEFT_DIR && psnake->front->tailX <= 0)
psnake->front->tailX = WIDTH - 2;
if (psnake->front->tailDir == RIGHT_DIR && psnake->front->tailX >= WIDTH - 1)
psnake->front->tailX = 1;
if (psnake->front->tailDir == UP_DIR && psnake->front->tailY <= 0)
psnake->front->tailY = HEIGHT - 2;
if (psnake->front->tailDir == DOWN_DIR && psnake->front->tailY >= HEIGHT - 1)
psnake->front->tailY = 1;
if (psnake->front->tailX == fruitX && psnake->front->tailY == fruitY) {
score += 10;
fruitX = rand() % (WIDTH - 2) + 1;
fruitY = rand() % (HEIGHT - 2) + 1;
enQueue(psnake, oldX, oldY, oldDir);
}
else if (psnake->sizeTail > 0) {
Node *current = psnake->rear;
while (current != psnake->front) {
current->tailX = current->prev->tailX;
current->tailY = current->prev->tailY;
current->tailDir = current->prev->tailDir;
current = current->prev;
}
}
}
int main(void) {
srand(time(NULL));
psnake = (Queue *)malloc(sizeof(Queue));
setUp();
ttyMode(1);
while (!gameOver) {
fieldDraw();
inputLogic();
snakeLogic();
usleep(100000 * 2.5);
}
ttyMode(0);
emptyQueue(psnake);
free(psnake);
printf("Game Over! Final score: %d\n", score);
return 0;
}