#include <conio.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define WIDTH 50 #define HEIGHT 25 int snake_x[WIDTH * HEIGHT], snake_y[WIDTH * HEIGHT]; int food_x, food_y; int snake_length; int score; int direction; void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void generate_food() { srand((unsigned)time(NULL)); food_x = rand() % (WIDTH - 2) + 1; food_y = rand() % (HEIGHT - 2) + 1; } void draw() { system("cls"); for (int i = 0; i < WIDTH; i++) { printf("#"); } printf(" "); for (int i = 1; i < HEIGHT - 1; i++) { printf("#"); for (int j = 1; j < WIDTH - 1; j++) { if (i == food_y && j == food_x) { printf("*"); } else { int is_snake = 0; for (int k = 0; k < snake_length; k++) { if (i == snake_y[k] && j == snake_x[k]) { printf("O"); is_snake = 1; break; } } if (!is_snake) { printf(" "); } } } printf("# "); } for (int i = 0; i < WIDTH; i++) { printf("#"); } printf(" "); printf("Score: %d", score); } void update() { int prev_x = snake_x[0], prev_y = snake_y[0]; int prev2_x, prev2_y; switch (direction) { case 0: // Up snake_y[0]--; break; case 1: // Down snake_y[0]++; break; case 2: // Right snake_x[0]++; break; case 3: // Left snake_x[0]--; break; } for (int i = 1; i < snake_length; i++) { prev2_x = snake_x[i]; prev2_y = snake_y[i]; snake_x[i] = prev_x; snake_y[i] = prev_y; prev_x = prev2_x; prev_y = prev2_y; } if (snake_x[0] == food_x && snake_y[0] == food_y) { score += 10; snake_length++; generate_food(); } }