字元遊戲 貪吃蛇

2021-08-14 03:55:23 字數 1758 閱讀 8672

貪吃蛇遊戲的設計思路很簡單,相信有過一些程式設計經驗的同學都不至於束手無策,可在我剛剛接觸程式設計時,這個小小的貪吃蛇遊戲可是讓我費了不少腦筋,即使軟導老師已經把偽**告訴了我們,我還是花費了好大的功夫。

話不多說,我們現在就開始吧,首先我們整理一下思路。首先列印地圖,然後用兩個一維陣列來表示蛇頭的座標,對蛇身而言用類似佇列的方法,在進行操作後,身體上每一塊都標名其更前(靠進頭部為前)一塊的相對位置。其中有個生產食物的函式,我們利用隨機數生成函式生成食物座標,並且判斷蛇頭是否觸碰到食物。同時為了實現蛇的自動行走我們還需要用到while(!kbhit()) +sleep()的類似結構。下面是我的**。

#include

#include

#include

#include

#include

#include

#define snake_head 'h'

#define snake_body 'x'

#define blank_cell ' '

#define wall_cell '*'

#define food $

void snake_move();

void output();

int gameover();

void creat_food();

void move(char choice, int snake_location_y, int snake_location_x, char map[12]);

void gotoxy(int x, int y);

char map[12][12] = ;

int snake_length = 5;

int snake_location_x[144] = ;

int snake_location_y[144] = ;

int food_x;

int food_y;

int main()

else

while (!kbhit())

else }}

return 0;

}void snake_move()

}int gameover()

if (snake_location_y[0] == 10 || snake_location_y[0] == 0)

for (int i = 1; i < snake_length; i++)

}return 1;

}void output()

printf("\n");}}

void creat_food()

map[food_y][food_x] = '$';

}void move(char choice, int snake_location_y, int snake_location_x, char map[12])

if (choice == 's')

if (choice == 'a')

if (choice == 'd')

if (snake_location_x[0] == food_x && snake_location_y[0] == food_y)

}void gotoxy(int x, int y)

由於筆者採用的是重新整理螢幕的方法,所以在遊戲的過程中會出現閃屏的現象,為了解決這個問題,我們需要用到區域性重新整理的方法,那為何我不用呢?因為博主還不會....

字元遊戲 貪吃蛇

貪吃蛇是一款十分經典的遊戲。下面介紹一下字元版本貪吃蛇。偽 框架 輸出字元矩陣 while not 遊戲結束 do ch 等待輸入 case ch do a 左前進一步,break d 右前進一步,break w 上前進一步,break s 下前進一步,break end case 輸出字元矩陣 e...

字元遊戲 貪吃蛇 智慧型

先借由自頂向下之邏輯使用偽程式碼方式將架構完成 include int char void 生成地圖 void 生成蛇 void 生成食物 void 蛇移動 void 畫面重新整理 void 吃 void 避免覆蓋 void 碰撞 void 輸入 生成地圖 生成蛇 生成食物 蛇移動 畫面重新整理 吃...

字元遊戲 貪吃蛇簡介

小時候玩過的貪吃蛇是這樣的 或者是這樣的 而我現在能打出來的卻是 只能說是 理想很豐滿,現實很骨感。這兩周的作業都是 貪吃蛇 這對乙個小白來說簡直是痛不欲生,因為不會做!所以網上查了很多資料以及看了很多dalao的文章,最後才勉勉強強打出走了很多彎路的貪吃蛇,見笑了。首先是移動 const int ...