-
Notifications
You must be signed in to change notification settings - Fork 5
/
snake.c
149 lines (135 loc) · 3.45 KB
/
snake.c
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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define BOARD_WIDTH 20
#define BOARD_HEIGHT 20
#define SNAKE_LENGTH 5
typedef struct
{
int x;
int y;
} Point;
typedef struct Node
{
Point pos;
struct Node *next;
} Node;
Node *snake;
void init_snake()
{
// Initialize the snake with a few segments
snake = malloc(sizeof(Node));
snake->pos.x = BOARD_WIDTH / 2;
snake->pos.y = BOARD_HEIGHT / 2;
snake->next = NULL;
for (int i = 1; i < SNAKE_LENGTH; i++)
{
Node *segment = malloc(sizeof(Node));
segment->pos.x = snake->pos.x - i;
segment->pos.y = snake->pos.y;
segment->next = snake;
snake = segment;
}
}
void display_board(char board[BOARD_HEIGHT][BOARD_WIDTH])
{
// Display the game board using ASCII characters
system("cls"); // Clear the console
for (int i = 0; i < BOARD_HEIGHT; i++)
{
for (int j = 0; j < BOARD_WIDTH; j++)
{
putchar(board[i][j]);
}
putchar('\n');
}
}
void update_snake(Point dir, char board[BOARD_HEIGHT][BOARD_WIDTH])
{
// Update the snake's position based on its current direction
Node *head = snake;
Point new_head = {head->pos.x + dir.x, head->pos.y + dir.y};
Node *new_segment = malloc(sizeof(Node));
new_segment->pos = new_head;
new_segment->next = head;
snake = new_segment;
board[new_head.y][new_head.x] = 'o'; // Add new head to board
if (board[new_head.y][new_head.x] == 'X')
{ // Check for collision with food
// Increase snake length and place new food
// TODO: Implement this
}
else
{
// Remove tail from board and free its memory
Node *tail = head;
while (tail->next->next != NULL)
{
tail = tail->next;
}
board[tail->next->pos.y][tail->next->pos.x] = ' ';
free(tail->next);
tail->next = NULL;
}
}
int main()
{
// Set up the game board and initialize the snake
char board[BOARD_HEIGHT][BOARD_WIDTH];
for (int i = 0; i < BOARD_HEIGHT; i++)
{
for (int j = 0; j < BOARD_WIDTH; j++)
{
board[i][j] = ' ';
}
}
init_snake();
Point dir = {1, 0}; // Snake starts moving to the right
// Set up the game loop
int game_over = 0;
while (!game_over)
{
// Update the game state
update_snake(dir, board);
display_board(board);
// Handle user input
if (_kbhit())
{ // Check if a key has been pressed
char key = _getch();
switch (key)
{
case 'w':
dir = (Point){0, -1};
break;
case 'a':
dir = (Point){-1, 0};
break;
case 's':
dir = (Point){0, 1};
case 'd':
dir = (Point){1, 0};
break;
case 'q':
game_over = 1;
break;
default:
break;
}
}
// Pause for a short time before updating the game again
clock_t start_time = clock();
while (clock() < start_time + 1000 / 10)
{
}
// TODO: Check for game over conditions (e.g. collision with wall or self)
}
// Free memory used by snake
while (snake != NULL)
{
Node *next = snake->next;
free(snake);
snake = next;
}
return 0;
}