基於 Turbo C++ / VC++ 6.0 圖形庫 graphics.h 實現的經典貪吃蛇遊戲,包含選單、計分、難度遞增、作者資訊等功能,適合初學者參考。
struct snake { char x,y; snake *prev, *next; },頭尾指針動態增刪。char map[30][30] 記錄空地(0)、蛇身(1)、食物(2)。bioskey() 非阻塞讀取方向鍵,防止 180° 轉向。gettime() 微秒級延遲,根據長度動態調整速度。barbox()、box() 繪製方格與 UI。#include <stdio.h>
#include <bios.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <alloc.h>
#include <stdlib.h>
#include <time.h>
#define LEFT 0x4B00
#define RIGHT 0x4D00
#define UP 0x4800
#define DOWN 0x5000
#define ESC 0x011B
#define ENTER 0x1C0D
// ... 完整代碼見原文,含選單、遊戲主循環、作者資訊等 ...
int main() {
srand((unsigned)time(NULL));
s_detectgraph(); // 圖形模式初始化
s1(); // 進入主選單
getch();
closegraph();
return 0;
}
graphics.lib,TC/VC6 專案設置「Linker → Libraries」加入。graphics.h,建議改用 SDL2、SFML 或 Qt 重寫圖形層。