#include graphics.h
成都創(chuàng)新互聯(lián)專(zhuān)注于沾化企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開(kāi)發(fā),商城網(wǎng)站建設(shè)。沾化網(wǎng)站建設(shè)公司,為沾化等地區(qū)提供建站服務(wù)。全流程按需制作網(wǎng)站,專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
#include stdlib.h
#include dos.h /*引用的庫(kù)函數(shù)*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b/*宏定義鍵名*/
#define N 200
int i,key;
int level;/*游戲等級(jí)*/
int score=0;/*得分*/
int gamespeed;/*游戲速度*/
struct Food
{
int x;/*食物的橫坐標(biāo)*/
int y;/*食物的縱坐標(biāo)*/
int yes;/*判斷是否要出現(xiàn)食物的變量*/
}food;/*食物的結(jié)構(gòu)體*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的節(jié)數(shù)*/
int direction;/*蛇移動(dòng)方向*/
int life;/* 蛇的生命,0活著,1死亡*/
}snake;/*蛇的結(jié)構(gòu)體*/
void Choicelevle(void);/*選擇游戲等級(jí)*/
void Init(void);/*圖形驅(qū)動(dòng)*/
void Close(void);/*圖形結(jié)束*/
void DRAW(void);/*游戲區(qū)域*/
void GameOver(void);/*結(jié)束游戲*/
void GamePlay(void);/*玩游戲具體過(guò)程*/
void PrScore(void);/*輸出成績(jī)*/
/*主函數(shù)*/
void main(void)
{
Init();/*圖形驅(qū)動(dòng)*/
Choicelevle();/*選擇游戲等級(jí)*/
DRAW();/*游戲區(qū)域*/
GamePlay();/*玩游戲具體過(guò)程*/
Close();/*圖形結(jié)束*/
}
/*圖形驅(qū)動(dòng)*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(gd,gm,"\\turboc2"); /*初始化圖形系統(tǒng)*/
cleardevice(); /*清除圖形界面*/
}
/*選擇游戲等級(jí)*/
void Choicelevle(void)
{char name[20];
setcolor(YELLOW);
settextstyle(0,0,6);
outtextxy(150,150,"Snake");
setcolor(GREEN);
settextstyle(0,0,1);
outtextxy(200,250,"please put in your English name:");
outtextxy(200,270,"Choice levle from 1-9.");
outtextxy(300,320,"name:yangzilong");/*制作人姓名*/
outtextxy(300,350,"number:0902060226");/*制作人學(xué)號(hào)*/
outtextxy(300,380,"class:computer science 0602");/*制作人班級(jí)*/
getch();
printf("please putin your name:");
gets(name);
printf("please choice levle:");
scanf("%d",level);
gamespeed=100000-400*level-300*level*level;
if(level9||level1)
{cleardevice(); /*清除圖形界面*/
setcolor(YELLOW); /*設(shè)置字體顏色*/
settextstyle(0,0,2); /*設(shè)置字體類(lèi)型*/
outtextxy(150,200,"level input error"); /*顯示文本*/
getch();
level=1;
}
}
void DRAW(void)
{cleardevice(); /*清屏*/
setcolor(2);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設(shè)置線型*/
rectangle(45,45,465,325);
}
/*玩游戲具體過(guò)程*/
void GamePlay(void)
{setcolor(5);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設(shè)置線型*/
randomize();/*隨機(jī)數(shù)發(fā)生器*/
food.yes=1;/*1表示需要出現(xiàn)新食物,0表示已經(jīng)存在食物*/
snake.life=0;/*活著*/
snake.direction=1;/*方向往右*/
snake.x[0]=320;snake.y[0]=240;/*蛇頭*/
snake.x[1]=330;snake.y[1]=240; /*蛇的第二節(jié)位置*/
snake.node=3;/*節(jié)數(shù)*/
PrScore();/*輸出得分*/
while(1)/*可以重復(fù)玩游戲,壓ESC鍵結(jié)束*/
{
while(!kbhit())/*在沒(méi)有按鍵的情況下,蛇自己移動(dòng)身體*/
{
if(food.yes==1)/*需要出現(xiàn)新食物*/
{
food.x=rand()%360+70;
food.y=rand()%250+60;
while(food.x%10!=0)/*食物隨機(jī)出現(xiàn)后必須讓食物能夠在整格內(nèi),這樣才可以讓蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*畫(huà)面上有食物了*/
}
if(food.yes==0)/*畫(huà)面上有食物了就要顯示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i0;i--)/*蛇的每個(gè)環(huán)節(jié)往前移動(dòng)*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個(gè)方向,通過(guò)這個(gè)控制來(lái)移動(dòng)蛇頭*/
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;isnake.node;i++)/*從蛇的第四節(jié)開(kāi)始判斷是否撞到自己了,因?yàn)樯哳^為兩節(jié),第三節(jié)不可能拐過(guò)來(lái)*/
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
GameOver();/*顯示失敗*/
snake.life=1; /*蛇死*/
break;
}
}
/*如果蛇頭碰到墻壁,蛇頭從對(duì)面墻出來(lái)*/
if(snake.x[0]50)
{snake.x[0]=450;/*如果蛇頭越過(guò)左邊界,則從右邊界進(jìn)入*/
snake.y[0]=snake.y[0];/*縱坐標(biāo)不變*/
for(i=snake.node-1;i0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節(jié)數(shù)向前推進(jìn)*/
}
{
setfillstyle(SOLID_FILL,0); /*設(shè)置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數(shù)*/
}
}
else
if(snake.x[0]450)
{snake.x[0]=50;/*如果蛇頭越過(guò)右邊界,則蛇頭從左邊界進(jìn)入*/
snake.y[0]=snake.y[0];/*縱坐標(biāo)不變*/
for(i=snake.node-1;i0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節(jié)數(shù)向前推進(jìn)*/
}
{
setfillstyle(SOLID_FILL,0); /*設(shè)置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數(shù)*/
}
}
else
if(snake.y[0]60)
{snake.y[0]=320;/*如果蛇頭越過(guò)上邊界,則從下邊界進(jìn)入*/
snake.x[0]=snake.x[0];/*橫坐標(biāo)不變*/
for(i=snake.node-1;i0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節(jié)數(shù)向前推進(jìn)*/
}
{
setfillstyle(SOLID_FILL,0); /*設(shè)置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數(shù)*/
}
}
else
if(snake.y[0]320)
{snake.y[0]=60;/*如果蛇頭越過(guò)下邊界,則從上邊界進(jìn)入*/
snake.x[0]=snake.x[0];/*橫坐標(biāo)不變*/
for(i=snake.node-1;i0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*蛇的其他節(jié)數(shù)向前推進(jìn)*/
}
{
setfillstyle(SOLID_FILL,0); /*設(shè)置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數(shù)*/
}
}
if(snake.life==1)/*如果蛇死就跳出內(nèi)循環(huán),重新開(kāi)始*/
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把畫(huà)面上的食物東西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10); /*用當(dāng)前線型和顏色畫(huà)一矩形*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一節(jié)先放在看不見(jiàn)的位置,下次循環(huán)就取前一節(jié)的位置*/
snake.node++;/*蛇的身體長(zhǎng)一節(jié)*/
food.yes=1;/*畫(huà)面上需要出現(xiàn)新的食物*/
score+=10; /*每吃掉一食物,得分累加10分*/
if(score%100==0)
{level++;gamespeed=100000-400*level-300*level*level;/*每吃掉10食物提升一級(jí),速度加快*/
PrScore();/*輸出新得分*/
setcolor(YELLOW); /*設(shè)置字體顏色*/
settextstyle(0,0,4); /*設(shè)置字體類(lèi)型*/
outtextxy(150,200,"LEVEL UP"); /*顯示文本*/
if(level==10)
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
bar(50,55,455,315);/*bar是表示填充的范圍的函數(shù)*/
}
PrScore();/*輸出新得分*/
}
setcolor(4);/*畫(huà)出蛇*/
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed); /*控制游戲速度*/
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/ /*用黑色去除蛇的的最后一節(jié)*/
if(snake.life==1)/*如果蛇死就跳出循環(huán)*/
break;
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*按ESC鍵退出*/
break;
else
if(key==UPsnake.direction!=4)
/*判斷是否往相反的方向移動(dòng)*/
snake.direction=3;
else
if(key==RIGHTsnake.direction!=2)
snake.direction=1;
else
if(key==LEFTsnake.direction!=1)
snake.direction=2;
else
if(key==DOWNsnake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戲結(jié)束*/
void GameOver(void)
{
cleardevice(); /*清屏*/
PrScore();
setcolor(RED); /*設(shè)置字體顏色*/
settextstyle(0,0,4); /*設(shè)置字體類(lèi)型*/
outtextxy(200,200,"GAME OVER"); /*顯示文本*/
getch();
}
/*輸出成績(jī)及游戲等級(jí)*/
void PrScore(void)
{
char str1[20];/*設(shè)置字符型數(shù)組*/
setfillstyle(SOLID_FILL,0);
bar(50,15,390,35); /*填充矩形框*/
setcolor(6); /*設(shè)置文本顏色*/
settextstyle(0,0,2); /*設(shè)置數(shù)組顯示位置*/
sprintf(str1,"score %d level %d",score,level);/*顯示數(shù)組內(nèi)容*/
outtextxy(55,20,str1);
setcolor(YELLOW); /*設(shè)置字體顏色*/
settextstyle(0,0,2); /*設(shè)置字體類(lèi)型*/
outtextxy(250,400,"EXIT=ESC ");/*顯示文本*/
}
void Close(void)
{
closegraph();
}
這是一個(gè)成功的貪吃蛇代碼(c語(yǔ)言編寫(xiě)的),希望你能看懂!慢慢看:
#define N 200
#include graphics.h
#include stdlib.h
#include dos.h
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*得分*/
int gamespeed=50000;/*游戲速度自己調(diào)整*/
struct Food
{
int x;/*食物的橫坐標(biāo)*/
int y;/*食物的縱坐標(biāo)*/
int yes;/*判斷是否要出現(xiàn)食物的變量*/
}food;/*食物的結(jié)構(gòu)體*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的節(jié)數(shù)*/
int direction;/*蛇移動(dòng)方向*/
int life;/* 蛇的生命,0活著,1死亡*/
}snake;
void Init(void);/*圖形驅(qū)動(dòng)*/
void Close(void);/*圖形結(jié)束*/
void DrawK(void);/*開(kāi)始畫(huà)面*/
void GameOver(void);/*結(jié)束游戲*/
void GamePlay(void);/*玩游戲具體過(guò)程*/
void PrScore(void);/*輸出成績(jī)*/
/*主函數(shù)*/
void main(void)
{
Init();/*圖形驅(qū)動(dòng)*/
DrawK();/*開(kāi)始畫(huà)面*/
GamePlay();/*玩游戲具體過(guò)程*/
Close();/*圖形結(jié)束*/
}
/*圖形驅(qū)動(dòng)*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(gd,gm,"c:\\tc");
cleardevice();
}
/*開(kāi)始畫(huà)面,左上角坐標(biāo)為(50,40),右下角坐標(biāo)為(610,460)的圍墻*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設(shè)置線型*/
for(i=50;i=600;i+=10)/*畫(huà)圍墻*/
{
rectangle(i,40,i+10,49); /*上邊*/
rectangle(i,451,i+10,460);/*下邊*/
}
for(i=40;i=450;i+=10)
{
rectangle(50,i,59,i+10); /*左邊*/
rectangle(601,i,610,i+10);/*右邊*/
}
}
/*玩游戲具體過(guò)程*/
void GamePlay(void)
{
randomize();/*隨機(jī)數(shù)發(fā)生器*/
food.yes=1;/*1表示需要出現(xiàn)新食物,0表示已經(jīng)存在食物*/
snake.life=0;/*活著*/
snake.direction=1;/*方向往右*/
snake.x[0]=100;snake.y[0]=100;/*蛇頭*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*節(jié)數(shù)*/
PrScore();/*輸出得分*/
while(1)/*可以重復(fù)玩游戲,壓ESC鍵結(jié)束*/
{
while(!kbhit())/*在沒(méi)有按鍵的情況下,蛇自己移動(dòng)身體*/
{
if(food.yes==1)/*需要出現(xiàn)新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)/*食物隨機(jī)出現(xiàn)后必須讓食物能夠在整格內(nèi),這樣才可以讓蛇吃到*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*畫(huà)面上有食物了*/
}
if(food.yes==0)/*畫(huà)面上有食物了就要顯示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i0;i--)/*蛇的每個(gè)環(huán)節(jié)往前移動(dòng),也就是貪吃蛇的關(guān)鍵算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個(gè)方向,通過(guò)這個(gè)判斷來(lái)移動(dòng)蛇頭*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;isnake.node;i++)/*從蛇的第四節(jié)開(kāi)始判斷是否撞到自己了,因?yàn)樯哳^為兩節(jié),第三節(jié)不可能拐過(guò)來(lái)*/
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
GameOver();/*顯示失敗*/
snake.life=1;
break;
}
}
if(snake.x[0]55||snake.x[0]595||snake.y[0]55||
snake.y[0]455)/*蛇是否撞到墻壁*/
{
GameOver();/*本次游戲結(jié)束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1)/*以上兩種判斷以后,如果蛇死就跳出內(nèi)循環(huán),重新開(kāi)始*/
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把畫(huà)面上的食物東西去掉*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一節(jié)先放在看不見(jiàn)的位置,下次循環(huán)就取前一節(jié)的位置*/
snake.node++;/*蛇的身體長(zhǎng)一節(jié)*/
food.yes=1;/*畫(huà)面上需要出現(xiàn)新的食物*/
score+=10;
PrScore();/*輸出新得分*/
}
setcolor(4);/*畫(huà)出蛇*/
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一節(jié)*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1)/*如果蛇死就跳出循環(huán)*/
break;
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*按ESC鍵退出*/
break;
else
if(key==UPsnake.direction!=4)
/*判斷是否往相反的方向移動(dòng)*/
snake.direction=3;
else
if(key==RIGHTsnake.direction!=2)
snake.direction=1;
else
if(key==LEFTsnake.direction!=1)
snake.direction=2;
else
if(key==DOWNsnake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戲結(jié)束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*輸出成績(jī)*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/*圖形結(jié)束*/
void Close(void)
{
getch();
closegraph();
}
2: srand(time(NULL)); 初始化隨機(jī)種子, 你就記住用rand函數(shù)之前必須要用這個(gè)
3: rand( )隨機(jī)函數(shù), 食物的x坐標(biāo)為 1到界面最右邊少一個(gè)位置中的任意一個(gè)值
4: 食物的y坐標(biāo)為 1到界面最下邊少一個(gè)位置中的任意一個(gè)值
5: 設(shè)置光標(biāo)到食物的坐標(biāo)位置, 以便打印出食物
//******友情提示:如想速度快點(diǎn),請(qǐng)改小_sleep(500)函數(shù)中參數(shù)*****??
#include?stdio.h??
#include?stdlib.h??
#include?conio.h??
#include?string.h??
#include?time.h??
const?int?H?=?8;???//地圖的高??
const?int?L?=?16;??//地圖的長(zhǎng)??
char?GameMap[H][L];???//游戲地圖??
int??key;??//按鍵保存??
int??sum?=?1,?over?=?0;??//蛇的長(zhǎng)度,?游戲結(jié)束(自吃或碰墻)??
int??dx[4]?=?{0,?0,?-1,?1};??//左、右、上、下的方向??
int??dy[4]?=?{-1,?1,?0,?0};??
struct?Snake???//蛇的每個(gè)節(jié)點(diǎn)的數(shù)據(jù)類(lèi)型??
{??
int?x,?y;??//左邊位置??
int?now;???//保存當(dāng)前節(jié)點(diǎn)的方向,?0,1,2,3分別為左右上下??
}Snake[H*L];??
const?char?Shead?=?'@';??//蛇頭??
const?char?Sbody?=?'#';??//蛇身??
const?char?Sfood?=?'*';??//食物??
const?char?Snode?=?'.';??//'.'在地圖上標(biāo)示為空??
void?Initial();??//地圖的初始化??
void?Create_Food();?//在地圖上隨機(jī)產(chǎn)生食物??
void?Show();???//刷新顯示地圖??
void?Button();??//取出按鍵,并判斷方向??
void?Move();???//蛇的移動(dòng)??
void?Check_Border();??//檢查蛇頭是否越界??
void?Check_Head(int?x,?int?y);???//檢查蛇頭移動(dòng)后的位置情況??
int?main()???
{??
Initial();??
Show();??
return?0;??
}??
void?Initial()??//地圖的初始化??
{??
int?i,?j;??
int?hx,?hy;??
system("title?貪吃蛇");??//控制臺(tái)的標(biāo)題??
memset(GameMap,?'.',?sizeof(GameMap));??//初始化地圖全部為空'.'??
system("cls");??
srand(time(0));???//隨機(jī)種子??
hx?=?rand()%H;????//產(chǎn)生蛇頭??
hy?=?rand()%L;??
GameMap[hx][hy]?=?Shead;??
Snake[0].x?=?hx;??Snake[0].y?=?hy;??
Snake[0].now?=?-1;??
Create_Food();???//隨機(jī)產(chǎn)生食物??
for(i?=?0;?i??H;?i++)???//地圖顯示??
{???
for(j?=?0;?j??L;?j++)??
printf("%c",?GameMap[i][j]);??
printf("\n");??
}??
printf("\n小小C語(yǔ)言貪吃蛇\(yùn)n");??
printf("按任意方向鍵開(kāi)始游戲\n");??
getch();???//先接受一個(gè)按鍵,使蛇開(kāi)始往該方向走??
Button();??//取出按鍵,并判斷方向??
}??
void?Create_Food()??//在地圖上隨機(jī)產(chǎn)生食物??
{??
int?fx,?fy;??
while(1)??
{??
fx?=?rand()%H;??
fy?=?rand()%L;??
if(GameMap[fx][fy]?==?'.')??//不能出現(xiàn)在蛇所占有的位置??
{???
GameMap[fx][fy]?=?Sfood;??
break;??
}??
}??
}??
void?Show()??//刷新顯示地圖??
{??
int?i,?j;??
while(1)??
{????
_sleep(500);?//延遲半秒(1000為1s),即每半秒刷新一次地圖??
Button();???//先判斷按鍵在移動(dòng)??
Move();??
if(over)??//自吃或碰墻即游戲結(jié)束??
{???
printf("\n**游戲結(jié)束**\n");??
printf("?????_\n");??
getchar();??
break;??
}??
system("cls");???//清空地圖再顯示刷新吼的地圖??
for(i?=?0;?i??H;?i++)???
{???
for(j?=?0;?j??L;?j++)??
printf("%c",?GameMap[i][j]);??
printf("\n");??
}??
printf("\n小小C語(yǔ)言貪吃蛇\(yùn)n");??
printf("按任意方向鍵開(kāi)始游戲\n");??
}??
}??
void?Button()??//取出按鍵,并判斷方向??
{??
if(kbhit()?!=?0)?//檢查當(dāng)前是否有鍵盤(pán)輸入,若有則返回一個(gè)非0值,否則返回0??
{???
while(kbhit()?!=?0)??//可能存在多個(gè)按鍵,要全部取完,以最后一個(gè)為主??
key?=?getch();?//將按鍵從控制臺(tái)中取出并保存到key中??
switch(key)??
{???//左??
case?75:??Snake[0].now?=?0;??
break;??
//右??
case?77:??Snake[0].now?=?1;???????
break;??
//上??
case?72:??Snake[0].now?=?2;??
break;??
//下??
case?80:??Snake[0].now?=?3;??
break;??
}??
}??
}??
void?Move()???//蛇的移動(dòng)??
{??
int?i,?x,?y;??
int?t?=?sum;??//保存當(dāng)前蛇的長(zhǎng)度??
//記錄當(dāng)前蛇頭的位置,并設(shè)置為空,蛇頭先移動(dòng)??
x?=?Snake[0].x;??y?=?Snake[0].y;??GameMap[x][y]?=?'.';??
Snake[0].x?=?Snake[0].x?+?dx[?Snake[0].now?];??
Snake[0].y?=?Snake[0].y?+?dy[?Snake[0].now?];??
Check_Border();???//蛇頭是否越界??
Check_Head(x,?y);??//蛇頭移動(dòng)后的位置情況,參數(shù)為:?蛇頭的開(kāi)始位置??
if(sum?==?t)??//未吃到食物即蛇身移動(dòng)哦??
for(i?=?1;?i??sum;?i++)??//要從蛇尾節(jié)點(diǎn)向前移動(dòng)哦,前一個(gè)節(jié)點(diǎn)作為參照??
{??
if(i?==?1)???//尾節(jié)點(diǎn)設(shè)置為空再移動(dòng)??
GameMap[?Snake[i].x?][?Snake[i].y?]?=?'.';??
if(i?==?sum-1)??//為蛇頭后面的蛇身節(jié)點(diǎn),特殊處理??
{??
Snake[i].x?=?x;??
Snake[i].y?=?y;??
Snake[i].now?=?Snake[0].now;??
}??
else???//其他蛇身即走到前一個(gè)蛇身位置??
{??
Snake[i].x?=?Snake[i+1].x;??
Snake[i].y?=?Snake[i+1].y;??
Snake[i].now?=?Snake[i+1].now;??
}??
GameMap[?Snake[i].x?][?Snake[i].y?]?=?'#';?//移動(dòng)后要置為'#'蛇身???
}??
}??
void?Check_Border()??//檢查蛇頭是否越界??
{??
if(Snake[0].x??0?||?Snake[0].x?=?H??
||?Snake[0].y??0?||?Snake[0].y?=?L)??
over?=?1;??
}??
void?Check_Head(int?x,?int?y)??//檢查蛇頭移動(dòng)后的位置情況??
{??
if(GameMap[?Snake[0].x?][?Snake[0].y?]?==?'.')??//為空??
GameMap[?Snake[0].x?][?Snake[0].y?]?=?'@';??
else?
if(GameMap[?Snake[0].x?][?Snake[0].y?]?==?'*')??//為食物??
{??
GameMap[?Snake[0].x?][?Snake[0].y?]?=?'@';????
Snake[sum].x?=?x;???//新增加的蛇身為蛇頭后面的那個(gè)??
Snake[sum].y?=?y;??
Snake[sum].now?=?Snake[0].now;??
GameMap[?Snake[sum].x?][?Snake[sum].y?]?=?'#';???
sum++;??
Create_Food();??//食物吃完了馬上再產(chǎn)生一個(gè)食物??
}??
else?
over?=?1;??
}
/*
C/C++貪吃蛇游戲,zjlj,2015.3.16
*/
#define DEBUG 0 //當(dāng)程序在調(diào)試階段時(shí) DEBUG為 1
#includeiostream
#includewindows.h
#includetime.h
#includeconio.h
using namespace std;
void readini(FILE **fphead, int *score, char *argv[]) //創(chuàng)建或打開(kāi)一個(gè)和運(yùn)行文件對(duì)應(yīng)的ini文件,讀取最高紀(jì)錄
{
char filename[200],*pfilename;
int flag=-1,i;
strcpy(filename,argv[0]);
for(i=0;filename[i]!='\0';i++)
{
if ('.'==filename[i])flag=1;
}
if(1==flag)
{
filename[i-1]='i';
filename[i-2]='n';
filename[i-3]='i';
}
else
{
filename[i]='.';
filename[i+1]='i';
filename[i+2]='n';
filename[i+3]='i';
filename[i+4]='\0';
}
for(;filename[i]!='\\'i=0;i--)pfilename=filename[i];
if ( (*fphead=fopen(pfilename, "rb+"))==NULL)
{
if ( (*fphead=fopen(pfilename, "wb+"))==NULL)
{
printf("無(wú)法創(chuàng)建或打開(kāi)\"%s\"文件\n",pfilename);
system("pause");
exit(0);
}
}
else
{
fread(score,sizeof(int),1,*fphead);
}
}
void writeini(FILE **fphead, int *score, char *argv[]) //打開(kāi)一個(gè)和運(yùn)行文件對(duì)應(yīng)的ini文件,寫(xiě)入最高紀(jì)錄
{
char filename[200],*pfilename;
int flag=-1,i;
strcpy(filename,argv[0]);
for(i=0;filename[i]!='\0';i++)
{
if ('.'==filename[i])flag=1;
}
if(1==flag)
{
filename[i-1]='i';
filename[i-2]='n';
filename[i-3]='i';
}
else
{
filename[i]='.';
filename[i+1]='i';
filename[i+2]='n';
filename[i+3]='i';
filename[i+4]='\0';
}
for(;filename[i]!='\\'i=0;i--)pfilename=filename[i];
if ( (*fphead=fopen(pfilename, "wb+"))==NULL)
{
printf("無(wú)法寫(xiě)入\"%s\"文件,磁盤(pán)寫(xiě)保護(hù)!\n",pfilename);
system("pause");
exit(0);
}
else
{
rewind(*fphead);
fwrite(score,sizeof(int),1,*fphead);
fclose(*fphead);
}
}
void gotoxy(int x,int y)//光標(biāo)定位,光標(biāo)定位函數(shù)SetConsoleCursorPosition是左上角位置是0,0然后向左向下延伸
{
COORD pos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//顏色函數(shù)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void Refresh(int q[][22], int grade, int gamespeed, int length,int score) // 輸出貪吃蛇棋盤(pán)
{
int i,j;
for(i=0;i22;i++)
{
for(j=0;j22;j++)
{
if(q[i][j]==0)//輸出棋盤(pán)空白
{
gotoxy(i,j);
color(11);
cout"■";
}
if(q[i][j]==1||q[i][j]==2)//輸出棋盤(pán)墻壁
{
gotoxy(i,j);
color(11);
cout"□";
}
if(q[i][j]==3)//輸出蛇頭
{
gotoxy(i,j);
color(14);
cout"★";
}
if(q[i][j]==4)//輸出蛇身
{
gotoxy(i,j);
color(12);
cout"◆";
}
if(q[i][j]==5)//輸出果子
{
gotoxy(i,j);
color(12);
cout"●";
}
}
if(i==0) cout "\t***********************";
if(i==1) cout "\t等級(jí)為:" grade;//顯示等級(jí)
if(i==3) cout "\t自動(dòng)前進(jìn)時(shí)間";
if(i==4) cout "\t間隔為:" gamespeed "ms";//顯示時(shí)間
if(i==6) cout "\t歷史最高分為:" score "分";
if(i==7) cout "\t你現(xiàn)在得分為:" (length+(grade-1)*8)*10 "分";
if(i==8) cout "\t**********************";
if(i==9) cout "\t游戲說(shuō)明:";
if(i==10) cout "\t(1)用小鍵盤(pán)方向鍵控制";
if(i==11) cout "\t蛇頭運(yùn)動(dòng)方向;";
if(i==12) cout "\t(2)蛇每吃一個(gè)果子蛇身";
if(i==13) cout "\t增加一節(jié);";
if(i==14) cout "\t(3)蛇咬到自己或碰到墻";
if(i==15) cout "\t壁游戲結(jié)束。";
if(i==18) cout "\t**********************";
if(i==19) cout "\tC/C++語(yǔ)言作業(yè):";
if(i==20) cout "\tzjlj,2015.03.16 ";
}
}
int main(int argc, char *argv[]){
int tcsQipan[22][22]; // 貪吃蛇棋盤(pán)是一個(gè)二維數(shù)組(如22*22,包括墻壁)
int i,j,score,directiontemp;
FILE *fpini;//*fpini 信息文件
readini(fpini, score, argv);//讀取ini文件的最高紀(jì)錄
if (score0)//最高成績(jī)小于零設(shè)置為零,初建文件會(huì)是負(fù)數(shù)
score=0;
while(1)
{
for(i=1;i=20;i++)
for(j=1;j=20;j++)
tcsQipan[i][j]=0; //貪吃蛇棋盤(pán)相應(yīng)坐標(biāo)標(biāo)上中間空白部分的標(biāo)志0
for(i=0;i=21;i++)
tcsQipan[0][i] = tcsQipan[21][i] = 1; //貪吃蛇棋盤(pán)相應(yīng)坐標(biāo)標(biāo)上上下墻壁的標(biāo)志1
for(i=1;i=20;i++)
tcsQipan[i][0] = tcsQipan[i][21] = 2; //貪吃蛇棋盤(pán)相應(yīng)坐標(biāo)標(biāo)上左右墻壁的標(biāo)志2
int tcsZuobiao[2][500]; //蛇的坐標(biāo)數(shù)組
for(i=0; i4; i++)
{
tcsZuobiao[0][i] = 1;//蛇身和蛇頭的x坐標(biāo)
tcsZuobiao[1][i] = i + 1;//蛇身和蛇頭的y坐標(biāo)
}
int head = 3,tail = 0;//標(biāo)示蛇頭和蛇尾的數(shù)組偏移量
for(i=1;i=3;i++)
tcsQipan[1][i]=4; //蛇身
tcsQipan[1][4]=3; //蛇頭
int x1, y1; // 隨機(jī)出果子
srand(time(0));//設(shè)置隨機(jī)種子
do
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白處重新出果子
tcsQipan[x1][y1]=5;//貪吃蛇棋盤(pán)相應(yīng)坐標(biāo)標(biāo)上果子的標(biāo)志5
color(12);
cout"\n\n\t\t\t\t貪吃蛇游戲即將開(kāi)始 !"endl;//準(zhǔn)備開(kāi)始
long start,starttemp;
int grade = 1, length = 4; //設(shè)置初始等級(jí)和蛇的初始長(zhǎng)度
int gamespeed = 500; //設(shè)置初始前進(jìn)時(shí)間間隔
for(i=3;i=0;i--)
{
start=clock();
while(clock()-start=1000);
system("cls");
if(i0)
cout "\n\n\t\t\t\t進(jìn)入倒計(jì)時(shí):" i endl; //倒計(jì)時(shí)顯示
else
Refresh(tcsQipan,grade,gamespeed,length,score); //初始棋盤(pán)顯示
}
int timeover=1,otherkey=1;//初始化超時(shí)時(shí)間和按鍵判斷參數(shù)
char direction = 77; // 設(shè)置初始情況下,向右運(yùn)動(dòng)
int x=tcsZuobiao[0][head],y=tcsZuobiao[1][head];//保存蛇頭坐標(biāo)到x,y變量
while(1)//運(yùn)行一局游戲
{
start = clock();
while((timeover=((starttemp=clock())-start=gamespeed))!kbhit());//如果有鍵按下或時(shí)間超過(guò)自動(dòng)前進(jìn)時(shí)間間隔則終止循環(huán)
if(direction==72||direction==80||direction==75 ||direction==77)
directiontemp=direction;//保留上一次方向按鍵
//starttemp=gamespeed+start-starttemp;//保留停留時(shí)間
if(timeover)
{
#if (DEBUG==1)
direction = getch();//調(diào)試代碼
#else
if((direction =getch())==-32)
direction = getch();
#endif
}
#if (DEBUG==1)//調(diào)試代碼
start=clock();
while(clock()-start=2000);
gotoxy(24,4);
cout "\t按鍵ASCII代碼"(int)direction" "endl;
#endif
if(!(direction==72||direction==80||direction==75 ||direction==77))
{
otherkey=0;// 按鍵非方向鍵,otherkey設(shè)置為0
}
else
{
otherkey=1;// 按鍵為方向鍵,otherkey設(shè)置為1
}
if(direction==72 directiontemp==80)//忽略反方向按鍵
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start=starttemp);
}
else if(direction==80 directiontemp==72)
{
direction=32;//設(shè)置按鍵為非方向鍵
otherkey=0;// 按鍵為非方向鍵,otherkey設(shè)置為0
// start = clock();
//while(clock()-start=starttemp);//補(bǔ)償?shù)却龝r(shí)間
}
else if(direction==75 directiontemp==77)
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start=starttemp);
}
else if(direction==77 directiontemp==75)
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start=starttemp);
}
switch(direction)//判斷方向鍵
{
case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上
case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下
case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左
case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;break; // 向右
default: break;
}
if(x==0 || x==21 ||y==0 || y==21) // 蛇頭碰到墻壁,結(jié)束本局游戲
{
gotoxy(22,12);
cout "\t游戲已結(jié)束!" endl;
if(score=(length+(grade-1)*8)*10)//判斷是否破記錄
{
gotoxy(10,7);
color(12);
cout "闖關(guān)失敗 加油耶!" endl;
fclose(fpini);//關(guān)閉ini文件
}
else
{
gotoxy(10,7);
color(12);
cout "恭喜您打破記錄" endl;
score=(length+(grade-1)*8)*10;
writeini(fpini, score, argv);//寫(xiě)入ini文件的最高紀(jì)錄
}
gotoxy(23,12);
cout "按回車(chē)鍵重新開(kāi)始,按ESC退出游戲" endl;//顯示的提示
break;//退出該局游戲
}
if(tcsQipan[x][y]!=0!(x==x1y==y1)tcsQipan[x][y]!=3) // 蛇頭碰到蛇身,結(jié)束本局游戲
{
gotoxy(22,12);
cout "\t游戲已結(jié)束!" endl;
if(score=(length+(grade-1)*8)*10)//判斷是否破記錄
{
gotoxy(10,7);
color(12);
cout "闖關(guān)失敗 加油耶!" endl;
fclose(fpini);//關(guān)閉ini文件
}
else
{
gotoxy(10,7);
color(12);
cout "恭喜您打破記錄" endl;
score=(length+(grade-1)*8)*10;
writeini(fpini, score, argv);//寫(xiě)入ini文件的最高紀(jì)錄
}
gotoxy(23,12);
cout "按回車(chē)鍵重新開(kāi)始,按ESC退出游戲" endl;//顯示的提示
break;//退出該局游戲
}
/*
游戲運(yùn)行時(shí)的核心算法開(kāi)始
*/
if(x==x1 y==y1) // 吃果子,長(zhǎng)度加1
{
length ++;
if(length=8)//長(zhǎng)度大于等于8重新計(jì)算長(zhǎng)度,等級(jí)加1
{
length -= 8;//重新計(jì)算長(zhǎng)度
grade ++;//等級(jí)加1
if(gamespeed50)//控制最快速度為50
gamespeed = 550 - grade * 50; // 改變自動(dòng)前進(jìn)時(shí)間間隔
}
tcsQipan[x][y]= 3;//貪吃蛇棋盤(pán)相應(yīng)坐標(biāo)現(xiàn)在蛇頭標(biāo)志改為蛇頭標(biāo)志3
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = 4;//貪吃蛇棋盤(pán)相應(yīng)坐標(biāo)原來(lái)蛇頭標(biāo)志改為蛇身標(biāo)志4
head = (head+1)%400;//防止數(shù)組越界
tcsZuobiao[0][head] = x;//蛇頭的x坐標(biāo)
tcsZuobiao[1][head] = y;//蛇頭的y坐標(biāo)
do//隨機(jī)出果子
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白處重新出果子
tcsQipan[x1][y1]=5;//貪吃蛇棋盤(pán)相應(yīng)坐標(biāo)標(biāo)上果子的標(biāo)志5
gotoxy(22,12);
cout "\t游戲進(jìn)行中!" endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else // 不吃果子
{
if(otherkey)
{
tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=0;
tail=(tail+1)%400;//防止數(shù)組越界
tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;
head=(head+1)%400;//防止數(shù)組越界
tcsZuobiao[0][head]=x;//蛇頭的x坐標(biāo)
tcsZuobiao[1][head]=y;//蛇頭的y坐標(biāo)
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=3;
gotoxy(22,12);
cout "\t游戲進(jìn)行中!" endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else
{
gotoxy(22,12);
cout "\t游戲暫停中!" endl;
}
}
/*
游戲運(yùn)行時(shí)的核心算法結(jié)束
*/
}
while(1)
{
while(!kbhit());
if((direction =getch())==13)//按回車(chē)鍵開(kāi)始下一局
break;
if(direction ==27)//按ESC退出游戲
exit(0);
}
system("cls");//清除屏幕重新開(kāi)始
}
return 0;
}
我以實(shí)現(xiàn)一個(gè)~~請(qǐng)笑納~#include "stdio.h"
#include "graphics.h"
#include "stdlib.h"
#include "dos.h"
#define N 200
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define Esc 0x011b
int i,key;
int score=0;
int gamespeed=50000;struct Food
{
int x;
int y;
int yes;
}food;
struct Snake
{
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;void Init();
void DrawK();
void GamePlay();
void GameOver();
void PrScore();
void Close();
void main()
{
Init();/*圖形界面驅(qū)動(dòng)*/
DrawK();/*作圍墻*/
GamePlay();
Close();
}
/*圖形界面驅(qū)動(dòng)模塊*/
void Init()
{
int gd=DETECT,gm;
initgraph(gd,gm,"");
}
/*作矩形圍墻*/
void DrawK()
{
setcolor(11);
setlinestyle(0,0,3);
for(i=50;i=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i=450;i+=10)
{
rectangle(50,i,59,i+10);
rectangle(601,i,610,i+10);
}
getch();
}void GamePlay()
{
randomize();
/*初始狀態(tài)*/
food.yes=1;
snake.life=0;
snake.direction=1;
snake.x[0]=100;
snake.y[0]=100;
snake.x[1]=110;
snake.y[1]=100;
snake.node=2;
PrScore();
/*重復(fù)玩游戲模塊*/
while(1)
{
/*沒(méi)有按鍵情況下蛇的運(yùn)動(dòng)*/
while(!kbhit())
{
/*食物的出現(xiàn)*/
if(food.yes==1)
{
food.x=rand()%400+60;
food.y=rand()%350+60;
/*將食物出現(xiàn)位置控制在整格*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*食物位置控制好了就改變狀態(tài)*/
}
/*顯示食物*/
if(food.yes==0)
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
/*蛇的位置變化*/
for(i=snake.node-1;i0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*按方向鍵時(shí)蛇的位置變化*/
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
/*蛇自己與自己相撞時(shí)生命結(jié)束*/
for(i=3;isnake.node;i++)
{
if(snake.x[i]==snake.x[0]snake.y[0]==snake.y[i])
{
GameOver();
snake.life=1;
break;
}
}
/*蛇撞墻時(shí)死亡*/
if(snake.x[0]55||snake.x[0]595||snake.y[0]55||snake.y[0]455)
{
GameOver();
snake.life=1;
}
/*蛇的生命狀態(tài)判斷*/
if(snake.life==1)
break;
/*蛇吃食物*/
if(snake.x[0]==food.xsnake.y[0]==food.y)
{
setcolor(0);
rectangle(food.x,food.y,food.x+=10,food.y-=10);
snake.x[snake.node]=-20;
snake.y[snake.node]=-20;
snake.node++;
food.yes=1;
score+=10;
PrScore();
}
/*畫(huà)蛇*/
setcolor(4);
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
/*去除最后一節(jié)*/
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}/*end while(!kbhit())*/
if(snake.life==1)
break;
key=bioskey(0);
if(key==Esc)
break;
else if(key==UPsnake.direction!=4)
snake.direction=3;
else if(key==RIGHTsnake.direction!=2)
snake.direction=1;
else if(key==LEFTsnake.direction!=1)
snake.direction=2;
else if(key==DOWNsnake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
//游戲結(jié)束
void GameOver()
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}/*打印分?jǐn)?shù)*/
void PrScore()
{
char str[10];
setfillstyle(1,14);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
} /*圖形結(jié)束*/
void Close()
{
getch();
closegraph();
}