C 貪吃蛇 多執行緒版(原始碼)

2021-10-03 01:17:14 字數 2059 閱讀 6360

在家閒著蛋疼寫了個貪吃蛇,供諸君品鑑~

ps:以後抽空補個詳解~

pps:還寫了個帶花樣帶難度的貪吃蛇,等有空放出來~

#include #include #include #include #include #include #include using namespace std;

typedef pairpii;

const int maxn = 100;

mutex mtx;//互斥鎖

/*執行緒1:獲取鍵盤資訊

執行緒2:獲取時間資訊

執行緒3:監視建立食物

執行緒4:顯示遊戲資訊

*/int direction;//方向:1:上 2:下 3:左 4:右

int state;//狀態:1:未獲得食物 2:獲得食物 3:暫停 4: 死亡

queuesnake;//蛇身

pii head;//蛇頭

int m_x, m_y;//地圖大小

int _time;//時間

int score;//分數

bool need_food;//是否需要建立食物

char mp[maxn][maxn];//地圖對映

//游標操作

handle hout = getstdhandle(std_output_handle);

coord coord;

//隱藏游標

void hide()

; setconsolecursorinfo(hout, &cursor_info);

}//定位游標

void locate(int x, int y)

;//建立圍牆

void create_wall()

for (int j = 0; j <= m_y; j++)

}//建立食物

void create_food() }}

//初始化

void init()

void read()

while (_kbhit())

else if (key == 80)

else if (key == 75)

else if (key == 77)

else if (key == '1')

else if (key == '2')

} }}void update()

if (state == 3)

a = clock();

while (1)

} next = head;

if (direction == 1)

else if (direction == 2)

else if (direction == 3)

else if (direction == 4)

if (state == 1)

else if (state == 2)

mtx.lock();

locate(head.first, head.second);

cout << 'o';

mtx.unlock();

mp[head.first][head.second] = 'o';

snake.push(head);

head = next;

char now_c = mp[next.first][next.second];

mtx.lock();

locate(head.first, head.second);

if (now_c == 'f')

else if (now_c == 'o' || now_c == '#')

else

mtx.unlock();

_time++; }}

void monitor_food()

if (need_food) }}

void info()

mtx.lock();

locate(m_x + 2, 0);

mtx.unlock(); }}

int main()

貪吃蛇原始碼

去年五一寫的乙個貪吃蛇遊戲,在tc3下執行成功,過幾天加點注釋 大家先湊和看吧.hoho.include include include include include include define vk esc 0x11b define vk up 0x4800 define vk down 0x...

貪吃蛇原始碼(C語言版)

貪吃蛇其實就是實現以下幾步 1 蛇的運動 通過 畫頭擦尾 來達到蛇移動的視覺效果 2 生成食物 3 蛇吃食物 實現 畫頭不擦尾 4 遊戲結束判斷 也就是蛇除了食物,其餘東西都不能碰 include include include include include define width 60 def...

java貪吃蛇原始碼

業務分析 一 找物件 塊 組成貪吃蛇的組成元素和每次隨機生成的塊。cell 貪吃蛇 可以考慮用陣列表示,不單獨設定乙個類 主窗體物件 tcs 二 分析類 cell x y width height image up dpwn left right tcs 各種變數,c 存下次出現的塊 she 存貪吃...