欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

c語言貪吃蛇編程控制函數(shù),c語言編程貪吃蛇教程

c語言 貪吃蛇 程序

基本思路:

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)東寶,十年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108

蛇每吃一個食物蛇身子就增加一格,用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;//蛇尾

//畫游戲邊框的函數(shù)

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;

}

}

//添加蛇身的函數(shù)

void AddBody(COORD pos)

{

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

pnew-pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew-next = head;//新創(chuàng)建蛇身的next指向原先的蛇頭

head-prev = pnew;//原先的蛇頭的prev指向新創(chuàng)建的蛇身

head = pnew;//把新創(chuàng)建的蛇身作為新的蛇頭

}

SetConsoleCursorPosition(hout, head-pos);

printf("◎");

}

//蛇身移動的函數(shù)

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;//保存當(dāng)前的蛇尾

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;

}

擴(kuò)展資料:

實現(xiàn)邏輯

1,可以設(shè)置光標(biāo),就能實現(xiàn)制定位置打印制定符號。

2,涉及一個結(jié)構(gòu)體,包含兩個元素坐標(biāo)元素和一個結(jié)構(gòu)體指針。

3,結(jié)構(gòu)體串聯(lián)形成鏈表,遍歷獲取成員坐標(biāo),打印符號得到蛇身。

4,不斷的加頭,去尾,重新遍歷坐標(biāo),再打印形成蛇的移動。

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

6,蛇的轉(zhuǎn)向判定,一條規(guī)則,不允許倒退。

7,轉(zhuǎn)向的實現(xiàn),跟行進(jìn)方向決定新的關(guān)節(jié)坐標(biāo)(當(dāng)前頭的上下左右)

8,死亡檢測,是否頭節(jié)點坐標(biāo)是否與墻壁重合,是否與身體其他關(guān)節(jié)重合。

9,加速減速,設(shè)置刷新休眠時間實現(xiàn)。

參考資料來源:百度百科-C語言

如何用C語言寫貪吃蛇

#includeconio.h #includegraphics.h #includetime.h #includestring.h #includemalloc.h #includestdio.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.xhead_f-y==food.y) { Sleep(100); crate(); food.judge=0; point=point+(6-grade)*10; if(food.x30||food.y30||food.x570||food.y570) life++; menu(); } if(head_f-x5||head_f-x595||head_f-y5||head_f-y595) { 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-xhead_f-y==p1-y) { Sleep(1000); life--; food.judge=0; init_graph(); init_insect(); menu(); break; } } if(life==0) { outtextxy(280,300,"游戲結(jié)束!"); 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[]={"分?jǐn)?shù):"},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,"設(shè)置: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-yp2-y) head_f-y+=5; else head_f-y-=5; } else { if(p1-xp2-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-xp1-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-xp1-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-xp1-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-xp1-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.x30||food.y30||food.x570||food.y570) { setcolor(RED); circle(f

C語言編寫貪吃蛇需要用哪些函數(shù)??

#include graphics.h

#include stdlib.h

#include dos.h /*引用的庫函數(shù)*/

#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;/*食物的橫坐標(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;/*蛇移動方向*/

int life;/* 蛇的生命,0活著,1死亡*/

}snake;/*蛇的結(jié)構(gòu)體*/

void Choicelevle(void);/*選擇游戲等級*/

void Init(void);/*圖形驅(qū)動*/

void Close(void);/*圖形結(jié)束*/

void DRAW(void);/*游戲區(qū)域*/

void GameOver(void);/*結(jié)束游戲*/

void GamePlay(void);/*玩游戲具體過程*/

void PrScore(void);/*輸出成績*/

/*主函數(shù)*/

void main(void)

{

Init();/*圖形驅(qū)動*/

Choicelevle();/*選擇游戲等級*/

DRAW();/*游戲區(qū)域*/

GamePlay();/*玩游戲具體過程*/

Close();/*圖形結(jié)束*/

}

/*圖形驅(qū)動*/

void Init(void)

{

int gd=DETECT,gm;

initgraph(gd,gm,"\\turboc2"); /*初始化圖形系統(tǒng)*/

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");/*制作人學(xué)號*/

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(level9||level1)

{cleardevice(); /*清除圖形界面*/

setcolor(YELLOW); /*設(shè)置字體顏色*/

settextstyle(0,0,2); /*設(shè)置字體類型*/

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);

}

/*玩游戲具體過程*/

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())/*在沒有按鍵的情況下,蛇自己移動身體*/

{

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;/*畫面上有食物了*/

}

if(food.yes==0)/*畫面上有食物了就要顯示*/

{

setcolor(GREEN);

rectangle(food.x,food.y,food.x+10,food.y-10);

}

for(i=snake.node-1;i0;i--)/*蛇的每個環(huán)節(jié)往前移動*/

{

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;isnake.node;i++)/*從蛇的第四節(jié)開始判斷是否撞到自己了,因為蛇頭為兩節(jié),第三節(jié)不可能拐過來*/

{

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;/*如果蛇頭越過左邊界,則從右邊界進(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;/*如果蛇頭越過右邊界,則蛇頭從左邊界進(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;/*如果蛇頭越過上邊界,則從下邊界進(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;/*如果蛇頭越過下邊界,則從上邊界進(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),重新開始*/

break;

if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以后*/

{

setcolor(0);/*把畫面上的食物東西去掉*/

rectangle(food.x,food.y,food.x+10,food.y-10); /*用當(dāng)前線型和顏色畫一矩形*/

snake.x[snake.node]=-20;snake.y[snake.node]=-20;

/*新的一節(jié)先放在看不見的位置,下次循環(huán)就取前一節(jié)的位置*/

snake.node++;/*蛇的身體長一節(jié)*/

food.yes=1;/*畫面上需要出現(xiàn)新的食物*/

score+=10; /*每吃掉一食物,得分累加10分*/

if(score%100==0)

{level++;gamespeed=100000-400*level-300*level*level;/*每吃掉10食物提升一級,速度加快*/

PrScore();/*輸出新得分*/

setcolor(YELLOW); /*設(shè)置字體顏色*/

settextstyle(0,0,4); /*設(shè)置字體類型*/

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);/*畫出蛇*/

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)

/*判斷是否往相反的方向移動*/

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è)置字體類型*/

outtextxy(200,200,"GAME OVER"); /*顯示文本*/

getch();

}

/*輸出成績及游戲等級*/

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è)置字體類型*/

outtextxy(250,400,"EXIT=ESC ");/*顯示文本*/

}

void Close(void)

{

closegraph();

}

C語言貪吃蛇的控制問題

rand函數(shù)用來產(chǎn)生一個隨機(jī)數(shù)的,%是求余的功能,即產(chǎn)生的隨機(jī)數(shù)除以%后面的數(shù)得到的余數(shù)。

|是或的意思,1|1

=

0|1

=

1|0

=

1,

0|0

=

0;

一個數(shù)如18表示這個數(shù)左移8位得到的數(shù)

一直閃屏是很正常的,控制臺程序基本都這樣

文章題目:c語言貪吃蛇編程控制函數(shù),c語言編程貪吃蛇教程
文章來源:http://chinadenli.net/article14/hsjdge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作建站公司網(wǎng)站營銷Google做網(wǎng)站品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司