c 實現貪吃蛇

2021-07-31 05:06:26 字數 4777 閱讀 4226

```

#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

#define key_right 77

#define key_esc 27

//邊框屬性

#define mapw 30

#define maph 20

#define offset_l 2

#define offset_u 2

using

namespace

std;

class bitmap;

class feed;

class snake;

//獲取控制代碼函式

handle initiate()

//控制台輸出函式

bool textout(handle houtput, int x, int y, lptstr lpszctring,word wcolors, int ncolors)

if (!fsuccess)

cout

<< "error:writeconsoleoutputcharacter"

<< endl;

return

0;}//end

bool textout1(handle houtput,int x,int y,lptstr lpszctring)

enum type ;

extern handle handle;

//列印遊戲介面

template

void print(t &t, enum type ty)

for (t::iterator it = t.begin();it != t.end();++it)

textout(handle, offset_l + 2 * it->coord.first, offset_u + it->coord.second, lpsztext, square_color+it->color,1);

}//node

struct node

friend

inline

bool

operator

<(const node &lhs, const node &rhs);

};//比較兩個節點是否相同的函式

bool equalnode(node node1, node node2)

//運算子過載(因為set需要排序)

inline

bool

operator

<(const node &lhs, const node &rhs)

else

if (lhs.coord.second == rhs.coord.second)

else

}//bitmap 類

class bitmap

void printbitmap();

void insert(pair edge);

private:

int x, y, w, h, c;

setbm;

};void bitmap::insert(pair edge)

void bitmap::printbitmap()

//feed類

class feed ;

void feed::printfeed()

class snake ;

snake(pair head, pair tail);

void printsnake();

void erasesnake();

void movefront(pair t);

bool nextstep(direction d,bitmap &b,feed &fd,bool &eat);

void snake::addnewfeed(bitmap &b, feed &f);

direction getdir()

bool changedirection(direction d, bitmap &b, feed &f, bool &eat); //判斷方向改變是否合法

private:

deque

sn;direction dir;

void addfrontnode(node n, feed &f);

};snake::snake(pair head, pair tail)

else

if (head.second > tail.second)

else

throw runtime_error("無法繪蛇");

}else

if (head.second == tail.second)

else

if (head.first < tail.first)

}else

throw runtime_error("無法繪蛇");

}void snake::printsnake()

void snake::erasesnake()

void snake::addfrontnode(node n, feed &f)

void snake::movefront(pair t)

else

}}bool snake::nextstep(direction d, bitmap &b, feed &f, bool &eat)

dir = d;

for (set

::iterator it = b.bm.cbegin();it != b.bm.cend();it++)

if (equalnode(*it, node(temp.first, temp.second,6)))

return

false;

for (deque

::iterator it = sn.begin();it != sn.end() - 1;it++)

if (equalnode(*it, node(temp.first, temp.second,6)))

return

false;

for (set

::iterator it = f.fd.begin();it != f.fd.end();it++)

}erasesnake();

movefront(temp);

printsnake();

return

true;

}bool snake::changedirection(direction d, bitmap &b, feed &f, bool &eat)

void snake::addnewfeed(bitmap &b, feed &f)

ok = true;

}if (!ok) continue;

for (it = f.fd.begin();it != f.fd.end(); it++)

ok = true;

}if (!ok) continue;

deque

::iterator dit;

for (dit = sn.begin();dit != sn.end();dit++)

ok = true;

}if (!ok) continue;

f.fd.insert(node(temp.first, temp.second,rand()%7));

break;

}}handle handle;

void init(bitmap &b,feed &feed,snake &s)

for (int j = 1; j < maph - 1; j++)

b.printbitmap();

for (int i = 0;i < 3;++i)

s.addnewfeed(b, feed);

feed.printfeed();

s.printsnake();

lptstr text1 = text("score:");

lptstr text2 = text("level:");

lptstr text = text("❤按任意鍵開始!❤");

textout1(handle, 1, 1, text);

textout1(handle, offset_l + 2*mapw , offset_u, text1);

textout1(handle, offset_l + 2 * mapw, offset_u + 5, text2);

char c = _getch();

}int main()

if (!ret)

if (eat) }}

sleep(speed);

delay++;

} //while (delay < max_delay)

ret = mysnake.nextstep(mysnake.getdir(), mybitmap, myfeed, eat);

if (!ret)

if (eat)

}}//while(1)

return

0;}

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

C實現貪吃蛇

include include include include define width 25 define high 15 int movedirection 1 2 3 4是小蛇的移動方向,分別表示上下左右 int canvas high width 二維陣列儲存遊戲畫布中對應的元素 0為空格,...