真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

go語(yǔ)言寫貪吃蛇 貪吃蛇編寫程序

貪吃蛇代碼中我看到下面這處初始化蛇身的函數(shù)就懵了,還望高手指教!

這個(gè)是命令行界面的程序吧。這個(gè)程序本質(zhì)就是移動(dòng)光標(biāo)到合適的坐標(biāo)位置,打印@,對(duì)吧。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、漢臺(tái)網(wǎng)站維護(hù)、網(wǎng)站推廣。

而這里的坐標(biāo),你可以想象一下整個(gè)屏幕都是字符。這個(gè)坐標(biāo)就是字符所在的位置(第一個(gè)字符是(0,0),向右x增加,向下y增加)。

你要在命令行界面上面做游戲,所以你要先定義你的游戲窗口大小,就是frame_height,frame_width。

gotoxy(x,y)這個(gè)函數(shù)是移動(dòng)光標(biāo)的函數(shù),將光標(biāo)移動(dòng)到指定行y和列x。

snake.x[0]=frame_height/2;//將蛇初始化在框的正中央

snake.y[0]=frame_width/2;//同上

所以,snake.x[0],snake.y[0]的值表示的是打印蛇頭的位置。這邊我感覺(jué)作者x,y自己賦值反了吧。

for(k=1;ksnake.len;k++)//設(shè)置蛇身長(zhǎng)度及實(shí)時(shí)變化

{

snake.x[k]=snake.x[k-1]+1;//*

snake.y[k]=snake.y[k-1];//*

gotoxy(snake.x[k],?snake.y[k]);

printf("@");

}

循環(huán)這邊就繼續(xù)打印蛇身了。x的坐標(biāo)加+,y的坐標(biāo)保持不變,然后打印第二個(gè)@。再循環(huán)打印一次第3個(gè)@。

貪吃蛇c語(yǔ)言代碼

#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;/*游戲速度自己調(diào)整*/

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;/*蛇移動(dòng)方向*/

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

}snake;

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

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

void DrawK(void);/*開(kāi)始畫(huà)面*/

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

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

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

/*主函數(shù)*/

void main(void)

{

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

DrawK();/*開(kāi)始畫(huà)面*/

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

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

}

/*圖形驅(qū)動(dòng)*/

void Init(void)

{

int gd=DETECT,gm;

initgraph(gd,gm,"c:\\tc");

cleardevice();

}

/*開(kāi)始畫(huà)面,左上角坐標(biāo)為(50,40),右下角坐標(biāo)為(610,460)的圍墻*/

void DrawK(void)

{

/*setbkcolor(LIGHTGREEN);*/

setcolor(11);

setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*設(shè)置線型*/

for(i=50;i=600;i+=10)/*畫(huà)圍墻*/

{

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);/*右邊*/

}

}

/*玩游戲具體過(guò)程*/

void GamePlay(void)

{

randomize();/*隨機(jī)數(shù)發(fā)生器*/

food.yes=1;/*1表示需要出現(xiàn)新食物,0表示已經(jīng)存在食物*/

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;/*節(jié)數(shù)*/

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

while(1)/*可以重復(fù)玩游戲,壓ESC鍵結(jié)束*/

{

while(!kbhit())/*在沒(méi)有按鍵的情況下,蛇自己移動(dòng)身體*/

{

if(food.yes==1)/*需要出現(xiàn)新食物*/

{

food.x=rand()%400+60;

food.y=rand()%350+60;

while(food.x%10!=0)/*食物隨機(jī)出現(xiàn)后必須讓食物能夠在整格內(nèi),這樣才可以讓蛇吃到*/

food.x++;

while(food.y%10!=0)

food.y++;

food.yes=0;/*畫(huà)面上有食物了*/

}

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

{

setcolor(GREEN);

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

}

for(i=snake.node-1;i0;i--)/*蛇的每個(gè)環(huán)節(jié)往前移動(dòng),也就是貪吃蛇的關(guān)鍵算法*/

{

snake.x[i]=snake.x[i-1];

snake.y[i]=snake.y[i-1];

}

/*1,2,3,4表示右,左,上,下四個(gè)方向,通過(guò)這個(gè)判斷來(lái)移動(dòng)蛇頭*/

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é)開(kāi)始判斷是否撞到自己了,因?yàn)樯哳^為兩節(jié),第三節(jié)不可能拐過(guò)來(lá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();/*本次游戲結(jié)束*/

snake.life=1; /*蛇死*/

}

if(snake.life==1)/*以上兩種判斷以后,如果蛇死就跳出內(nèi)循環(huán),重新開(kāi)始*/

break;

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

{

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

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

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

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

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

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

score+=10;

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

}

setcolor(4);/*畫(huà)出蛇*/

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);/*用黑色去除蛇的的最后一節(jié)*/

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)/*如果蛇死就跳出循環(huán)*/

break;

key=bioskey(0);/*接收按鍵*/

if(key==ESC)/*按ESC鍵退出*/

break;

else

if(key==UPsnake.direction!=4)

/*判斷是否往相反的方向移動(dòng)*/

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

settextstyle(0,0,4);

outtextxy(200,200,"GAME OVER");

getch();

}

/*輸出成績(jī)*/

void PrScore(void)

{

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

}

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

void Close(void)

{

getch();

closegraph();

}

貪吃蛇程序設(shè)計(jì)報(bào)告

#includegraphics.h

#includestdlib.h

#includedos.h

#define LEFT 0x4b00

#define RIGHT 0x4d00

#define DOWN 0x5000

#define UP 0x4800

#define ESC 0x011b

int i,key;

int score=0;

int gamespeed=32000;

struct Food /*食物的結(jié)構(gòu)體*/

{

int x; /*食物的橫坐標(biāo)*/

int y; /*食物的縱坐標(biāo)*/

int yes; /*食物是否出現(xiàn)的變量*/

}food;

struct Snack /*蛇的結(jié)構(gòu)體*/

{

int x[N];

int y[N];

int node; /*蛇的節(jié)數(shù)*/

int direction; /*蛇的方向*/

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

}snake;

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

void Close(void); /*關(guān)閉游戲函數(shù)*/

void DrawK(void); /*畫(huà)圖函數(shù)*/

void GameOver(void);/*輸出失敗函數(shù)*/

void GamePlay(); /*游戲控制函數(shù) 主要程序*/

void PrScore(void); /*分?jǐn)?shù)輸出函數(shù)*/

DELAY(char ch)/*調(diào)節(jié)游戲速度*/

{

if(ch=='3')

{

delay(gamespeed); /*delay是延遲函數(shù)*/

delay(gamespeed);

}

else if(ch=='2')

{

delay(gamespeed);

}

}

Menu()/*游戲開(kāi)始菜單*/

{

char ch;

printf("Please choose the gamespeed:\n");

printf("1-Fast 2-Normal 3-Slow\n");

printf("\nPlease Press The numbers..\n");

do

{ch=getch();}

while(ch!='1'ch!='2'ch!='3');

clrscr();

return(ch);

}

/*主函數(shù)*/

void main(void)

{

int ch;

ch=Menu();

Init();

DrawK();

GamePlay(ch);

Close();

}

void Init(void)

{

int gd=DETECT,gm;

initgraph(gd,gm,"c:\\tc");

cleardevice();

}

void DrawK(void)

{

setcolor(11);

setlinestyle(SOLID_LINE,0,THICK_WIDTH);

for(i=50;i=600;i+=10)

{

rectangle(i,40,i+10,49); /*畫(huà)出上邊框*/

rectangle(i,451,i+10,460); /*畫(huà)出下邊框*/

}

for(i=40;i=450;i+=10)

{

rectangle(50,i,59,i+10); /*畫(huà)出左邊框*/

rectangle(601,i,610,i+10); /*畫(huà)出右邊框*/

}

}

void GamePlay(char ch)

{

randomize(); /*隨機(jī)數(shù)發(fā)生器*/

food.yes=1; /*1代表要出現(xiàn)食物,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) /*可以重復(fù)游戲*/

{

while(!kbhit()) /*在沒(méi)有按鍵的情況下蛇自己移動(dòng)*/

{

if(food.yes==1) /*需要食物*/

{

food.x=rand()%400+60;

food.y=rand()%350+60; /*使用rand函數(shù)隨機(jī)產(chǎn)生食物坐標(biāo)*/

while(food.x%10!=0)

food.x++;

while(food.y%10!=0)

food.y++; /*判斷食物是否出現(xiàn)在整格里*/

food.yes=0; /*現(xiàn)在有食物了*/

}

if(food.yes==0) /*有食物了就要顯示出來(lái)*/

{

setcolor(GREEN);

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

}

for(i=snake.node-1;i0;i--) /*貪吃蛇的移動(dòng)算法*/

{

snake.x[i]=snake.x[i-1];

snake.y[i]=snake.y[i-1]; /*貪吃蛇的身體移動(dòng)算法*/

}

switch(snake.direction) /*貪吃蛇的頭部移動(dòng)算法,以此來(lái)控制移動(dòng)*/

{

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++) /*判斷是否頭部與身體相撞*/

{

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) /*如果死亡就退出循環(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); /*吃的食物后用黑色將食物擦去*/

snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*現(xiàn)把增加的一節(jié)放到看不到的地方去*/

snake.node++;

food.yes=1;

score+=10;

PrScore();

}

setcolor(4); /*每次移動(dòng)后將后面的身體擦去*/

for(i=0;isnake.node;i++)

rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);

delay(gamespeed);

DELAY(ch);

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

}

}

void GameOver(void)

{

cleardevice();

setcolor(RED);

settextstyle(0,0,4);

outtextxy(200,200,"GAME OVER");

getch();

}

void PrScore(void)

{

char str[10];

setfillstyle(SOLID_FILL,YELLOW);

bar(50,15,220,35);

setcolor(6);

settextstyle(0,0,2);

sprintf(str,"scord:%d",score);

outtextxy(55,20,str);

}

void Close(void)

{

getch();

closegraph();

}

貪吃蛇

#include "graphics.h"

#include "stdio.h"

#define MAX 200

#define MAXX 30

#define MAXY 30

#define UP 18432

#define DOWN 20480

#define LEFT 19200

#define RIGHT 19712

#define ESC 283

#define ENTER 7181

#define PAGEUP 18688

#define PAGEDOWN 20736

#define KEY_U 5749

#define KEY_K 9579

#define CTRL_P 6512

#define TRUE 1

#define FALSE 0

#define GAMEINIT 1

#define GAMESTART 2

#define GAMEHAPPY 3

#define GAMEOVER 4

struct SPlace

{

int x;

int y;

int st;

} place[MAX];

int speed;

int count;

int score;

int control;

int head;

int tear;

int x,y;

int babyx,babyy;

int class;

int eat;

int game;

int gamedelay[]={5000,4000,3000,2000,1000,500,250,100};

int gamedelay2[]={1000,1};

static int hitme=TRUE,hit = TRUE;

void init(void);

void nextstatus(void);

void draw(void);

void init(void)

{

int i;

for(i=0;iMAX;i++)

{

place[i].x = 0;

place[i].y = 0;

place[i].st = FALSE;

}

place[0].st = TRUE;

place[1].st = TRUE;

place[1].x = 1;

speed = 9;

count = 0;

score = 0;

control = 4;

head = 1;

tear = 0;

x = 1;

y = 0;

babyx = rand()%MAXX;

babyy = rand()%MAXY;

eat = FALSE;

game = GAMESTART;

}

void nextstatus(void)

{

int i;

int exit;

int xx,yy;

xx = x;

yy = y;

switch(control)

{

case 1: y--; yy = y-1; break;

case 2: y++; yy = y+1; break;

case 3: x--; xx = x-1; break;

case 4: x++; xx = x+1; break;

}

hit = TRUE;

if ( ((control == 1) || (control ==2 )) ( (y 1) ||(y = MAXY-1)) ||

(((control == 3) || (control == 4)) ((x 1) ||(x = MAXX-1) ) ) )

{

hit = FALSE;

}

if ( (y 0) ||(y = MAXY) ||

(x 0) ||(x = MAXX) )

{

game = GAMEOVER;

control = 0;

return;

}

for (i = 0; i MAX; i++)

{

if ((place[i].st)

(x == place[i].x)

(y == place[i].y) )

{

game = GAMEOVER;

control = 0;

return;

}

if ((place[i].st)

(xx == place[i].x)

(yy == place[i].y) )

{

hit = FALSE;

goto OUT;

}

}

OUT:

if ( (x == babyx) (y == babyy) )

{

eat = TRUE;

count ++;

score += (1+class) * 10;

}

head ++;

if (head = MAX) head = 0;

place[head].x = x;

place[head].y = y;

place[head].st= TRUE;

if (eat == FALSE)

{

place[tear].st = FALSE;

tear ++;

if (tear = MAX) tear = 0;

}

else

{

eat = FALSE;

exit = TRUE;

while(exit)

{

babyx = rand()%MAXX;

babyy = rand()%MAXY;

exit = FALSE;

for( i = 0; i MAX; i++ )

if( (place[i].st)( place[i].x == babyx) (place[i].y == babyy))

exit ++;

}

}

if (head == tear) game = GAMEHAPPY;

}

void draw(void)

{

char temp[50];

int i,j;

for (i = 0; i MAX; i++ )

{

setfillstyle(1,9);

if (place[i].st)

bar(place[i].x*15+1,place[i].y*10+1,place[i].x*15+14,place[i].y*10+9);

}

setfillstyle(1,4);

bar(babyx*15+1,babyy*10+1,babyx*15+14,babyy*10+9);

setcolor(8);

setfillstyle(1,8);

bar(place[head].x*15+1,place[head].y*10+1,place[head].x*15+14,place[head].y*10+9);

/* for( i = 0; i = MAXX; i++ )

line( i*15,0, i*15, 10*MAXY);

for( j = 0; j = MAXY; j++ )

line( 0, j*10, 15*MAXX, j*10);

*/

rectangle(0,0,15*MAXX,10*MAXY);

sprintf(temp,"Count: %d",count);

settextstyle(1,0,2);

setcolor(8);

outtextxy(512,142,temp);

setcolor(11);

outtextxy(510,140,temp);

sprintf(temp,"1P: %d",score);

settextstyle(1,0,2);

setcolor(8);

outtextxy(512,102,temp);

setcolor(12);

outtextxy(510,100,temp);

sprintf(temp,"Class: %d",class);

setcolor(8);

outtextxy(512,182,temp);

setcolor(11);

outtextxy(510,180,temp);

}

main()

{

int pause = 0;

char temp[50];

int d,m;

int key;

int p;

static int keydown = FALSE;

int exit = FALSE;

int stchange = 0;

d = VGA;

m = VGAMED;

initgraph( d, m, "" );

setbkcolor(3);

class = 3;

init();

p = 1;

while(!exit)

{

if (kbhit())

{

key = bioskey(0);

switch(key)

{

case UP: if( (control != 2) !keydown)

control = 1;

keydown = TRUE;

break;

case DOWN: if( (control != 1) !keydown)

control = 2;

keydown = TRUE;

break;

case LEFT: if( (control != 4) !keydown)

control = 3;

keydown = TRUE;

break;

case RIGHT: if( (control != 3) !keydown)

control = 4;

keydown = TRUE;

break;

case ESC: exit = TRUE;break;

case ENTER: init();break;

case PAGEUP: class --; if (class0) class = 0; break;

case PAGEDOWN: class ++;if (class7) class = 7; break;

case KEY_U: if( ( (control ==1) ||(control ==2)) !keydown)

control = 3;

else if(( (control == 3) || (control == 4)) !keydown)

control = 1;

keydown = TRUE;

break;

case KEY_K: if( ( (control ==1) ||(control ==2)) !keydown)

control = 4;

else if(( (control == 3) || (control == 4)) !keydown)

control = 2;

keydown = TRUE;

break;

case CTRL_P:pause = 1 - pause; break;

}

}

stchange ++ ;

putpixel(0,0,0);

if (stchange gamedelay[class] + gamedelay2[hit])

{

stchange = 0;

keydown = FALSE;

p = 1 - p;

setactivepage(p);

cleardevice();

if (!pause)

nextstatus();

else

{

settextstyle(1,0,4);

setcolor(12);

outtextxy(250,100,"PAUSE");

}

draw();

if(game==GAMEOVER)

{

settextstyle(0,0,6);

setcolor(8);

outtextxy(101,101,"GAME OVER");

setcolor(15);

outtextxy(99,99,"GAME OVER");

setcolor(12);

outtextxy(100,100,"GAME OVER");

sprintf(temp,"Last Count: %d",count);

settextstyle(0,0,2);

outtextxy(200,200,temp);

}

if(game==GAMEHAPPY)

{

settextstyle(0,0,6);

setcolor(12);

outtextxy(100,300,"YOU WIN");

sprintf(temp,"Last Count: %d",count);

settextstyle(0,0,2);

outtextxy(200,200,temp);

}

setvisualpage(p);

}

}

closegraph();

}

具體的編譯和界面還是要靠你。


文章名稱:go語(yǔ)言寫貪吃蛇 貪吃蛇編寫程序
URL鏈接:http://weahome.cn/article/dohgcgp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部