Small snake game in C
Commit cbef75faf772
tarball3 files changed+64-40
@@ -315,12 +315,13 @@ bool draw_board(
315315
selected game_size,316316
board_state **board,317317
struct winsize window_size, 318
- bool redraw, bool paused318
+ bool redraw, bool paused,319
+ uint16_t score319320
) {320321
321322
uint8_t i, j;322323
323
- uint8_t actual_height = game_size.y + 2;324
+ uint8_t actual_height = game_size.y + 2 + 2;324325
uint8_t actual_width = game_size.x * 2 + 1 + 2;325326
326327
if (actual_width > window_size.ws_col || actual_height > window_size.ws_row) {@@ -343,13 +344,26 @@ bool draw_board(
343344
printf("\033[%c", UP_CODE);344345
printf("\r");345346
}347
+ 348
+ for (i = 0; i < ((actual_width - 9) >> 1) + horizontal_buffer; i++)349
+ printf(" ");350
+ printf("╭───────╮\n");351
+ for (i = 0; i < ((actual_width - 9) >> 1) + horizontal_buffer; i++)352
+ printf(" ");353
+ printf("│ \033[32m%05d\033[0m │\n", score);346354
347355
for (i = 0; i < horizontal_buffer; i++)348356
printf(" ");349357
printf("╭");350358
for (i = 0; i < game_size.x; i++)351359
printf("──");352
- printf("─╮\n");360
+ printf("─╮");361
+ printf("\r");362
+ for (i = 0; i < ((actual_width - 9) >> 1) + horizontal_buffer; i++)363
+ printf("\033[%c", RIGHT_CODE);364
+ printf("┴───────┴\n");365
+366
+353367
354368
for (i = 0; i < game_size.y; i++) {355369
@@ -139,6 +139,8 @@ int main(int argc, char *args[]) {
139139
140140
} else {141141
142
+ /* Settings Menu */143
+142144
if (change_menu) {143145
144146
draw_settings(selected_setting, game_speed, game_size);@@ -149,6 +151,8 @@ int main(int argc, char *args[]) {
149151
150152
if (pressed_key < DIRECTIONS) {151153
154
+ int8_t setting_change = 0;155
+152156
switch (pressed_key) {153157
case UP:154158
selected_setting = (selected_setting + (SETTINGS_MENU - 1)) % SETTINGS_MENU;@@ -157,44 +161,46 @@ int main(int argc, char *args[]) {
157161
selected_setting = (selected_setting + 1) % SETTINGS_MENU;158162
break;159163
case LEFT:160
- switch (selected_setting) {161
- case SPEED_S:162
- if (game_speed < SPEED_L - 1)163
- game_speed++;164
- break;165
- case WIDTH_S:166
- if (game_size.x > MIN_SIZE)167
- game_size.x--;168
- break;169
- case HEIGHT_S:170
- if (game_size.y > MIN_SIZE)171
- game_size.y--;172
- break;173
- default:174
- break;175
- }164
+ setting_change = 1;176165
break;177166
case RIGHT:178
- switch (selected_setting) {179
- case SPEED_S:180
- if (game_speed)181
- game_speed--;182
- break;183
- case WIDTH_S:184
- if (game_size.x < MAX_SIZE)185
- game_size.x++;186
- break;187
- case HEIGHT_S:188
- if (game_size.y < MAX_SIZE)189
- game_size.y++;190
- break;191
- default:192
- break;193
- }167
+ setting_change = -1;194168
break;195169
default:196170
break;197171
}172
+ 173
+ if (setting_change) {174
+ switch (selected_setting) {175
+ case SPEED_S:176
+ if (177
+ game_speed + setting_change <= SPEED_L - 1 &&178
+ ((int8_t) game_speed) + setting_change >= 0 179
+ ) {180
+ game_speed += setting_change;181
+ }182
+ break;183
+ case WIDTH_S:184
+ if (185
+ game_size.x - setting_change >= MIN_SIZE &&186
+ game_size.x - setting_change <= MAX_SIZE187
+ ) {188
+ game_size.x -= setting_change;189
+ }190
+ break;191
+ case HEIGHT_S:192
+ if (193
+ game_size.y - setting_change >= MIN_SIZE &&194
+ game_size.y - setting_change <= MAX_SIZE195
+ ) {196
+ game_size.y -= setting_change;197
+ }198
+ break;199
+ default:200
+ break;201
+ }202
+ }203
+198204
change_menu = true;199205
200206
}@@ -255,7 +261,9 @@ int main(int argc, char *args[]) {
255261
256262
bool paused = false;257263
258
- if ((too_small = draw_board(game_size, board, current_size, true, false)))264
+ uint16_t score = 0;265
+266
+ if ((too_small = draw_board(game_size, board, current_size, true, false, score)))259267
paused = true;260268
261269
@@ -280,7 +288,7 @@ int main(int argc, char *args[]) {
280288
if (toupper(string[0]) == 'P' || string[0] == '\n') {281289
paused = !paused;282290
if (paused)283
- draw_board(game_size, board, current_size, true, paused);291
+ draw_board(game_size, board, current_size, true, paused, score);284292
}285293
286294
if (!paused)@@ -307,7 +315,7 @@ int main(int argc, char *args[]) {
307315
init_size = current_size;308316
309317
paused = true;310
- too_small = draw_board(game_size, board, current_size, true, paused);318
+ too_small = draw_board(game_size, board, current_size, true, paused, score);311319
}312320
313321
if (!paused) {@@ -324,6 +332,7 @@ int main(int argc, char *args[]) {
324332
325333
if (board[head_y][head_x] == FRUIT) {326334
my_snake.length++;335
+ score++;327336
snake_part *tmp = realloc(my_snake.position, (my_snake.length + 1) * sizeof(snake_part));328337
if (tmp == NULL)329338
goto end_free;@@ -335,7 +344,7 @@ int main(int argc, char *args[]) {
335344
336345
update_field(my_snake, game_size, board, false);337346
338
- draw_board(game_size, board, current_size, false, false);347
+ draw_board(game_size, board, current_size, false, false, score);339348
340349
switch (update) {341350
case 0: @@ -368,5 +377,6 @@ end_of_program:
368377
369378
370379
printf("\033[32m");380
+ printf("\n\nYour score: %5d", score);371381
return 0;372382
}@@ -80,7 +80,7 @@ bool draw_menu(main_menu menu, struct winsize window_size);
8080
void draw_settings(settings selected_setting, speed game_speed, selected game_size);8181
8282
bool draw_board(selected game_size, board_state **board,83
- struct winsize window_size, bool redraw, bool paused);83
+ struct winsize window_size, bool redraw, bool pause, uint16_t score);8484
8585
void draw_pause(struct winsize window_size, 8686
uint8_t field_width,uint8_t field_height);