forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakeGame.c
More file actions
172 lines (143 loc) · 3.8 KB
/
snakeGame.c
File metadata and controls
172 lines (143 loc) · 3.8 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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 20
#define HEIGHT 20
// Snake head coordinates
int x, y;
// Fruit coordinates
int fruitX, fruitY;
// Score and tail details
int score;
int tailX[100], tailY[100];
int nTail;
// Game control flags
int gameover;
// Direction enumeration
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir, prevDir;
// Setup initial game state
void setup() {
gameover = 0;
dir = STOP;
x = WIDTH / 2;
y = HEIGHT / 2;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
score = 0;
nTail = 0;
}
// Draw the game area
void draw() {
system("cls"); // Clear screen
// Top boundary
for (int i = 0; i < WIDTH + 2; i++) printf("##");
printf("\n");
// Main grid
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (j == 0) printf("##"); // Left boundary
if (i == y && j == x)
printf("[]"); // Snake head
else if (i == fruitY && j == fruitX)
printf("**"); // Food
else {
int printed = 0;
// Draw the tail
for (int k = 0; k < nTail; k++) {
if (tailX[k] == j && tailY[k] == i) {
printf("oo");
printed = 1;
break;
}
}
if (!printed) printf(" "); // Empty space
}
if (j == WIDTH - 1) printf("##"); // Right boundary
}
printf("\n");
}
// Bottom boundary
for (int i = 0; i < WIDTH + 2; i++) printf("##");
printf("\n");
// Display score
printf("Score: %d\t(Press 'q' to quit)\n", score);
}
// Handle user input
void input() {
if (_kbhit()) {
int first = _getch();
// Check if it's an arrow key
if (first == 0 || first == 224) {
int ch = _getch();
switch (ch) {
case 72: if (prevDir != DOWN) dir = UP; break; // Up arrow
case 80: if (prevDir != UP) dir = DOWN; break; // Down arrow
case 75: if (prevDir != RIGHT) dir = LEFT; break; // Left arrow
case 77: if (prevDir != LEFT) dir = RIGHT; break; // Right arrow
}
}
else if (first == 'q' || first == 'Q') {
gameover = 1; // Quit the game
}
}
}
// Game logic (movement, collision, growth)
void logic() {
prevDir = dir;
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
// Move tail
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
// Move head
switch (dir) {
case LEFT: x--; break;
case RIGHT: x++; break;
case UP: y--; break;
case DOWN: y++; break;
default: break;
}
// Wrap around screen edges
if (x >= WIDTH) x = 0;
else if (x < 0) x = WIDTH - 1;
if (y >= HEIGHT) y = 0;
else if (y < 0) y = HEIGHT - 1;
// If snake eats fruit
if (x == fruitX && y == fruitY) {
score += 10;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
nTail++;
}
// Check for collision with tail
for (int i = 0; i < nTail; i++) {
if (tailX[i] == x && tailY[i] == y)
gameover = 1;
}
}
int main() {
setup();
// Game loop
while (!gameover) {
draw();
input();
logic();
Sleep(100); // Control speed
}
system("cls");
printf("\n\tGAME OVER!\n\tYour Score: %d\n", score);
printf("\n\tPress any key to exit...\n");
getch();
return 0;
}