貪吃蛇 控制台

2021-07-11 05:48:17 字數 2725 閱讀 7479

本文把遊戲區域就行編號,第一行從0到width-1,…… 到height-1 到 width*height-1(二維陣列)。並用trace[len]陣列儲存snake移動的軌跡(儲存的是數值,數值就能表現出所在的行和列),(trace[0]始終為snake的頭部),根據display()函式繪圖,延時,在繪圖,達到重新整理螢幕的效果。

; // windows api, 游標位置

hout=getstdhandle(std_output_handle);

setconsolecursorposition(hout,pos); // set

//setconsoletextattribute(hout,0x00|0x05);

}void display()

printf("\n");

}setconsoletextattribute(getstdhandle(std_output_handle), 0x00|0x06); // api, set color

gotoxy(width*2+4, height/3);

printf("\t ↑ ← ↓ → space");

gotoxy(width*2+4, height/5*3);

printf("\t score: %d", len*100);

setconsoletextattribute(getstdhandle(std_output_handle), 0x00|0x07); // api, set color

/* display the rand point */

gotoxy(2*x+2, y+1); // include bound

printf("■"); // 2 char

setconsoletextattribute(getstdhandle(std_output_handle), 0x00|0x02); // api, set color

/* display the snake */

for(i = 0; i < len; i++)

sleep(speed); // delay speed ms

}/* 根據ms的方向進行移位,儲存新的路徑 */

void automove()

trace[i] += ms; /* i = 0 */

}/* 合併新的隨機點 */

int merge()

trace[i] += ms; /* i = 0 */

return

1; }

return0;}

/* born randpoint */

void randpoint()

}void keyinput()

int key = getch(); //獲取鍵盤輸入

if(key == 27)

exit(0);

else

if(key == ' ')

else

if(key == 0xe0)

}automove();

/* judge out of the bound */

if(trace[0] < 1 || trace[0] > height*width || trace[0]%width == width-1 && ms == left

|| trace[0]%width == 0 && ms == right)

gameover();

/* judge head touched the body */

for(i = 1; i < len; i++)

if(trace[0] == trace[i])

gameover();

if(merge() == 1)

randpoint();

display();

}void gameover()

控制台版貪吃蛇

include include include include include 地圖大小 define map size 20 全域性螢幕輸出快取區控制代碼 handle g houtput 地圖屬性 struct mapnode s nodetype e type 全域性地圖資料 mapnode ...

C 控制台貪吃蛇

軟體實訓作品,現放出個人源 供需要的同學參考 int startx 8 x起點 const int starty 4 y起點 const int rangex 60 x範圍 const int rangey 20 y範圍 const int endx startx rangex x終點 const ...

C 控制台貪吃蛇

大二下的遊戲程式設計第乙個小作業,以此篇記錄一下,感謝李仕老師的循循善誘和同學們的熱情討論。include include include using namespace std define leftborder 3 define topborder 3 bool gameover,died 遊戲...