貪吃蛇涵蓋:陣列/鏈表、鍵盤輸入、定時器、碰撞檢測、遊戲循環、分數系統——適合綜合練習。
struct Node {int x,y; Node* next;} 鏈表身體_kbhit() + _getch() (Windows) / ncurses (Linux)Sleep() / nanosleep() / SetTimerSetConsoleCursorPosition / ANSI 轉義碼
#define WIDTH 40
#define HEIGHT 20
typedef struct { int x, y; } Point;
Point snake[1000]; // 固定陣列模擬隊列
int head = 0, tail = 0, len = 3;
int dir = 1; // 0上1右2下3左
Point food;
void init() { /* 初始化蛇身、食物、畫面 */ }
void input() { /* 非阻塞讀鍵盤改變dir */ }
void update() { /* 移動蛇頭、檢測牆壁/自身、吃食物增長 */ }
void render() { /* 清屏、畫蛇身、畫食物、畫分數 */ }
int main() {
init();
while (1) {
input();
update();
render();
Sleep(100); // 速度控制
}
}