C 控制台貪吃蛇

2021-07-10 14:04:14 字數 4631 閱讀 1128

軟體實訓作品, 現放出個人源**, 供需要的同學參考

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

int endy = starty + rangey; //y終點

bool issnake[rangey + 10 ][rangex + 10]; //標記蛇身

int speed; //休眠時間,控制蛇前進速度; 值越小, 蛇越快

list

snake; //蛇身位置結點

int curdiraction; //蛇的當前前進方向, 1上, 2下, 3左, 4右

int score; //當前分數

int snakelen; //蛇的長度

int foodx, foody; //食物座標

int gox, goy; //蛇頭座標

void goto(int x, int y); //定位游標

void drawboard(); //繪製邊框

void showinformation(); //展示遊戲資訊

void init(); //初始化遊戲

void runsnake(int x, int y); //繪製蛇身

void try(int& x, int& y); //試走

bool isgoodcoord(int x, int y); //前進座標是否合法

void addfood(); //增加食物

void eartfood(); //吃食物

void initsnake(); //初始化蛇身

bool endgame(); //結束遊戲

bool startgame(); //啟動遊戲

bool gamemenu(); //遊戲選單

void showranking(); //排行榜

void showabout(); //相關資訊

void inputdata(); //輸入玩家名字

int main()

return0;}

node::node(int x1, int y1)

bool

operator

< (userdata a, userdata b)

userdata::userdata(string s, int sc)

void goto(int x, int y) ;

setconsolecursorposition(getstdhandle(std_output_handle), coord);

}void showinformation()

void drawboard() ; //游標資訊

setconsolecursorinfo(hout, &cursor_info); //隱藏游標

coord size = ;

setconsolescreenbuffersize(hout, size); //重設緩衝區大小

small_rect rc = ;

setconsolewindowinfo(hout, true, &rc); //重設視窗大小

setconsoletextattribute(hout, cyan);

for (int i = startx - 2; i <= endx + 2; i += 2)

for (int i = starty - 1; i <= endy + 1; ++i)

}void init()

void runsnake(int x, int y)

snake.push_front(node(x, y));

issnake[y][x] = true;

goto(x, y);

printf("■");

node back = snake.back();

snake.pop_back();

issnake[back.y][back.x] = false;

goto(back.x, back.y);

printf(" ");

}void try(int& x, int& y) }}

if (curdiraction == 1) --y; //使用者沒有輸入時

else

if (curdiraction == 2) ++y;

else

if (curdiraction == 3) x -= 2;

else x += 2;

}bool isgoodcoord(int x, int y)

void addfood()

goto(foodx, foody);

printf("★");

}void eartfood()

void initsnake()

}bool endgame()

}bool startgame()

inputdata();

if (endgame()) return

false;

return

true;

}bool gamemenu() }}

}void showranking()

fclose(fp);

sort(vu.begin(), vu.end()); //對得分進行排名

system("cls");

drawboard();

setconsoletextattribute(getstdhandle(std_output_handle), cyan);

goto(startx + 8, starty + 2);

printf("使用者");

goto(startx + 28, starty + 2);

printf("分數");

goto(startx + 48, starty + 2);

printf("排行");

setconsoletextattribute(getstdhandle(std_output_handle), yellow);

for (int i = 0; i < len && i < 10; ++i)

goto(startx + 4, endy - 2);

printf("----------------- 按'1'返回遊戲選單 ---------------");

while (true)

}}void showabout()

}}void inputdata() ;

setconsolecursorposition(getstdhandle(std_input_handle), coord);

while (true)

file *fp = fopen("gamedata.txt", "a");

if (fp == null) fp = fopen("gamedata.txt", "w+");

fprintf(fp, "%s %d\n", name, score);

fclose(fp);

}

C 控制台貪吃蛇

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

貪吃蛇 控制台

本文把遊戲區域就行編號,第一行從0到width 1,到height 1 到 width height 1 二維陣列 並用trace len 陣列儲存snake移動的軌跡 儲存的是數值,數值就能表現出所在的行和列 trace 0 始終為snake的頭部 根據display 函式繪圖,延時,在繪圖,達到...

C語言控制台貪吃蛇

貪吃蛇 這個蛇是怎麼變長的.遊戲就是方塊和空格的更新 沒有牆的地方初始化為0,有牆的地方初始化為1,有蛇的地方初始化為2,食物初始化為3 所有的操作都在snakegroup裡面進行 include include include include include include include con...