C實現貪吃蛇

2021-10-08 21:32:19 字數 1997 閱讀 5642

#include

#include

#include

#include

#define width 25

#define high 15

int movedirection;

//1 2 3 4是小蛇的移動方向,分別表示上下左右

int canvas[high]

[width]=;

//二維陣列儲存遊戲畫布中對應的元素

//0為空格,-1為邊框,1為蛇頭,大於1為蛇身,-2表示食物

int food_x, food_y;

//食物的位置

void

gotoxy

(int x,

int y)

//設定游標位置的函式

void

hidecursor()

//隱藏游標函式

;// 第二個值為0表示隱藏游標

setconsolecursorinfo

(getstdhandle

(std_output_handle)

,&cursor_info);}

void

startup()

for(j =

0; j < width; j++

)//初始化蛇頭蛇身

canvas[high /2]

[width /2]

=1;for

(i =

1; i <

5; i++

) movedirection =4;

//初始化食物的位置

food_x =

rand()

%(high -5)

+2; food_y =

rand()

%(width -5)

+2; canvas[food_x]

[food_y]=-

2;}void

movesnakebydirection()

if(canvas[i]

[j]==2)

//記錄舊蛇頭的位置}}

}int newhead_i, newhead_j;

//新蛇頭的位置

if(movedirection ==1)

//向上

if(movedirection ==2)

//向下

if(movedirection ==3)

//向左

if(movedirection ==4)

//向右

//判斷小蛇是否碰撞到食物,

if(canvas[newhead_i]

[newhead_j]==-

2)else

//判斷蛇頭撞到邊框或者自身,遊戲失敗

if(canvas[newhead_i]

[newhead_j]

>

0|| canvas[newhead_i]

[newhead_j]==-

1)else

}void

show()

else

if(canvas[i]

[j]==-1

)else

if(canvas[i]

[j]==1)

else

if(canvas[i]

[j]>1)

else

if(canvas[i]

[j]==-2

)}printf

("\n");

}sleep

(300);

}void

updatewithoutinput()

void

updatewithinput()

if(input ==

'd')

if(input ==

'w')

if(input ==

's')}}

intmain()

return0;

}

c 實現貪吃蛇

include include include include include include include include include word square color 7 義方向 define key up 72 define key down 80 define key left 75...

貪吃蛇 c 實現

週末無聊,嘗試寫了下貪吃蛇。先上 include include include include include includeusing namespace std define up 72 define down 80 define left 75 define right 77 struct ...

C 實現貪吃蛇

vs 2015 easyx 蛇能上下左右移動 蛇能吃食物 能判斷蛇的死亡 蛇的長度,每節蛇的座標,蛇移動的方向 蛇初始化,移動,改變方向,吃食物,畫蛇,蛇是否死亡 食物的座標,食物是否被吃掉 初始化食物,新的食物,畫食物 因為蛇吃食物時需要知道食物的座標,所以需要獲得食物座標的方法 因為蛇吃食物後需...