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

貪吃蛇的c語言源代碼

發布時間: 2023-07-20 03:55:06

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

。。4555555555555

⑵ C語言 的 貪吃蛇 代碼 謝謝 大家啦。。。。。。。

你好,很榮幸回答你的問題,我這里是一個c的貪吃蛇源代碼,希望對你有幫助,不過運行這個時需要你的軟體包含驚蟄EasyX圖形函數,比如vc++6.0,如遇到問題問題可以聯系我,希望對你有幫助。
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>

#define LEFT 'a'
#define RIGHT 'd'
#define DOWN 's'
#define UP 'w'
#define ESC 27

#define N 200 /*蛇的最大長度*/

int i;
char key;
int score=0; /*得分*/
int gamespeed=100; /*游戲速度自己調整*/

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 Init(void); /*圖形驅動*/
void Close(void); /*圖形結束*/
void DrawK(void); /*開始畫面*/
void GameOver(void); /*結束游戲*/
void GamePlay(void); /*玩游戲具體過程*/
void PrScore(void); /*輸出成績*/

/*主函數*/
void main(void)
{
Init(); /*圖形驅動*/
DrawK(); /*開始畫面*/
GamePlay(); /*玩游戲具體過程*/

Close(); /*圖形結束*/
}

/*圖形驅動*/
void Init(void)
{
int gd=9,gm=2;

initgraph(&gd,&gm," ");
cleardevice();
}

/*開始畫面,左上角坐標為(50,40),右下角坐標為(610,460)的圍牆*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(LIGHTCYAN);
setlinestyle(PS_SOLID,0,1); /*設置線型*/
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); /*右邊*/
}
}

/*玩游戲具體過程*/
void GamePlay(void)
{
srand(time(NULL)); /*隨機數發生器*/
food.yes=1; /*1表示需要出現新食物,0表*/
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(); /*輸出得分*/
while(1) /*可以重復玩游戲,壓ESC鍵*/
{
while(!kbhit()) /*在沒有按鍵的情況下,蛇自*/
{
if(food.yes==1) /*需要出現新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+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]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455) /*蛇是否撞到牆壁*/
{ GameOver(); /*本次游戲結束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1) /*以上兩種判斷以後,如果蛇*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以後*/
{
setcolor(BLACK); /*把畫面上的食物東西去*/
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(); /*輸出新得分*/
}
setcolor(RED); /*畫出蛇*/

for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
Sleep(gamespeed);
setcolor(BLACK); /*用黑色去除蛇的的最後*/
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=getch(); /*接收按鍵*/
if (key == ESC) break; /*按ESC鍵退出*/
switch(key)
{
case UP:
if(snake.direction!=4) /*判斷是否往相反的方向移動*/
snake.direction=3;
break;
case RIGHT:
if(snake.direction!=2)
snake.direction=1;
break;
case LEFT:
if(snake.direction!=1)
snake.direction=2;
break;
case DOWN:
if(snake.direction!=3)
snake.direction=4;
break;
}

}/*endwhile(1)*/
}

/*游戲結束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
setfont(56,0,"黑體");
outtextxy(200,200,"GAME OVER");
getch();
}

/*輸出成績*/
void PrScore(void)
{
char str[10];

setfillstyle(YELLOW);
bar(50,15,220,35);
setcolor(BROWN);
setfont(16,0,"宋體");
sprintf(str,"score:%d",score);
outtextxy(55,16,str);
}

/*圖形結束*/
void Close(void)
{
closegraph();
}

⑶ 用C語言編寫貪吃蛇游戲的程序

回答:Mr.emily
大師
6月3日
16:45
#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;
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
Close();
void
DrawK();
void
GamePlay();
void
GameOver();
void
PrScore();
void
main()
{
Init();
DrawK();
GamePlay();
Close();
}
void
Init()
{int
gd=DETECT,gm;
initgraph(&gd,&gm,"F:\\tuoboc2");/*此處為turboc的路徑,讀者可以根據自己的電腦而改*/
cleardevice();
}
void
DrawK()
{setbkcolor(LIGHTGREEN);
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
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);
}
}
void
GamePlay()
{randomize();
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();
while(1)
{while(!kbhit())
{
if(food.yes==1)
{food.x=rand()%400+60;
food.y=rand()%350+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];
}
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]<55||snake.x[0]>595||snake.y[0]<55||snake.y[0]>455)
{GameOver();
snake.life=1;
}
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;
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);
}
if(snake.life==1)
break;
key=bioskey(0);
if(key==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;
}
}
void
GameOver()
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(3,0,4);
outtextxy(100,100,"Mengmeng,i
love
you!");
getch();
}
void
PrScore()
{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);
}
void
Close()
{
getch();
closegraph();
}
Mr.emily

⑷ 求c語言控制面板貪吃蛇源代碼

//******友情提示:如想速度快點,請改小_sleep(500)函數中參數*****

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
const int H = 8; //地圖的高
const int L = 16; //地圖的長
char GameMap[H][L]; //游戲地圖
int key; //按鍵保存
int sum = 1, over = 0; //蛇的長度, 游戲結束(自吃或碰牆)
int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向
int dy[4] = {-1, 1, 0, 0};
struct Snake //蛇的每個節點的數據類型
{
int x, y; //左邊位置
int now; //保存當前節點的方向, 0,1,2,3分別為左右上下
}Snake[H*L];
const char Shead = '@'; //蛇頭
const char Sbody = '#'; //蛇身
const char Sfood = '*'; //食物
const char Snode = '.'; //'.'在地圖上標示為空
void Initial(); //地圖的初始化
void Create_Food(); //在地圖上隨機產生食物
void Show(); //刷新顯示地圖
void Button(); //取出按鍵,並判斷方向
void Move(); //蛇的移動
void Check_Border(); //檢查蛇頭是否越界
void Check_Head(int x, int y); //檢查蛇頭移動後的位置情況
int main()
{
Initial();
Show();
return 0;
}
void Initial() //地圖的初始化
{
int i, j;
int hx, hy;
system("title 貪吃蛇"); //控制台的標題
memset(GameMap, '.', sizeof(GameMap)); //初始化地圖全部為空'.'
system("cls");
srand(time(0)); //隨機種子
hx = rand()%H; //產生蛇頭
hy = rand()%L;
GameMap[hx][hy] = Shead;
Snake[0].x = hx; Snake[0].y = hy;
Snake[0].now = -1;
Create_Food(); //隨機產生食物
for(i = 0; i < H; i++) //地圖顯示
{
for(j = 0; j < L; j++)
printf("%c", GameMap[i][j]);
printf("\n");
}

printf("\n小小C語言貪吃蛇\n");
printf("按任意方向鍵開始游戲\n");

getch(); //先接受一個按鍵,使蛇開始往該方向走
Button(); //取出按鍵,並判斷方向
}
void Create_Food() //在地圖上隨機產生食物
{
int fx, fy;
while(1)
{
fx = rand()%H;
fy = rand()%L;

if(GameMap[fx][fy] == '.') //不能出現在蛇所佔有的位置
{
GameMap[fx][fy] = Sfood;
break;
}
}
}
void Show() //刷新顯示地圖
{
int i, j;
while(1)
{
_sleep(500); //延遲半秒(1000為1s),即每半秒刷新一次地圖
Button(); //先判斷按鍵在移動
Move();
if(over) //自吃或碰牆即游戲結束
{
printf("\n**游戲結束**\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語言貪吃蛇\n");
printf("按任意方向鍵開始游戲\n");
}
}
void Button() //取出按鍵,並判斷方向
{
if(kbhit() != 0) //檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0
{
while(kbhit() != 0) //可能存在多個按鍵,要全部取完,以最後一個為主
key = getch(); //將按鍵從控制台中取出並保存到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() //蛇的移動
{
int i, x, y;
int t = sum; //保存當前蛇的長度
//記錄當前蛇頭的位置,並設置為空,蛇頭先移動
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); //蛇頭移動後的位置情況,參數為: 蛇頭的開始位置
if(sum == t) //未吃到食物即蛇身移動哦
for(i = 1; i < sum; i++) //要從蛇尾節點向前移動哦,前一個節點作為參照
{
if(i == 1) //尾節點設置為空再移動
GameMap[ Snake[i].x ][ Snake[i].y ] = '.';

if(i == sum-1) //為蛇頭後面的蛇身節點,特殊處理
{
Snake[i].x = x;
Snake[i].y = y;
Snake[i].now = Snake[0].now;
}
else //其他蛇身即走到前一個蛇身位置
{
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 ] = '#'; //移動後要置為'#'蛇身
}
}
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) //檢查蛇頭移動後的位置情況
{

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; //新增加的蛇身為蛇頭後面的那個
Snake[sum].y = y;
Snake[sum].now = Snake[0].now;
GameMap[ Snake[sum].x ][ Snake[sum].y ] = '#';
sum++;
Create_Food(); //食物吃完了馬上再產生一個食物
}
else
over = 1;
}

⑸ 貪吃蛇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();
}

}

}

⑹ 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語言源代碼擴展閱讀:

實現邏輯

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

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

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

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

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

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

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

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

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

熱點內容
html5移動端源碼下載 發布:2025-02-08 06:20:45 瀏覽:147
外網訪問黑群暉 發布:2025-02-08 05:45:59 瀏覽:559
中央存儲伺服器公司地址 發布:2025-02-08 05:38:48 瀏覽:821
伺服器如何查詢表空間的文件路徑 發布:2025-02-08 05:38:00 瀏覽:162
宏基4741g哪個配置好 發布:2025-02-08 05:37:56 瀏覽:810
混合料運輸車的配置是如何計算的 發布:2025-02-08 05:31:35 瀏覽:293
android紅包插件 發布:2025-02-08 05:31:34 瀏覽:365
ea伺服器怎麼連接 發布:2025-02-08 05:16:45 瀏覽:463
更加密更改 發布:2025-02-08 05:15:20 瀏覽:786
倉儲資源配置都需要開展哪些任務 發布:2025-02-08 05:13:51 瀏覽:676