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

用c語言貪吃蛇

發布時間: 2023-01-04 09:19:00

1. 誰有用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;
}

2. C語言寫貪吃蛇

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h> /*引用的庫函數*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b/*宏定義鍵名*/
#define N 200
int i,key;
int level;/*游戲等級*/
int score=0;/*得分*/
int gamespeed;/*游戲速度*/
struct Food
{
int x;/*食物的橫坐標*/
int y;/*食物的縱坐標*/
int yes;/*判斷是否要出現食物的變數*/
}food;/*食物的結構體*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的節數*/
int direction;/*蛇移動方向*/
int life;/* 蛇的生命,0活著,1死亡*/
}snake;/*蛇的結構體*/
void Choicelevle(void);/*選擇游戲等級*/
void Init(void);/*圖形驅動*/
void Close(void);/*圖形結束*/
void DRAW(void);/*游戲區域*/
void GameOver(void);/*結束游戲*/
void GamePlay(void);/*玩游戲具體過程*/
void PrScore(void);/*輸出成績*/
/*主函數*/
void main(void)
{
Init();/*圖形驅動*/
Choicelevle();/*選擇游戲等級*/
DRAW();/*游戲區域*/
GamePlay();/*玩游戲具體過程*/
Close();/*圖形結束*/
}

/*圖形驅動*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\turboc2"); /*初始化圖形系統*/
cleardevice(); /*清除圖形界面*/
}
/*選擇游戲等級*/
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");/*製作人學號*
/outtextxy(300,380,"class:computer science
0602");/*製作人班級*/getch();printf("please putin your
name:");gets(name);printf("please choice
levle:");scanf("%d",&level);gamespeed=100000-400*level-300*level*level;if(level>9||level<1){cleardevice();
/*清除圖形界面*/setcolor(YELLOW); /*設置字體顏色*/settextstyle(0,0,2);
/*設置字體類型*/outtextxy(150,200,"level input error");
/*顯示文本*/getch();level=1;}
}
void DRAW(void)
{cleardevice(); /*清屏*/setcolor(2);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設置線型*/rectangle(45,45,465,325);}
/*玩游戲具體過程*/
void GamePlay(void)
{setcolor(5);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*
設置線型*/randomize();/*隨機數發生器*/food.yes=1;/*1表示需要出現新食物,0表示已經存在食物*
/snake.life=0;/*活著*/snake.direction=1;/*方嚮往右*/snake.x[0]=320;
snake.y[0]=240;/*蛇頭*/snake.x[1]=330;snake.y[1]=240;
/*蛇的第二節位置*/snake.node=3;/*節數*/PrScore();/*輸出得分*/while(1)/*可以重復玩游戲,壓ESC鍵結
束*/{while(!kbhit())/*在沒有按鍵的情況下,蛇自己移動身體*/{if(food.yes==1)/*需要出現新食物*
/{food.x=rand()%360+70;food.y=rand()%250+60;while(food.x%10!=0)/*食物隨機出現後
必須讓食物能夠在整格內,這樣才可以讓蛇吃到*/food.x++;while(food.y%10!=0)food.y++;food.yes=0;
/*畫面上有食物了*/}
if(food.yes==0)/*畫面上有食物了就要顯示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)/*蛇的每個環節往前移動*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個方向,通過這個控制來移動蛇頭*/
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;i<snake.node;i++)/*從蛇的第四節開始判斷是否撞到自己了,因為蛇頭為兩節,第三節不可能拐過來*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*顯示失敗*/
snake.life=1; /*蛇死*/
break;
}
}

/*如果蛇頭碰到牆壁,蛇頭從對面牆出來*/
if(snake.x[0]<50)
{snake.x[0]=450;
/*如果蛇頭越過左邊界,則從右邊界進入*/snake.y[0]=snake.y[0];/*縱坐標不變*/for(i=snake.node-1;
i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他節數向前推進*/}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數*/
}
}
else
if(snake.x[0]>450)
{snake.x[0]=50;
/*如果蛇頭越過右邊界,則蛇頭從左邊界進入*/snake.y[0]=snake.y[0];/*縱坐標不變*/for(i=snake.node-
1;i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他節數向前推進*/}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數*/
}
}
else
if(snake.y[0]<60)
{snake.y[0]=320;
/*如果蛇頭越過上邊界,則從下邊界進入*/snake.x[0]=snake.x[0];/*橫坐標不變*/for(i=snake.node-1;
i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他節數向前推進*/}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數*/
}
}
else
if(snake.y[0]>320)
{snake.y[0]=60;
/*如果蛇頭越過下邊界,則從上邊界進入*/snake.x[0]=snake.x[0];/*橫坐標不變*/for(i=snake.node-1;
i>0;i--){snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];
/*蛇的其他節數向前推進*/}
{
setfillstyle(SOLID_FILL,0); /*設置填充模式和顏色,0表示黑色*/
bar(50,55,455,315);/*bar是表示填充的范圍的函數*/
}
}
if(snake.life==1)/*如果蛇死就跳出內循環,重新開始*/
break;
if(snake.x[0]==food.x&&snake.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; /*每吃掉一食物,得分累加10分*/
if(score%100==0)
{level++;gamespeed=100000-400*level-
300*level*level;/*每吃掉10食物提升一級,速度加快*/PrScore();/*輸出新得分*/setcolor(YELLOW);
/*設置字體顏色*/settextstyle(0,0,4); /*設置字體類型*/outtextxy(150,200,"LEVEL UP");

/*顯示文本*/if(level==10){level=1,gamespeed=100000-400*level-
300*level*level;}
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
bar(50,55,455,315);/*bar是表示填充的范圍的函數*/
}
PrScore();/*輸出新得分*/
}
setcolor(4);/*畫出蛇*/
for(i=0;i<snake.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)*/ /*用黑色去除蛇的的最後一節*/
if(snake.life==1)/*如果蛇死就跳出循環*/
break;
key=bioskey(0);/*接收按鍵*/
if(key==ESC)/*按ESC鍵退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判斷是否往相反的方向移動*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戲結束*/
void GameOver(void)
{
cleardevice(); /*清屏*/
PrScore();
setcolor(RED); /*設置字體顏色*/
settextstyle(0,0,4); /*設置字體類型*/
outtextxy(200,200,"GAME OVER"); /*顯示文本*/
getch();
}
/*輸出成績及游戲等級*/
void PrScore(void)
{
char str1[20];/*設置字元型數組*/
setfillstyle(SOLID_FILL,0);
bar(50,15,390,35); /*填充矩形框*/
setcolor(6); /*設置文本顏色*/
settextstyle(0,0,2); /*設置數組顯示位置*/
sprintf(str1,"score %d level %d",score,level);/*顯示數組內容*/
outtextxy(55,20,str1);
setcolor(YELLOW); /*設置字體顏色*/
settextstyle(0,0,2); /*設置字體類型*/
outtextxy(250,400,"EXIT=ESC ");/*顯示文本*/
}
void Close(void)
{
closegraph();
}

3. 怎樣在mac上用C語言寫出一個貪吃蛇游戲

話說,我用c++11和opengl在mac上寫過一個貪吃蛇,可以參考一下:https://git.oschina.net/lt123345/snakegame

主要是貪吃蛇本身的核心邏輯才70行左右(偷懶用了STL的list,不用自己實現鏈表)

當時裝逼用英語寫的注釋,請不要打我!

核心邏輯大概是這樣子 每次移動的時候:

1. 如果蛇頭出界或者碰到自己,game over

2. 如果蛇頭吃到食物,蛇變長。

3. 否則(蛇頭什麼都沒碰到)移動蛇。

用鏈表存蛇身的各個位置的話,鏈表尾部當蛇頭,那蛇變長的操作很簡單:把新的蛇頭位置插入鏈表尾部。

移動蛇的操作也簡單:把新的蛇頭位置插入鏈表尾部,刪除鏈表頭。

然後,剩下的事情還有兩個:

1. 拿到蛇身的所有位置,在對應位置畫上蛇身

2. 獲取用戶輸入,改變蛇的移動方向

那。。。你需要了解的只有:

  1. opengl怎麼在指定位置畫方塊(圓圈)

  2. opengl怎麼處理用戶輸入

沒了。

4. 大專考試,C語言貪吃蛇程序編寫,

//DEVC++通過
/*
[email protected]
*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>
#include<direct.h>
#include<stdbool.h>

#defineW80//屏幕寬度
#defineH37//屏幕高度
#defineSNAKE_ALL_LENGTH200//蛇身最長為

voidCALLBACKTimerProc(
HWNDhwnd,
UINTmessage,
UINTidTimer,
DWORDdwTime);
voidstart();

structMYPOINT
{
intx;
inty;
}s[SNAKE_ALL_LENGTH],head,end,food;

intmax_count=0;//歷史最高分,如果count>max_count,則max_count=count
intold_max_count=0;//歷史最高分,不會變動,用於死亡時判斷max_count是否大於old_max_count,如果大於,則寫入文件
intcount=0;//得分
intlen=20;//當前蛇身長度
intdirect=0;//方向:0-向右,1-向下,2-向左,3-向上
intspeed=200;//速度:毫秒
boolisfood=false;//食物是否存在
inttimerID;
boolstop=false;//暫停
char*ini_path;//數據文件絕對路徑
voidsetxy(intx,inty)//設置CMD窗口游標位置
{
COORDcoord={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

voidhide_cursor()//隱藏CMD窗口游標
{
CONSOLE_CURSOR_INFOcci;
cci.bVisible=FALSE;
cci.dwSize=sizeof(cci);
HANDLEhandle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle,&cci);
}

voidset_food()//設置食物坐標
{
if(isfood==true)
{
return;
}
intx,y,i;
boolflag=false;
while(1)
{
flag=false;
x=rand()%(W-14)+6;
y=rand()%(H-12)+6;
for(i=0;i<len;i++)//判斷食物是否落在蛇身上
{
if(s[i].x==x&&s[i].y==y)
{
flag=true;
break;
}
}
if(flag==true)
{
continue;
}
else
{
food.x=x;
food.y=y;
break;
}
}
setxy(food.x,food.y);
printf("*");
isfood=true;
}

voidcheck_board()//檢測蛇身是否越界和相交
{
inti;
if(s[0].x>=W-3||s[0].x<=2||s[0].y>=H-1||s[0].y<=2)
{
setxy(W/2-5,0);
printf("gameover ");
stop=true;
if(old_max_count<max_count)
{
chart[5]={''};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
}
for(i=1;i<len;i++)
{
if(s[i].x==s[0].x&&s[i].y==s[0].y)
{
setxy(W/2-5,0);
printf("gameover ");
stop=true;
if(old_max_count<max_count)
{
chart[5]={''};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
break;
}
}
if(stop==true)
{
KillTimer(NULL,timerID);
intc;
while(1)
{
fflush(stdin);
c=_getch();
if(c=='n'||c=='N')
{
start();
}
elseif(c=='q'||c=='Q')
{
exit(0);
}
elsecontinue;
}
}
}

voidprintf_body(boolis_first)//列印蛇身
{
if(is_first==true)//如果是第一次列印蛇身
{
inti;
for(i=0;i<len;i++)
{
setxy(s[i].x,s[i].y);
printf("O");
}
}
else//如果不是第一次列印蛇身
{
setxy(end.x,end.y);
printf("");
setxy(s[0].x,s[0].y);
printf("O");
}
if(food.x==s[0].x&&food.y==s[0].y)//如果吃到食物
{
count++;
isfood=false;//重置食物
set_food();
len++;
KillTimer(NULL,timerID);
if(speed>100)speed-=10;
elseif(speed>50)speed-=5;
elseif(speed>30)speed-=2;
elseif(speed>16)speed-=1;
else;
setxy(0,0);
if(max_count<count) max_count=count;
printf("speed:%dmsscore:%dbestscore:%d",speed,count,max_count);
timerID=SetTimer(NULL,001,speed,TimerProc);
}
}

voidchange_body_pos(intx,inty)//改變蛇身的坐標數據
{
end.x=s[len-1].x;
end.y=s[len-1].y;
inti;
for(i=len-1;i>0;i--)
{
s[i].x=s[i-1].x;
s[i].y=s[i-1].y;
}
s[0].x=x;
s[0].y=y;
}
voidCALLBACKTimerProc(
HWNDhwnd,
UINTmessage,
UINTidTimer,
DWORDdwTime)
{
switch(direct)
{
case0:
head.x++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case1:
head.y++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case2:
head.x--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case3:
head.y--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
}
}

voidstart()
{
inti;
KillTimer(NULL,timerID);
count=0;//得分
len=20;//當前蛇身長度
direct=0;//方向:0-向右,1-向下,2-向左,3-向上
speed=200;//速度:毫秒
isfood=false;//食物是否存在
stop=false;//停止
system("cls");
setxy(1,4);
printf("┌─────────────────────────────────────┐ ");
for(i=0;i<33;i++)
{
printf("││ ");
}
printf("└─────────────────────────────────────┘");
head.x=len-1+5;
head.y=H/2;
for(i=0;i<len;i++)
{
s[i].x=head.x-i;
s[i].y=head.y;
}
setxy(0,0);
printf("speed:%d:msscore:%dbestscore:%d",speed,count,max_count);
printf_body(true);
set_food();
timerID=SetTimer(NULL,001,speed,TimerProc);
intc;
MSGmsg;
while(GetMessage(&msg,NULL,0,0))
{
if(stop==true) break;
if(_kbhit())//如果按下的是方向鍵或功能鍵,_getch()要調用兩次,第一次返回0XE0或0
{
fflush(stdin);
c=_getch();//上:72下:80左:75右:77
if(c==0XE0||c==0)
{
c=_getch();
if(c==72&&direct!=1&&direct!=3)
{
direct=3;
}
elseif(c==80&&direct!=1&&direct!=3)
{
direct=1;
}
elseif(c==75&&direct!=0&&direct!=2)
{
direct=2;
}
elseif(c==77&&direct!=0&&direct!=2)
{
direct=0;
}
}
elseif(c=='')
{
setxy(W/2-10,0);
system("pause");
setxy(W/2-10,0);
printf("");
}
}
if(msg.message==WM_TIMER)
{
DispatchMessage(&msg);
}
}
}

intmain()
{
ini_path=(char*)malloc(sizeof(char)*50);
srand((unsigned)time(0));
getcwd(ini_path,50);//取得當前程序絕對路徑
ini_path=strcat(ini_path,"snake.dat");

max_count=GetPrivateProfileInt("MAX_COUNT","max_count",0,ini_path);
old_max_count=max_count;
charcmd[50];
sprintf(cmd,"modeconcols=%dlines=%d",W,H);
system(cmd);//改變CMD窗口大小
hide_cursor();
start();
return0;
}

5. 如何用C語言寫貪吃蛇

p->x和p->y都是指向int的指針,不是數組,所以x[1]、y[1]等數據,使用的全是未申請的內存。
你應該定將他們成一個整型數組,大小足夠大,或者乾脆使用鏈表。

6. 貪吃蛇c語言代碼

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
char gamemap[20][40];//游戲地圖大小 20*40
int score=0;//當前分數
//記錄蛇的結點
int x[800];//每個結點的行編號
int y[800];//每個結點的列編號
int len = 0;//蛇的長度

//記錄水果信息
int fx=0;//食物的橫坐標
int fy=0;//食物的縱坐標
int fcount=0;//食物的數目

//主要函數操作
void createfood();//生成食物
void PrintgameMap(int x[],int y[]);//畫游戲地圖
void move(int x[],int y[]);//移動蛇

int main()
{
srand(time(NULL));
//初始化蛇頭和身體的位置,默認剛開始蛇長為2
x[len] = 9;
y[len] = 9;
len++;
x[len] = 9;
y[len] = 8;
len++;

createfood();
PrintgameMap(x,y);
move(x,y);
return 0;
}
void createfood()
{
if(0==fcount)
{
int tfx=rand()%18+1;
int tfy=rand()%38+1;
int i,j;
int have=0;//為0表示食物不是食物的一部分
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
if(x[i]==fx&&y[j]==fy)
{
have=1;
break;
}
else
{
have=0;
}
}
if(1==have)//若為蛇的一部分,執行下一次循環
{
continue;
}
else//否則生成新的水果
{
fcount++;
fx=tfx;
fy=tfy;
break;
}
}

}
}
//游戲地圖
void PrintgameMap(int x[],int y[])
{
int snake = 0,food=0;

int i, j;
//畫游戲地圖,並畫出蛇的初始位置
for (i = 0; i < 20; i++)
{
for (j = 0; j < 40; j++)
{
if (i == 0 && j >= 1 && j <= 38)
{
gamemap[i][j] = '=';

}
else if (i == 19 && j >= 1 && j <= 38)
{
gamemap[i][j] = '=';
}
else if (j == 0 || j == 39)
{
gamemap[i][j] = '#';
}
else
{
gamemap[i][j] = ' ';
}
//判斷蛇是否在當前位置
int k;
for ( k = 0; k < len; k++)
{

if (i == x[k]&&j == y[k])
{
snake = 1;
break;
}
else
{
snake = 0;
}
}
{
if(fcount&&fx==i&&fy==j)
{
food=1;
}
else
{
food=0;
}
}
//若蛇在當前位置
if (1==snake )
{
printf("*");
}
else if(1==food)
{
printf("f");
}
//若蛇不在當前位置並且當前位置沒有水果
else
{
printf("%c", gamemap[i][j]);
}

}
printf("\n");

}

printf("score:%d",score);
}
//移動
void move(int x[],int y[])
{
char s;
s=getch();
int move=0,beat=0;
while (1)
{

int cx[800];
int cy[800];
memcpy(cx, x, sizeof(int)*len);
memcpy(cy, y, sizeof(int)*len);
//頭

if (s=='w')
{

x[0]--;
move=1;
if(x[0]<=0)
{
printf("Game over\n");
break;
}

}
else if (s=='s')
{
x[0]++;
move=1;
if(x[0]>=19)
{
printf("Game over\n");
break;
}

}
else if (s=='a')
{
y[0] --;
move=1;
if(y[0]<=0)
{
printf("Game over\n");
break;
}

}
else if (s=='d')
{
y[0]++;
move=1;
if(y[0]>=39)
{
printf("Game over\n");
break;
}
}

//身體
int i;
for ( i = 1; i < len; i++)
{

x[i] = cx[i - 1];
y[i] = cy[i - 1];

}
for(i=1;i<len;i++)//要是咬到了自己
{
if(x[0]==x[i]&&y[0]==y[i])
{
beat=1;
}
else
{
beat=0;
}
}
if(1==beat)
{
printf("Game over\n");
break;
}
if(1==move)
{
if(fcount&&x[0]==fx&&y[0]==fy)//如果吃到了果子
{
//拷貝當前蛇頭地址到第二個結點
memcpy(x+1,cx,sizeof(int)*len);
memcpy(y+1,cy,sizeof(int)*len);
len++;
fcount--;
fx=0;
fy=0;
score++;
createfood();
}
Sleep(70);
system("cls");
PrintgameMap( x, y);

}
else
continue;
if(kbhit())//判斷是否按下按鍵
{
s=getch();
}

}

}

7. 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-07-05 09:55:08 瀏覽:87
達內學校php 發布:2025-07-05 09:52:05 瀏覽:398
獲取資料庫所有表 發布:2025-07-05 09:39:12 瀏覽:654
wcfphp 發布:2025-07-05 09:39:07 瀏覽:178
解壓密碼對 發布:2025-07-05 09:33:00 瀏覽:586
廣東金稅盤的伺服器地址是什麼 發布:2025-07-05 09:10:29 瀏覽:704
掛式手機卡的服務密碼是多少 發布:2025-07-05 08:57:40 瀏覽:944
電信卡密碼八位數是多少 發布:2025-07-05 08:49:37 瀏覽:441
配置高用的久選什麼電腦 發布:2025-07-05 08:22:40 瀏覽:741
迷你世界如何卡進設密碼的房間 發布:2025-07-05 08:15:16 瀏覽:882