c語言貪吃蛇

2022-05-30 02:03:11 字數 1759 閱讀 5018

思路:函式gotoxy(x,y)使游標移植螢幕的x,y座標(螢幕左上角為0,0),用來繪製蛇和介面,color()函式用來設定繪製的顏色。設有snakelong節,第i節蛇的x座標為x[i],y座標為y[i],以使用者設定重新繪製蛇,

根據鍵盤輸入控制蛇頭(x[1],y[1])的座標,然後繪製蛇頭,並且擦掉蛇尾(x[snakelong],y[snakelong]),每次更新介面時判斷蛇頭是否和蛇身相交或者與牆壁相交,如果有則退出迴圈結束遊戲,每次更新介面

判斷蛇頭是否和蘋果(使蛇變長)相交,相交的話snakelong++。並且下次繪製時不擦蛇尾。當蘋果被吃掉了,則新隨機生成乙個蘋果,蘋果在介面內並且不與蛇相交。

其他積分計算什麼的看**吧。

#include #include #include #include int x[10000];

int y[10000];

int snakelong=3;

char c;

int h=25;

int w=50;

int life=0;

int speed;

int go=5; //0:front 1:back 2:left 3:right

void color(int b)

void gotoxy(int x, int y)

void snakemove(int go)

else

int i;

for(i=snakelong;i>=2;i--)

if(go==0)

if(go==1)

if(go==2)

if(go==3)

}void printback(int high,int weight)

for(i=1;i<=weight;i=i+2)

for(i=1;i<=high;i++)

for(i=1;i<=high;i++)

gotoxy(w+3,0);

printf("按空格鍵暫停");

gotoxy(w+3,1);

printf("按wasd控制前後左右與繼續");

} printf("★");

color(10);

}int main()

x[1]=10;

y[1]=10;

int i;

gotoxy(0,0);

for(i=1;i<=1000;i++)printf(" ");

color(12);

printback(h,w);

color(10);

gotoxy(x[1],y[1]);

printf("■");

while(life==0)

for(i=2;i<=snakelong;i++)

if(x[1]==x[i]&&y[1]==y[i])

if(life==1) break;

if(x[1]<=0||x[1]>=w||y[1]<=0||y[1]>=h-1) break;

if(go!=5) snakemove(go);

gotoxy(w+3,3);

printf("分數:%d",(snakelong-3)*10);

sleep(250-speed*20);

}beep(350,200);

beep(250,200);

gotoxy(0,h+1);

printf("你的蛇死了,按任意鍵退出遊戲");

getch();

return 0;

}

C語言貪吃蛇

include include include include include include define screen width 40 遊戲螢幕寬度 define screen length 15 遊戲螢幕長度 define start x 16 螢幕起始x座標 define start y ...

C語言 貪吃蛇

include include include define framewidght 22 define frameheight 22 short generaterandnumber short a,short b void posconsolecursor short x,short y voi...

C語言貪吃蛇

親測devc 編譯器完美執行 define crt secure no warnings include include include include void readini file fphead,int score,char argv 建立或開啟乙個和執行檔案對應的ini檔案,讀取最高紀錄 ...