當前位置:首頁 » 編程語言 » c語言編的貪吃蛇

c語言編的貪吃蛇

發布時間: 2025-08-28 04:33:28

⑴ 求c語言的貪吃蛇的代碼 能運行的

//ConsoleApplication1.cpp:定義控制台應用程序的入口點。
//

#include"stdafx.h"

#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<conio.h>


#defineframe_height20//definemapsize
#defineframe_width40
#defineUP'w'//defineoperatekey
#defineDOWN's'
#defineLEFT'a'
#defineRIGHT'd'


inti,j,k;
charch=UP;//initialdirection
intgrow=0;//flag:ifsnakegrow


structFood{
intx;
inty;
}food;
structSnake{
intx[50];
inty[50];
intlen;
intspeed;
}snake;//snake[0]ishead


voidinit_map(void);
voipdate_food(void);
voidmove_snake(void);
intis_alive(void);
voidget_speed(void);
voidgotoxy(intx,inty);


intmain()
{
init_map();//初始化地圖
while(1)
{
update_food();
//是否產生食物
get_speed();//獲取速度
move_snake();//移動蛇身
Sleep(snake.speed);
//移動速度
if(!(is_alive()))
//蛇的死活(撞牆或自食)
break;
}
printf("GameOver!");
_getch();


return0;
}
//initialize
voidinit_map(void)
{
//initialfood
srand(time(NULL));
food.x=rand()%(frame_height-2)+1;
food.y=rand()%(frame_width-2)+1;
gotoxy(food.x,food.y);
printf("!");
//initialsnake
snake.x[0]=frame_height/2;
snake.y[0]=frame_width/2;
gotoxy(snake.x[0],snake.y[0]);
printf("@");
snake.len=3;
snake.speed=200;
for(k=1;k<snake.len;k++)
{
snake.x[k]=snake.x[k-1]+1;
snake.y[k]=snake.y[k-1];
gotoxy(snake.x[k],snake.y[k]);
printf("@");
}
//initialbar
for(j=0;j<frame_width;j++)
{
gotoxy(0,j);
printf("#");
gotoxy(frame_height-1,j);
printf("#");
}
for(i=1;i<frame_height-1;i++)
{
gotoxy(i,0);
printf("#");
gotoxy(i,frame_width-1);
printf("#");
}
}
//generatefood
voipdate_food()
{
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
srand(time(NULL));
food.x=rand()%(frame_height-2)+1;
food.y=rand()%(frame_width-2)+1;
gotoxy(food.x,food.y);
printf("!");


snake.len++;
grow=1;
}
}
//movesnake
voidmove_snake()
{
if(_kbhit())
ch=_getch();
if(!grow)
{
gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf("");
}
for(k=snake.len-1;k>0;k--)
{
snake.x[k]=snake.x[k-1];
snake.y[k]=snake.y[k-1];
}
switch(ch)
{
caseUP:snake.x[0]--;break;
caseDOWN:snake.x[0]++;break;
caseLEFT:snake.y[0]--;break;
caseRIGHT:snake.y[0]++;break;
default:break;
}
gotoxy(snake.x[0],snake.y[0]);
printf("@");
grow=0;
gotoxy(frame_height,0);
}
//isalive
intis_alive(void)
{
if(snake.x[0]==0||snake.x[0]==frame_height-1||snake.y[0]==frame_width-1||snake.y[0]==0)
return0;
for(k=1;k<snake.len;k++)
if(snake.x[k]==snake.x[0]&&snake.y[k]==snake.y[0])
return0;
return1;
}
//speep
voidget_speed(void)
{
if(snake.len<=6)
snake.speed=200;
elseif(snake.len<=10)
snake.speed=100;
elseif(snake.len<=20)
snake.speed=50;
elseif(snake.len<=30)
snake.speed=30;
elsesnake.speed=20;
}
//movecursor
voidgotoxy(intx,inty)
{
HANDLEhout;
COORDcor;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
cor.X=y;
cor.Y=x;
SetConsoleCursorPosition(hout,cor);
}

⑵ 如何用C語言寫貪吃蛇

#include<conio.h> #include<graphics.h> #include<time.h> #include<string.h> #include<malloc.h> #include<stdio.h> int grade=5,point=0,life=3; void set(),menu(),move_head(),move_body(),move(),init_insect(),left(),upon(),right(),down(),init_graph(),food_f(),ahead(),crate(); struct bug { int x; int y; struct bug *last; struct bug *next; }; struct fd { int x; int y; int judge; }food={0,0,0}; struct bug *head_f=NULL,*head_l,*p1=NULL,*p2=NULL; void main() { char ch; initgraph(800,600); set(); init_insect(); while(1) { food_f(); Sleep(grade*10); setcolor(BLACK); circle(head_l->x,head_l->y,2); setcolor(WHITE); move_body(); if(kbhit()) { ch=getch(); if(ch==27) { ahead(); set(); } else if(ch==-32) { switch(getch()) { case 72:upon();break; case 80:down();break; case 75:left();break; case 77:right();break; } } else ahead(); } else { ahead(); } if(head_f->x==food.x&&head_f->y==food.y) { Sleep(100); crate(); food.judge=0; point=point+(6-grade)*10; if(food.x<30||food.y<30||food.x>570||food.y>570) life++; menu(); } if(head_f->x<5||head_f->x>595||head_f->y<5||head_f->y>595) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); } for(p1=head_f->next;p1!=NULL;p1=p1->next) { if(head_f->x==p1->x&&head_f->y==p1->y) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); break; } } if(life==0) { outtextxy(280,300,"游戲結束!"); getch(); return; } move(); }; } void init_graph() { clearviewport(); setlinestyle(PS_SOLID,1,5); rectangle(2,2,600,598); setlinestyle(PS_SOLID,1,1); } void set() { init_graph(); outtextxy(640,50,"1、開始 / 返回"); outtextxy(640,70,"2、退出"); outtextxy(640,90,"3、難度"); outtextxy(640,110,"4、重新開始"); switch(getch()) { case '1': menu();setcolor(GREEN);circle(food.x,food.y,2);setcolor(WHITE);return; case '2': exit(0);break; case '3': outtextxy(700,90,":1 2 3 4 5");grade=getch()-48;set();break; case '4': food.judge=0,grade=5;point=0;life=3;init_insect();menu();break; default: outtextxy(250,300,"輸入錯誤!"); set();break; } } void menu() { char str[20],str1[]={"分數:"},str2[]={"難度:"},str3[]={"生命值:"}; init_graph(); sprintf(str,"%d",point); strcat(str1,str); outtextxy(640,50,str1); sprintf(str,"%d",grade); strcat(str2,str); outtextxy(640,70,str2); sprintf(str,"%d",life); strcat(str3,str); outtextxy(640,90,str3); outtextxy(640,110,"設置:ESC"); } void init_insect() { head_f=(struct bug *)malloc(sizeof(struct bug)); head_f->last=NULL; head_f->x=300; head_f->y=300; p2=head_f->next=p1=(struct bug *)malloc(sizeof(struct bug)); p1->last=head_f; p1->x=295; p1->y=300; p1=p1->next=(struct bug *)malloc(sizeof(struct bug)); p1->next=NULL; p1->x=290; p1->y=300; p1->last=p2; head_l=p1; } void move() { for(p1=head_f;p1!=NULL;p1=p1->next) { circle(p1->x,p1->y,2); } } void move_head() { } void move_body() { for(p1=head_l,p2=p1->last;p2!=NULL;p1=p2,p2=p2->last) { p1->x=p2->x; p1->y=p2->y; } } void ahead() { p1=head_f; p2=p1->next; p2=p2->next; if(p1->x==p2->x) { if(p1->y>p2->y) head_f->y+=5; else head_f->y-=5; } else { if(p1->x>p2->x) { head_f->x+=5; } else head_f->x-=5; } } void upon() { p1=head_f->next; p1=p1->next; head_f->y-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y+=5; ahead(); } } void down() { p1=head_f->next; p1=p1->next; head_f->y+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->y-=5; ahead(); } } void left() { p1=head_f->next; p1=p1->next; head_f->x-=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x+=5; ahead(); } } void right() { p1=head_f->next; p1=p1->next; head_f->x+=5; if(p1->x==head_f->x&&p1->y==head_f->y) { head_f->x-=5; ahead(); } } void food_f() { if(!food.judge) { food.x=(rand()%117+1)*5; food.y=(rand()%117+1)*5; food.judge=1; if(food.x<30||food.y<30||food.x>570||food.y>570) { setcolor(RED); circle(f

⑶ c語言貪吃蛇源代碼怎麼用

C語言貪吃蛇源代碼必須經過相應的C/C++編譯器編譯成EXE文件後才能運行。

由於我們通常使用的操作系統是Windows系統,而在該系統下最長用的C/C++編譯器是VC++編譯器,目前在大專院校常用的版本還是VC++6.0

下面就以VC++6.0來說明編譯過程:

1.在VC++6.0中通過「File」菜單下的 「Open」子菜單打開貪吃蛇代碼

/*
C/C++貪吃蛇游戲,zjlj,2015.3.16
*/
#defineDEBUG0//當程序在調試階段時DEBUG為1
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
usingnamespacestd;
voidreadini(FILE**fphead,int*score,char*argv[])//創建或打開一個和運行文件對應的ini文件,讀取最高紀錄
{
charfilename[200],*pfilename;
intflag=-1,i;

strcpy(filename,argv[0]);
for(i=0;filename[i]!='';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]='';
}
for(;filename[i]!='\'&&i>=0;i--)pfilename=&filename[i];
if((*fphead=fopen(pfilename,"rb+"))==NULL)
{
if((*fphead=fopen(pfilename,"wb+"))==NULL)
{
printf("無法創建或打開"%s"文件 ",pfilename);
system("pause");
exit(0);
}
}
else
{
fread(score,sizeof(int),1,*fphead);
}
}
voidwriteini(FILE**fphead,int*score,char*argv[])//打開一個和運行文件對應的ini文件,寫入最高紀錄
{
charfilename[200],*pfilename;
intflag=-1,i;

strcpy(filename,argv[0]);
for(i=0;filename[i]!='';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]='';
}
for(;filename[i]!='\'&&i>=0;i--)pfilename=&filename[i];
if((*fphead=fopen(pfilename,"wb+"))==NULL)
{
printf("無法寫入"%s"文件,磁碟防寫! ",pfilename);
system("pause");
exit(0);
}
else
{
rewind(*fphead);
fwrite(score,sizeof(int),1,*fphead);
fclose(*fphead);
}
}
voidgotoxy(intx,inty)//游標定位,游標定位函數SetConsoleCursorPosition是左上角位置是0,0然後向左向下延伸
{
COORDpos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
voidcolor(inta)//顏色函數
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}

voidRefresh(intq[][22],intgrade,intgamespeed,intlength,intscore)//輸出貪吃蛇棋盤
{
inti,j;
for(i=0;i<22;i++)
{
for(j=0;j<22;j++)
{
if(q[i][j]==0)//輸出棋盤空白
{
gotoxy(i,j);
color(11);
cout<<"■";
}
if(q[i][j]==1||q[i][j]==2)//輸出棋盤牆壁
{
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<<" ***********************";
if(i==1)cout<<" 等級為:"<<grade;//顯示等級
if(i==3)cout<<" 自動前進時間";
if(i==4)cout<<" 間隔為:"<<gamespeed<<"ms";//顯示時間
if(i==6)cout<<" 歷史最高分為:"<<score<<"分";
if(i==7)cout<<" 你現在得分為:"<<(length+(grade-1)*8)*10<<"分";
if(i==8)cout<<" **********************";
if(i==9)cout<<" 游戲說明:";
if(i==10)cout<<" (1)用小鍵盤方向鍵控制";
if(i==11)cout<<" 蛇頭運動方向;";
if(i==12)cout<<" (2)蛇每吃一個果子蛇身";
if(i==13)cout<<" 增加一節;";
if(i==14)cout<<" (3)蛇咬到自己或碰到牆";
if(i==15)cout<<" 壁游戲結束。";
if(i==18)cout<<" **********************";
if(i==19)cout<<" C/C++語言作業:";
if(i==20)cout<<" zjlj,2015.03.16";
}
}

intmain(intargc,char*argv[]){
inttcsQipan[22][22];//貪吃蛇棋盤是一個二維數組(如22*22,包括牆壁)
inti,j,score,directiontemp;
FILE*fpini;//*fpini信息文件
readini(&fpini,&score,argv);//讀取ini文件的最高紀錄
if(score<0)//最高成績小於零設置為零,初建文件會是負數
score=0;
while(1)
{
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
tcsQipan[i][j]=0;//貪吃蛇棋盤相應坐標標上中間空白部分的標志0
for(i=0;i<=21;i++)
tcsQipan[0][i]=tcsQipan[21][i]=1;//貪吃蛇棋盤相應坐標標上上下牆壁的標志1
for(i=1;i<=20;i++)
tcsQipan[i][0]=tcsQipan[i][21]=2;//貪吃蛇棋盤相應坐標標上左右牆壁的標志2
inttcsZuobiao[2][500];//蛇的坐標數組
for(i=0;i<4;i++)
{
tcsZuobiao[0][i]=1;//蛇身和蛇頭的x坐標
tcsZuobiao[1][i]=i+1;//蛇身和蛇頭的y坐標
}
inthead=3,tail=0;//標示蛇頭和蛇尾的數組偏移量
for(i=1;i<=3;i++)
tcsQipan[1][i]=4;//蛇身
tcsQipan[1][4]=3;//蛇頭
intx1,y1;//隨機出果子
srand(time(0));//設置隨機種子
do
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白處重新出果子
tcsQipan[x1][y1]=5;//貪吃蛇棋盤相應坐標標上果子的標志5
color(12);
cout<<" 貪吃蛇游戲即將開始!"<<endl;//准備開始
longstart,starttemp;
intgrade=1,length=4;//設置初始等級和蛇的初始長度
intgamespeed=500;//設置初始前進時間間隔
for(i=3;i>=0;i--)
{
start=clock();
while(clock()-start<=1000);
system("cls");
if(i>0)
cout<<" 進入倒計時:"<<i<<endl;//倒計時顯示
else
Refresh(tcsQipan,grade,gamespeed,length,score);//初始棋盤顯示
}
inttimeover=1,otherkey=1;//初始化超時時間和按鍵判斷參數
chardirection=77;//設置初始情況下,向右運動
intx=tcsZuobiao[0][head],y=tcsZuobiao[1][head];//保存蛇頭坐標到x,y變數
while(1)//運行一局游戲
{
start=clock();
while((timeover=((starttemp=clock())-start<=gamespeed))&&!kbhit());//如果有鍵按下或時間超過自動前進時間間隔則終止循環
if(direction==72||direction==80||direction==75||direction==77)
directiontemp=direction;//保留上一次方向按鍵
//starttemp=gamespeed+start-starttemp;//保留停留時間
if(timeover)
{
#if(DEBUG==1)
direction=getch();//調試代碼
#else
if((direction=getch())==-32)
direction=getch();
#endif
}
#if(DEBUG==1)//調試代碼
start=clock();
while(clock()-start<=2000);
gotoxy(24,4);
cout<<" 按鍵ASCII代碼"<<(int)direction<<""<<endl;
#endif
if(!(direction==72||direction==80||direction==75||direction==77))
{
otherkey=0;//按鍵非方向鍵,otherkey設置為0
}
else
{
otherkey=1;//按鍵為方向鍵,otherkey設置為1
}
if(direction==72&&directiontemp==80)//忽略反方向按鍵
{
direction=32;
otherkey=0;
//start=clock();
//while(clock()-start<=starttemp);
}
elseif(direction==80&&directiontemp==72)
{
direction=32;//設置按鍵為非方向鍵
otherkey=0;//按鍵為非方向鍵,otherkey設置為0
//start=clock();
//while(clock()-start<=starttemp);//補償等待時間
}
elseif(direction==75&&directiontemp==77)
{
direction=32;
otherkey=0;
//start=clock();
//while(clock()-start<=starttemp);
}
elseif(direction==77&&directiontemp==75)
{
direction=32;
otherkey=0;
//start=clock();
//while(clock()-start<=starttemp);
}


switch(direction)//判斷方向鍵
{
case72:x=tcsZuobiao[0][head]-1;y=tcsZuobiao[1][head];break;//向上
case80:x=tcsZuobiao[0][head]+1;y=tcsZuobiao[1][head];break;//向下
case75:x=tcsZuobiao[0][head];y=tcsZuobiao[1][head]-1;break;//向左
case77:x=tcsZuobiao[0][head];y=tcsZuobiao[1][head]+1;break;//向右
default:break;
}


if(x==0||x==21||y==0||y==21)//蛇頭碰到牆壁,結束本局游戲
{
gotoxy(22,12);
cout<<" 游戲已結束!"<<endl;
if(score>=(length+(grade-1)*8)*10)//判斷是否破記錄
{
gotoxy(10,7);
color(12);
cout<<"闖關失敗加油耶!"<<endl;
fclose(fpini);//關閉ini文件
}
else
{
gotoxy(10,7);
color(12);
cout<<"恭喜您打破記錄"<<endl;
score=(length+(grade-1)*8)*10;
writeini(&fpini,&score,argv);//寫入ini文件的最高紀錄
}
gotoxy(23,12);
cout<<"按回車鍵重新開始,按ESC退出遊戲"<<endl;//顯示的提示
break;//退出該局游戲
}
if(tcsQipan[x][y]!=0&&!(x==x1&&y==y1)&&tcsQipan[x][y]!=3)//蛇頭碰到蛇身,結束本局游戲
{
gotoxy(22,12);
cout<<" 游戲已結束!"<<endl;
if(score>=(length+(grade-1)*8)*10)//判斷是否破記錄
{
gotoxy(10,7);
color(12);
cout<<"闖關失敗加油耶!"<<endl;
fclose(fpini);//關閉ini文件
}
else
{
gotoxy(10,7);
color(12);
cout<<"恭喜您打破記錄"<<endl;
score=(length+(grade-1)*8)*10;
writeini(&fpini,&score,argv);//寫入ini文件的最高紀錄
}
gotoxy(23,12);
cout<<"按回車鍵重新開始,按ESC退出遊戲"<<endl;//顯示的提示
break;//退出該局游戲
}
/*
游戲運行時的核心演算法開始
*/
if(x==x1&&y==y1)//吃果子,長度加1
{
length++;
if(length>=8)//長度大於等於8重新計算長度,等級加1
{
length-=8;//重新計算長度
grade++;//等級加1
if(gamespeed>50)//控制最快速度為50
gamespeed=550-grade*50;//改變自動前進時間間隔
}
tcsQipan[x][y]=3;//貪吃蛇棋盤相應坐標現在蛇頭標志改為蛇頭標志3
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;//貪吃蛇棋盤相應坐標原來蛇頭標志改為蛇身標志4
head=(head+1)%400;//防止數組越界
tcsZuobiao[0][head]=x;//蛇頭的x坐標
tcsZuobiao[1][head]=y;//蛇頭的y坐標
do//隨機出果子
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白處重新出果子
tcsQipan[x1][y1]=5;//貪吃蛇棋盤相應坐標標上果子的標志5
gotoxy(22,12);
cout<<" 游戲進行中!"<<endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else//不吃果子
{
if(otherkey)
{
tcsQipan[tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=0;
tail=(tail+1)%400;//防止數組越界
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;
head=(head+1)%400;//防止數組越界
tcsZuobiao[0][head]=x;//蛇頭的x坐標
tcsZuobiao[1][head]=y;//蛇頭的y坐標
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=3;
gotoxy(22,12);
cout<<" 游戲進行中!"<<endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else
{
gotoxy(22,12);
cout<<" 游戲暫停中!"<<endl;
}
}
/*
游戲運行時的核心演算法結束
*/
}
while(1)
{
while(!kbhit());
if((direction=getch())==13)//按回車鍵開始下一局
break;
if(direction==27)//按ESC退出遊戲
exit(0);
}
system("cls");//清除屏幕重新開始
}
return0;
}

熱點內容
真實戰爭安卓手機怎麼安裝內存卡 發布:2025-08-28 06:22:52 瀏覽:172
上海雲伺服器訂餐 發布:2025-08-28 06:22:46 瀏覽:920
購買伺服器如何付款 發布:2025-08-28 06:22:10 瀏覽:966
FTP獵頭 發布:2025-08-28 05:55:29 瀏覽:486
顆粒壓壓縮機 發布:2025-08-28 05:54:08 瀏覽:602
數控編程F的意思 發布:2025-08-28 05:31:58 瀏覽:259
python的平方 發布:2025-08-28 05:20:35 瀏覽:643
東莞壓縮彈簧廠 發布:2025-08-28 05:14:06 瀏覽:792
c語言inout 發布:2025-08-28 05:02:57 瀏覽:917
java父類訪問子類變數么 發布:2025-08-28 04:54:20 瀏覽:404