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

貪吃蛇c語言vc

發布時間: 2022-10-10 02:48:40

㈠ 求在VC++6.0中運行的貪吃蛇代碼

#包括< stdio, h >

#包括< process。H >

#包括< Windows。H >

#包括< conio。H >

#包括<時間。H >

#包括< stdlib。H >

#defineWIDTH40

#defineHEIGH12

枚舉方向{//方向

離開了,

對的,

向上

};

StructFood{//食品

Intx;

Inty;

};

{//繪制蛇體

intx;

inty;

structNode*next;

};

structSnake{//蛇屬性

intlenth;//長度

enumdirectiondir;//方向

};

structFood*food;//食物

structSnake*snake;//蛇屬性

structNode*snode,*tail;//蛇身

intSPEECH=200;

intscore=0;//分數

intsmark=0;//吃食物標記

inttimes=0;

intSTOP=0;

voidInitfood();//產生食物

voidInitsnake();//構造snake

voidEatfood();//頭部前進

voidAddnode(intx,inty);//增加蛇身

voiddisplay(structNode*shead);//顯示蛇身坐標

voidmove();//蛇移動

voiddraw();//畫蛇

voidHomepage();//主頁

voidkeybordhit();//監控鍵盤按鍵

voidAddtail();//吃到食物

voidgotoxy(intx,inty)//定位游標

COORDpos;

pos.X=x-1;

pos.Y=y-1;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);

voidInitsnake()//構造snake

inti;

snake=(structSnake*)malloc(sizeof(structSnake));

tail=(structNode*)malloc(sizeof(structNode));

food=(structFood*)malloc(sizeof(structFood));

snake->lenth=5;//初始長度 5

snake->dir=RIGHT;//初始蛇頭方向 右

for(i=2;i<=snake->lenth+2;i++)//增加 5 個結點

Addnode(i,2);

voidInitfood()//產生食物

structNode*p=snode;

intmark=1;

srand((unsigned)time(NULL));//以時間為種子產生隨機數

while(1)

food->x=rand()%(WIDTH-2)+2;//食物X坐標

food->y=rand()%(HEIGH-2)+2;//食物Y坐標

while(p!=NULL)

if((food->x==p->x)&&(food->y==p->y))//如果食物產生在蛇身上

{//則重新生成食物

mark=0;//食物生成無效

break;

p=p->next;

if(mark==1)//如果食物不在蛇身上,生成食物,否則重新生成食物

gotoxy(food->x,food->y);

printf("%c",3);

break;

mark=1;

p=snode;

voidmove()//移動

structNode*q,*p=snode;

if(snake->dir==RIGHT)

Addnode(p->x+1,p->y);

if(smark==0)

while(p->next!=NULL)

q=p;

p=p->next;

q->next=NULL;

free(p);

if(snake->dir==LEFT)

Addnode(p->x-1,p->y);

if(smark==0)

while(p->next!=NULL)

q=p;

p=p->next;

q->next=NULL;

free(p);

if(snake->dir==UP)

Addnode(p->x,p->y-1);

if(smark==0)

while(p->next!=NULL)

q=p;

p=p->next;

q->next=NULL;

free(p);

if(snake->dir==DOWN)

Addnode(p->x,p->y+1);

if(smark==0)

while(p->next!=NULL)

q=p;

p=p->next;

q->next=NULL;

free(p);

voidAddnode(intx,inty)//增加蛇身

structNode*newnode=(structNode*)malloc(sizeof(structNode));

structNode*p=snode;

newnode->next=snode;

newnode->x=x;

newnode->y=y;

snode=newnode;//結點加到蛇頭

if(x<2||x>=WIDTH||y<2||y>=HEIGH)//碰到邊界

STOP=1;

gotoxy(10,19);

printf("撞牆,游戲結束,任意鍵退出!\n");//失敗

_getch();

free(snode);//釋放內存

free(snake);

exit(0);

while(p!=NULL)//碰到自身

if(p->next!=NULL)

if((p->x==x)&&(p->y==y))

STOP=1;

gotoxy(10,19);

printf("撞到自身,游戲結束,任意鍵退出!\n");//失敗

_getch();

free(snode);//釋放內存

free(snake);

exit(0);

p=p->next;

voidEatfood()//吃到食物

Addtail();

score++;

voidAddtail()//增加蛇尾

structNode*newnode=(structNode*)malloc(sizeof(structNode));

structNode*p=snode;

tail->next=newnode;

newnode->x=50;

newnode->y=20;

newnode->next=NULL;//結點加到蛇頭

tail=newnode;//新的蛇尾

voiddraw()//畫蛇

structNode*p=snode;

inti,j;

while(p!=NULL)

gotoxy(p->x,p->y);

printf("%c",2);

tail=p;

p=p->next;

if(snode->x==food->x&&snode->y==food->y)//蛇頭坐標等於食物坐標

smark=1;

Eatfood();//增加結點

Initfood();//產生食物

if(smark==0)

gotoxy(tail->x,tail->y);//沒吃到食物清除之前的尾結點

printf("%c",'');//如果吃到食物,不清楚尾結點

else

times=1;

if((smark==1)&&(times==1))

gotoxy(tail->x,tail->y);//沒吃到食物清除之前的尾結點

printf("%c",'');//如果吃到食物,不清楚尾結點

smark=0;

gotoxy(50,12);

printf("食物: %d,%d",food->x,food->y);

gotoxy(50,5);

printf("分數:%d",score);

gotoxy(50,7);

printf("速度:%d",SPEECH);

gotoxy(15,14);

printf("按o鍵加速");

gotoxy(15,15);

printf("按p鍵減速");

gotoxy(15,16);

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

voidHideCursor()//隱藏游標

CONSOLE_CURSOR_INFOcursor_info={1,0};

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

voidHomepage()//繪主頁

intx,y;

HideCursor();//隱藏游標

printf("----------------------------------------\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("|\t\t\t\t|\n");

printf("----------------------------------------\n");

gotoxy(5,13);

printf("任意鍵開始游戲!按W.A.S.D控制方向");

_getch();

Initsnake();

Initfood();

gotoxy(5,13);

printf("");

voidkeybordhit()//監控鍵盤

charch;

if(_kbhit())

ch=getch();

switch(ch)

case'W':

case 'w':if(snake->dir==DOWN)//如果本來方向是下,而按相反方向無效

break;

else

snake->dir=UP;break;

case'A':

case 'a':if(snake->dir==RIGHT)//如果本來方向是右,而按相反方向無效

break;

else

snake->dir=LEFT;break;

case'S':

case 's':if(snake->dir==UP)//如果本來方向是上,而按相反方向無效

break;

else

snake->dir=DOWN;break;

case'D':

case 'd':if(snake->dir==LEFT)//如果本來方向是左,而按相反方向無效

break;

else

snake->dir=RIGHT;break;

case'O':

case'o':

if(SPEECH>=150)//速度加快

SPEECH=SPEECH-50;

break;

case'P':

case'p':

if(SPEECH<=400)//速度減慢

SPEECH=SPEECH+50;

break;

case''://暫停

gotoxy(15,18);

printf("游戲已暫停,按任意鍵恢復游戲");

system("pause>nul");

gotoxy(15,18);

printf("");

break;

default:break;

intmain(void)//程序入口

Homepage();

while(!STOP)

keybordhit();//監控鍵盤按鍵

move();//蛇的坐標變化

draw();//蛇的重繪

Sleep(SPEECH);//暫時掛起線程

return0;

(1)貪吃蛇c語言vc擴展閱讀:

注意事項:

1.代碼將源信息轉換為易於通信或存儲的符號。解碼(解碼)是還原和解碼的過程,它將代碼符號轉換為接受者能夠理解的形式。

2.編碼的原因之一是為了在普通語言(口頭或書面)難以實現的情況下進行交流。例如,一面旗幟可以用一個特定的標記來表達一個特定的信息,而站在遠處的另一個人可以解釋標記來重現該信息。

㈡ c語言用vc編寫的貪吃蛇怎麼實現未點擊按鍵時,按原來方向自動移動。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#include<conio.h>
//#defineKEYCODE
//#defineTESTSNAKE
#definemaxx23
#definemaxy38
typedefstructSnake//定義蛇結構體
{
intx;
inty;
structSnake*next;
}snake;
snake*head=NULL;//初始化頭尾指針
snake*tail=NULL;
snake*egg=NULL;
vx=1;
vy=0;
intspeed=5;
voidcheckkey();
voidPos();//設置游標函數
voidStartgame();//游戲開始
voidGamecircle();//游戲循環
voidCreatmap();//創建地圖
voidInisnake();//初始化蛇身
voidPrintsnake();//列印蛇
voidSnakemove();//蛇移動
voidDeletetail();//去頭
voidDessnake();
intnewegg();
voidprintegg();
inteategg();
voidmain()
{
#ifdefKEYCODE
checkkey();
#endif
Startgame();
Gamecircle();
Dessnake();
Pos(maxx+2,4);
system("pause");
}
voidPos(inty,intx)//設置控制台游標位置
{
HANDLEhOutput;//建立一個句柄
COORDpos;//定義游標位置
pos.X=x<<1;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);//獲取句柄
SetConsoleCursorPosition(hOutput,pos);//在顯示器上顯示游標地址
}
voidStartgame()
{
srand(time(0));
Pos(5,3);
printf("歡迎來到貪吃蛇的世界! ");
Pos(7,6);
printf("用↑↓←→分別控制蛇的移動,");
Pos(8,6);
printf("Ctrl+↑為加速,Ctrl+↓為減速。 ");
system("pause");
system("cls");
Creatmap();
Inisnake();
newegg();
Printsnake();
}
voidtestsnake()
{snake*t;
for(t=tail;t;t=t->next)
if(t==0xcdcdcdcd)
{
printf(" 數據完整性破壞 系統將退出");t=0;
}
}
voidGamecircle()
{intch,tick=1;
while(1)
{
Sleep(25);
if(kbhit()){
ch=getch();
if(ch==27)break;//ESC鍵退出
if(ch==224)ch=getch();elsecontinue;
switch(ch){
case72:{if(vy==0)vy=-1;vx=0;break;}//↑
case80:{if(vy==0)vy=1;vx=0;break;}//↓
case77:{if(vx==0)vx=1;vy=0;break;}//←
case75:{if(vx==0)vx=-1;vy=0;break;}//→
case141:{if(speed>1)speed--;break;}//Ctrl+↑為加速,
case145:{if(speed<20)speed++;break;}//Ctrl+↓為減速
default:;
}
}
if(!--tick){
if(!eategg())//如果不是吃掉了蛋就移動
{
Snakemove();//蛇移動
Deletetail();//去蛇尾
printegg();
}
tick=speed;
}
}
}
voidCreatmap()
{
inti;
for(i=0;i<=maxy+1;i++)//列印上下邊框(29個格子)
{
Pos(0,i);
printf("■");
Pos(maxx+1,i);
printf("■");
}
for(i=1;i<maxx+1;i++)//列印左右邊框(25個格子,不算重疊)
{
Pos(i,0);
printf("■");
}
for(i=1;i<maxx+1;i++)
{
Pos(i,maxy+1);
printf("■");
}
}
voidInisnake()//初始化蛇身(尾添加)
{
inti;
snake*pTemp;//建立第一個節點
pTemp=(snake*)malloc(sizeof(snake));
pTemp->x=12;
pTemp->y=5;
if(NULL==head)//節點接入鏈表
{
head=pTemp;
tail=pTemp;
}
for(i=1;i<3;i++)//後面節點接入
{
pTemp=(snake*)malloc(sizeof(snake));
pTemp->x=12+i;
pTemp->y=5;
head->next=pTemp;
head=pTemp;
}
head->next=NULL;//最後一個節點next置空
}
voidDessnake()//初始化蛇身(尾添加)
{snake*t;
while(tail!=NULL)//還有節點
{
t=tail->next;
free(tail);
tail=t;
}
if(egg)free(egg);
}
voidPrintsnake()//輸出蛇
{
snake*pTempPrint=tail;
while(pTempPrint!=NULL)
{
Pos(pTempPrint->x,pTempPrint->y);
printf("■");
pTempPrint=pTempPrint->next;
}
}
voidSnakemove()//蛇前進(添新頭去尾)
{
snake*Newhead;
intx=head->x,y=head->y;
intdelt=0;
if(vx){
y+=vx;
if(y>maxy){
delt=y-maxy;
y=maxy;
}
if(y<1){
delt=1-y;
y=1;
}
if(delt)
{
vx=abs(vx);
vy=((x>maxx/2)?-vx:vx);
x+=((x>maxx/2)?-delt:delt);
vx=0;
}
}
else{
x+=vy;
if(x>maxx){
delt=x-maxx;
x=maxx;
}
if(x<1){
delt=1-x;
x=1;
}
if(delt)
{
vy=abs(vy);
vx=((y>maxy/2)?-vy:vy);
y+=((y>maxy/2)?-delt:delt);
vy=0;
}
}
Newhead=(snake*)malloc(sizeof(snake));//添加新尾

Newhead->x=x;
Newhead->y=y;
Newhead->next=NULL;
head->next=Newhead;//先連後斷
head=Newhead;
}
voidprintegg()
{
Pos(egg->x,egg->y);
printf("■");
}
intnewegg()
{snake*t;inti;
if(!(egg=(snake*)malloc(sizeof(snake))))
return0;
for(i=0;i<10000;i++){
egg->x=rand()%maxx+1;
egg->y=rand()%maxy+1;
for(t=tail;t;t=t->next)
if(t->x==egg->x&&t->y==egg->y)
break;
if(!t)break;
}
if(i>=10000)
return0;
egg->next=NULL;
printegg();
return1;
}
inteategg()
{
intx=head->x,y=head->y;
if((x+vy==egg->x)&&(y+vx==egg->y))
{
head->next=egg;
head=egg;
newegg();
return1;
}
elsereturn0;
}
voidDeletetail()//去尾
{
snake*p,*p1,*p2;
p1=tail;//p2=NULL;
p2=p1->next;//p2得到倒數第二節的地址
Pos(p1->x,p1->y);
printf("");//尾節點用空格代替
free(p1);//釋放尾節點
tail=p2;//尾指針挪到倒數第二節
p=head;//輸出蛇身
while(p2->next!=NULL)
p2=p2->next;
Pos(p2->x,p2->y);
printf("■");

}
voidcheckkey()
{
intch;
printf("鍵盤鍵碼測試,ESC退出... ");
while(1)
{
if(kbhit()){
ch=getch();
if(ch==27)break;//ESC鍵退出
if(ch==224){printf("%d",ch);ch=getch();}elsecontinue;
printf(" %d ",ch);
}
}
}

上面是一個部分實現的貪吃蛇,裡面就通過延時等待,實現了自動的移動。主要在void Gamecircle()函數中。

如還有問題,請追問

㈢ 用VC++6.0,貪吃蛇C語言代碼加思路

㈣ 誰有用c語言製作貪吃蛇的代碼,及使用方法,用VC打

/*
C/C++貪吃蛇游戲,zjlj,2015.3.16
*/
#define DEBUG 0 //當程序在調試階段時 DEBUG為 1
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
void readini(FILE **fphead, int *score, char *argv[]) //創建或打開一個和運行文件對應的ini文件,讀取最高紀錄
{
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("無法創建或打開\"%s\"文件\n",pfilename);
system("pause");
exit(0);
}
}
else
{
fread(score,sizeof(int),1,*fphead);
}
}
void writeini(FILE **fphead, int *score, char *argv[]) //打開一個和運行文件對應的ini文件,寫入最高紀錄
{
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("無法寫入\"%s\"文件,磁碟防寫!\n",pfilename);
system("pause");
exit(0);
}
else
{
rewind(*fphead);
fwrite(score,sizeof(int),1,*fphead);
fclose(*fphead);
}
}
void gotoxy(int x,int y)//游標定位,游標定位函數SetConsoleCursorPosition是左上角位置是0,0然後向左向下延伸
{
COORD pos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//顏色函數
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}

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

int main(int argc, char *argv[]){
int tcsQipan[22][22]; // 貪吃蛇棋盤是一個二維數組(如22*22,包括牆壁)
int i,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
int tcsZuobiao[2][500]; //蛇的坐標數組
for(i=0; i<4; i++)
{
tcsZuobiao[0][i] = 1;//蛇身和蛇頭的x坐標
tcsZuobiao[1][i] = i + 1;//蛇身和蛇頭的y坐標
}
int head = 3,tail = 0;//標示蛇頭和蛇尾的數組偏移量
for(i=1;i<=3;i++)
tcsQipan[1][i]=4; //蛇身
tcsQipan[1][4]=3; //蛇頭
int x1, 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<<"\n\n\t\t\t\t貪吃蛇游戲即將開始 !"<<endl;//准備開始
long start,starttemp;
int grade = 1, length = 4; //設置初始等級和蛇的初始長度
int gamespeed = 500; //設置初始前進時間間隔
for(i=3;i>=0;i--)
{
start=clock();
while(clock()-start<=1000);
system("cls");
if(i>0)
cout << "\n\n\t\t\t\t進入倒計時:" << i << endl; //倒計時顯示
else
Refresh(tcsQipan,grade,gamespeed,length,score); //初始棋盤顯示
}
int timeover=1,otherkey=1;//初始化超時時間和按鍵判斷參數
char direction = 77; // 設置初始情況下,向右運動
int x=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 << "\t按鍵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);
}
else if(direction==80 && directiontemp==72)
{
direction=32;//設置按鍵為非方向鍵
otherkey=0;// 按鍵為非方向鍵,otherkey設置為0
// start = clock();
//while(clock()-start<=starttemp);//補償等待時間
}
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) // 蛇頭碰到牆壁,結束本局游戲
{
gotoxy(22,12);
cout << "\t游戲已結束!" << 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 << "\t游戲已結束!" << 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 << "\t游戲進行中!" << 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 << "\t游戲進行中!" << endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else
{
gotoxy(22,12);
cout << "\t游戲暫停中!" << endl;
}
}
/*
游戲運行時的核心演算法結束
*/
}
while(1)
{
while(!kbhit());
if((direction =getch())==13)//按回車鍵開始下一局
break;
if(direction ==27)//按ESC退出遊戲
exit(0);
}
system("cls");//清除屏幕重新開始
}
return 0;
}

㈤ C語言編貪吃蛇,用VC++可以嗎能運行嗎

完全可以,Dev也可以的,看你個人的喜好。個人建議用Dev,他站的內存少,便於安裝,功能也很強大,c語言學習的最佳搭檔。

㈥ c語言 貪吃蛇 程序

基本思路:

蛇每吃一個食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇頭的運動,而蛇身子跟著蛇頭走,每後一格蛇身子下一步走到上一格蛇身子的位置,以此類推。

#include <stdio.h>

#include <conio.h>

#include <windows.h>

#define BEG_X 2

#define BEG_Y 1

#define WID 20

#define HEI 20

HANDLE hout;

typedef enum {UP, DOWN, LEFT, RIGHT} DIR;

typedef struct Snake_body

{

COORD pos;//蛇身的位置

struct Snake_body *next;//下一個蛇身

struct Snake_body *prev;//前一個蛇身

}SNAKE, *PSNAKE;

PSNAKE head = NULL;//蛇頭

PSNAKE tail = NULL;//蛇尾

//畫游戲邊框的函數

void DrawBorder()

{

int i, j;

COORD pos = {BEG_X, BEG_Y};

for(i = 0; i < HEI; ++i)

{

SetConsoleCursorPosition(hout, pos);

for(j = 0; j < WID; ++j)

{

if(i == 0)//第一行

{

if(j == 0)

printf("┏");

else if(j == WID - 1)

printf("┓");

else

printf("━");

}

else if(i == HEI - 1)//最後一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最後一列

printf("┃");

else

printf(" ");

}

++pos.Y;

}

}

//添加蛇身的函數

void AddBody(COORD pos)

{

PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));

pnew->pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew->next = head;//新創建蛇身的next指向原先的蛇頭

head->prev = pnew;//原先的蛇頭的prev指向新創建的蛇身

head = pnew;//把新創建的蛇身作為新的蛇頭

}

SetConsoleCursorPosition(hout, head->pos);

printf("◎");

}

//蛇身移動的函數

void MoveBody(DIR dir)

{

PSNAKE ptmp;

COORD pos = head->pos;

switch(dir)

{

case UP:

if(head->pos.Y > BEG_Y + 1)

--pos.Y;

else

return;

break;

case DOWN:

if(head->pos.Y < BEG_Y + HEI - 2)

++pos.Y;

else

return;

break;

case LEFT:

if(head->pos.X > BEG_X + 2)

pos.X -= 2;

else

return;

break;

case RIGHT:

if(head->pos.X < BEG_X + (WID - 2) * 2)

pos.X += 2;

else

return;

break;

}

AddBody(pos);//添加了一個新的蛇頭

ptmp = tail;//保存當前的蛇尾

tail = tail->prev;

if(tail)

tail->next = NULL;

SetConsoleCursorPosition(hout, ptmp->pos);

printf(" ");

free(ptmp);

}

int main()

{

int ctrl;

DIR dir = RIGHT;//初始蛇的方向是向右的

COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};

system("color 0E");

system("mode con cols=90 lines=30");

hout = GetStdHandle(STD_OUTPUT_HANDLE);

printf(" ------------貪吃蛇的移動------------");

DrawBorder();

//自定義幾個蛇的身體

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

//控制蛇的移動

while(ctrl = getch())

{

switch(ctrl)

{

case 'w':

if(dir == DOWN)

continue;

dir = UP;

break;

case 's':

if(dir == UP)

continue;

dir = DOWN;

break;

case 'a':

if(dir == RIGHT)

continue;

dir = LEFT;

break;

case 'd':

if(dir == LEFT)

continue;

dir = RIGHT;

break;

case 'q':

return 0;

}

MoveBody(dir);

}

return 0;

}

(6)貪吃蛇c語言vc擴展閱讀:

實現邏輯

1,可以設置游標,就能實現制定位置列印製定符號。

2,涉及一個結構體,包含兩個元素坐標元素和一個結構體指針。

3,結構體串聯形成鏈表,遍歷獲取成員坐標,列印符號得到蛇身。

4,不斷的加頭,去尾,重新遍歷坐標,再列印形成蛇的移動。

5,食物產生的位置判定,不能越界,也不能與蛇身體重合。

6,蛇的轉向判定,一條規則,不允許倒退。

7,轉向的實現,跟行進方向決定新的關節坐標(當前頭的上下左右)

8,死亡檢測,是否頭節點坐標是否與牆壁重合,是否與身體其他關節重合。

9,加速減速,設置刷新休眠時間實現。

㈦ 求貪吃蛇的c語言代碼,覺得挺好玩的

。。4555555555555

㈧ c語言貪吃蛇代碼

基本思路:

蛇每吃一個食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇頭的運動,而蛇身子跟著蛇頭走,每後一格蛇身子下一步走到上一格蛇身子的位置,以此類推。

#include <stdio.h>

#include <conio.h>

#include <windows.h>

#define BEG_X 2

#define BEG_Y 1

#define WID 20

#define HEI 20

HANDLE hout;

typedef enum {UP, DOWN, LEFT, RIGHT} DIR;

typedef struct Snake_body

{

COORD pos;//蛇身的位置

struct Snake_body *next;//下一個蛇身

struct Snake_body *prev;//前一個蛇身

}SNAKE, *PSNAKE;

PSNAKE head = NULL;//蛇頭

PSNAKE tail = NULL;//蛇尾

//畫游戲邊框的函數

void DrawBorder()

{

int i, j;

COORD pos = {BEG_X, BEG_Y};

for(i = 0; i < HEI; ++i)

{

SetConsoleCursorPosition(hout, pos);

for(j = 0; j < WID; ++j)

{

if(i == 0)//第一行

{

if(j == 0)

printf("┏");

else if(j == WID - 1)

printf("┓");

else

printf("━");

}

else if(i == HEI - 1)//最後一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最後一列

printf("┃");

else

printf(" ");

}

++pos.Y;

}

}

//添加蛇身的函數

void AddBody(COORD pos)

{

PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));

pnew->pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew->next = head;//新創建蛇身的next指向原先的蛇頭

head->prev = pnew;//原先的蛇頭的prev指向新創建的蛇身

head = pnew;//把新創建的蛇身作為新的蛇頭

}

SetConsoleCursorPosition(hout, head->pos);

printf("◎");

}

//蛇身移動的函數

void MoveBody(DIR dir)

{

PSNAKE ptmp;

COORD pos = head->pos;

switch(dir)

{

case UP:

if(head->pos.Y > BEG_Y + 1)

--pos.Y;

else

return;

break;

case DOWN:

if(head->pos.Y < BEG_Y + HEI - 2)

++pos.Y;

else

return;

break;

case LEFT:

if(head->pos.X > BEG_X + 2)

pos.X -= 2;

else

return;

break;

case RIGHT:

if(head->pos.X < BEG_X + (WID - 2) * 2)

pos.X += 2;

else

return;

break;

}

AddBody(pos);//添加了一個新的蛇頭

ptmp = tail;//保存當前的蛇尾

tail = tail->prev;

if(tail)

tail->next = NULL;

SetConsoleCursorPosition(hout, ptmp->pos);

printf(" ");

free(ptmp);

}

int main()

{

int ctrl;

DIR dir = RIGHT;//初始蛇的方向是向右的

COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};

system("color 0E");

system("mode con cols=90 lines=30");

hout = GetStdHandle(STD_OUTPUT_HANDLE);

printf(" ------------貪吃蛇的移動------------");

DrawBorder();

//自定義幾個蛇的身體

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

//控制蛇的移動

while(ctrl = getch())

{

switch(ctrl)

{

case 'w':

if(dir == DOWN)

continue;

dir = UP;

break;

case 's':

if(dir == UP)

continue;

dir = DOWN;

break;

case 'a':

if(dir == RIGHT)

continue;

dir = LEFT;

break;

case 'd':

if(dir == LEFT)

continue;

dir = RIGHT;

break;

case 'q':

return 0;

}

MoveBody(dir);

}

return 0;

}

(8)貪吃蛇c語言vc擴展閱讀:

實現邏輯

1,可以設置游標,就能實現制定位置列印製定符號。

2,涉及一個結構體,包含兩個元素坐標元素和一個結構體指針。

3,結構體串聯形成鏈表,遍歷獲取成員坐標,列印符號得到蛇身。

4,不斷的加頭,去尾,重新遍歷坐標,再列印形成蛇的移動。

5,食物產生的位置判定,不能越界,也不能與蛇身體重合。

6,蛇的轉向判定,一條規則,不允許倒退。

7,轉向的實現,跟行進方向決定新的關節坐標(當前頭的上下左右)

8,死亡檢測,是否頭節點坐標是否與牆壁重合,是否與身體其他關節重合。

9,加速減速,設置刷新休眠時間實現。

㈨ 關於c語言編譯貪吃蛇的調試問題(VC++),運行出來只有一條死蛇(不能動QAQ),字數問題就只打出關鍵函數

啊……這是TC編的吧,我這木有那個編譯器了,沒法調,你將斷點短在這里,if(food.yes==1),看看不按鍵的時候會走到這個循環裡面不,如果可以在斷在你的switch裡面,應該是沒有進到這個代碼段裡面,看看問題出在哪就OK了,另外,switch語句裡面最好加個default判斷,不然容易出問題

熱點內容
安卓qq郵箱在qq哪裡可以看到 發布:2025-03-11 10:51:33 瀏覽:263
windows內核安全編程源碼 發布:2025-03-11 10:50:51 瀏覽:989
狗追兔子編程 發布:2025-03-11 10:45:35 瀏覽:542
存儲過程in參數 發布:2025-03-11 10:35:29 瀏覽:121
雙位元組加密 發布:2025-03-11 10:35:19 瀏覽:984
小米4android71 發布:2025-03-11 10:33:51 瀏覽:946
怎麼用ftp上傳網站 發布:2025-03-11 10:25:30 瀏覽:192
酷狗傳歌到手機文件夾 發布:2025-03-11 10:14:58 瀏覽:579
遺傳進化演算法 發布:2025-03-11 10:13:23 瀏覽:620
php時間戳js 發布:2025-03-11 10:11:29 瀏覽:1001