貪吃蛇是程式設計入門的經典練習題,透過這個專案可以熟悉圖形介面程式設計、鍵盤事件處理、遊戲迴圈邏輯以及資料結構的運用。本文提供一個基於 Windows 平台、使用 graphics.h 圖形庫的完整實作,包含彩虹背景、星空特效、分數顯示等視覺增強功能。
程式主要包含以下模組:
WinMM.lib 支援)。frame()):繪製邊框、分數面板、彩虹漸層背景、底部星空動畫。STAR 結構體,透過 InitStar() 初始化位置與速度,MoveStar() 實現水平移動並循環重生,營造星空流動效果。frame() 中以 while(true) 持續更新星星位置,配合 Sleep(40) 控制幀率。利用 HSL 色彩空間,固定飽和度 (S=1) 與亮度 (L=0.7),逐行遞增色相 (H) 並微調亮度,繪製水平線條構成頂部彩虹帶:
float H = 190, S = 1, L = 0.7f;
for(int y = 70; y < 480; y++) {
L += 0.0005f;
setlinecolor(HSLtoRGB(H, S, L));
line(0, y, 639, y);
}
以螢幕右下角為圓心,半徑從 400 遞減至 344,色相每次增加 5 度,繪製同心圓弧:
H = 0; S = 1; L = 0.5f;
setlinestyle(PS_SOLID, 2);
for(int r = 400; r > 344; r--) {
H += 5;
setlinecolor(HSLtoRGB(H, S, L));
circle(500, 480, r);
}
定義結構體儲存座標、速度、顏色:
struct STAR {
int x, y;
double step;
int color;
} star[MAXSTAR];
InitStar() 將星星重置至左側 (x=171),隨機 Y 軸,速度 1~6 像素/幀,顏色隨速度變亮。MoveStar() 清除舊像素、更新座標、超出右邊界則重生、繪製新像素。
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"WinMM.lib")
#include <string.h>
#include <math.h>
#include <time.h>
#include <graphics.h>
#define PI 3.1415926
#define WIDTH 640
#define HEIGHT 480
#define MAXSTAR 100
struct STAR { int x, y; double step; int color; } star[MAXSTAR];
void frame();
void InitStar(int);
void MoveStar(int);
int main() {
PlaySound("k:\\resource\\劉三姐.wav", NULL, SND_FILENAME|SND_ASYNC|SND_LOOP);
initgraph(WIDTH, HEIGHT, NOMINIMIZE);
setbkcolor(WHITE);
cleardevice();
frame();
getch();
closegraph();
return 0;
}
void frame() {
int i;
srand((unsigned)time(NULL));
setcolor(RGB(rand()%225, rand()%225, rand()%225));
rectangle(0, 0, WIDTH-1, HEIGHT-1);
setcolor(CYAN);
line(0, 49, WIDTH-1, 49);
outtextxy(1, 1, "PRESENT-SCORE:");
outtextxy(1, 16, "HIGHEST-SCORE:");
outtextxy(1, 30, " START-TIMES:");
line(120, 0, 120, 49);
line(170, 0, 170, 49);
outtextxy(121, 1, "888888");
line(0, 16, 170, 16);
line(0, 30, 170, 30);
// 第二條框
setfillstyle(BS_SOLID);
setfillcolor(LIGHTGREEN);
fillrectangle(0, 50, 639, 69);
// 彩虹漸層
float H = 190, S = 1, L = 0.7f;
for(int y = 70; y < 480; y++) {
L += 0.0005f;
setlinecolor(HSLtoRGB(H, S, L));
line(0, y, 639, y);
}
// 圓弧彩虹
H = 0; S = 1; L = 0.5f;
setlinestyle(PS_SOLID, 2);
for(int r = 400; r > 344; r--) {
H += 5;
setlinecolor(HSLtoRGB(H, S, L));
circle(500, 480, r);
}
// 星空
setbkcolor(BLACK);
setfillcolor(BLACK);
solidrectangle(170, 1, 639, 49);
for(i = 0; i < MAXSTAR; i++) {
InitStar(i);
star[i].x = rand() % (640 - 170) + 170;
}
while(true) {
for(i = 0; i < MAXSTAR; i++) MoveStar(i);
Sleep(40);
}
}
void InitStar(int i) {
star[i].x = 171;
star[i].y = rand() % 50;
star[i].step = (rand() % 5000) / 1000.0 + 1;
star[i].color = (int)(star[i].step * 255 / 6.0 + 0.5);
star[i].color = RGB(star[i].color, star[i].color, star[i].color);
}
void MoveStar(int i) {
putpixel((int)star[i].x, star[i].y, 0);
star[i].x += star[i].step;
if (star[i].x > 639) InitStar(i);
putpixel((int)star[i].x, star[i].y, star[i].color);
}
graphics.h 的編譯環境(如 EasyX、Dev-C++ 配合 WinBGIm)。k:\\resource\\劉三姐.wav 需根據實際情況修改或註解掉 PlaySound 行。_kbhit() + _getch()) 驅動蛇身移動。