貪吃蛇 C語言實現

2021-09-06 16:16:43 字數 3564 閱讀 7880

首先

必備知識棧:(由於是用c實現,c的基礎就不廢話了)

1.鍊錶相關知識的熟練使用

2.動態記憶體的簡單使用與釋放方法

3.隨機數的生成與儲存

4.監聽鍵盤函式的使用

5.window.h中基本的控制台函式

6.時間函式的簡單應用

其次:下面是實現的具體**:(有詳細的注釋解釋)

#pragma comment(lib,"user32.lib")

#include#include#include#include#include#include#pragma comment(lib,"winmm.lib")

#define u 1

#define d 2

#define l 3

#define r 4

typedef struct node

node;

node *head,*food,*index; //頭,食物,游標

void snake_create(); //蛇

void wall_create(); //牆

void pos(int,int); //游標位置

void game_description(); //遊戲說明介面

void snake_move(); //移動控制

void game_control(); //遊戲控制

void start_game(); //開始遊戲

void pause_game(); //暫停遊戲

void exit_game(); //退出遊戲

void bitself(); //咬自己

void bitwall(); //撞到牆

void spawn_food(); //出現食物

int score = 0, add = 10, death_type, direction = r, speedtime = 200;

void pos(int x, int y) //設定游標位置

void game_description() //遊戲描述

void wall_create() //生成牆

for(i = 1;i < 26;i++) //列印左右邊框 }

void snake_create() //生成蛇

while (tail != null) //由頭到尾,輸出蛇身 }

void spawn_food() //生成食物

node* food_1 = (node*)malloc(sizeof(node));

food_1 -> x = rand() % 52 + 2;

food_1 -> y = rand() % 24 + 1; //確保食物在牆的範圍內

while( (food_1 -> x) % 2 != 0)

index = head;

while( index != null)

index = index -> next;

} food = food_1;

pos(food -> x,food -> y);

printf("●");

}void snake_move() //蛇的移動

nexthead -> next = head;

head = nexthead;

index = head;

bitself();

if(nexthead -> x == food -> x && nexthead -> y == food -> y)

score += add;

free(food);

food = null; //吃到食物就將其賦空

spawn_food();

} else //前進

pos(index -> next ->x ,index -> next -> y);

printf(" ");

free(index -> next);

index -> next = null;

} }

void game_control() //控制小蛇

else if(getasynckeystate(vk_down) && direction != u)

else if(getasynckeystate(vk_right) && direction != l)

else if(getasynckeystate(vk_left) && direction != r)

else if(getasynckeystate(vk_escape))

else if(getasynckeystate(vk_space))

else if(getasynckeystate(vk_f1)) //加速

}} else if(getasynckeystate(vk_f2)) //減速

}sleep(speedtime);

snake_move();

} }void pause_game() //暫停 }}

void bitwall() //撞牆

}void bitself() //咬自己

self = self -> next; }}

void exit_game() //退出遊戲

else if(death_type == 2)

else if(death_type == 3)

pos(24, 13);

printf("你的得分是%d\n",score);

getchar();

exit(0);

}int main()

下面是**的實現:

進入

開始之後,按空格鍵暫停

可以按f1和f2實現速度的改變

遊戲結束介面

小總結:

參考了網上的小思路,可以按f1 和 f2 來實現小蛇的加速和減速,這個其實是儲存了乙個速度,通過函式的來改變即可,不過卻很巧妙。

總體來說,貪吃蛇難度不大,但其需要我們對基本知識有乙個較為全面的把握,不失為作為全面回顧的好案例,對於c初學者而言比較友好。

end

附上乙隻超級可愛的小貓咪,真的超級可愛

C語言實現貪吃蛇

貪吃蛇是我們耳熟能詳的遊戲,遊戲思路很簡單,我們操縱一條蛇在螢幕上游走尋找食物,當吃到食物時蛇就會變長,當蛇頭碰到牆壁或者自己的身體時,此時蛇就會死亡,而遊戲也就結束了。那麼我們設計遊戲的時候,需要注意三點 1 及時重新整理蛇的位置 2 蛇吃到食物時,蛇的身體會變長,同時及時重新整理新的食物 3 蛇...

C語言實現貪吃蛇

include include include include include define up w define down s define left a define right d define quit q 函式宣告 void welcome 開始介面 void chose 選擇難度 vo...

C語言實現貪吃蛇

include include easyx圖形庫 include include 視窗大小 enum wincoord 座標 typedef struct coordinate coord 蛇struct snake snake 食物 struct food food 方向 enum positio...