用到了佇列的出隊和入隊,對佇列不清楚的可以網上看看關於什麼是佇列
注:其中用到了雙緩衝來解決閃屏問題
效果如下
話不多說,**如下:
/********************
*佇列思想,根據每移動就吧下乙個位置壓入佇列,把尾部位置變為空,並出隊
* *
*****************/
#include
#include
#include
#define width 30
//蛇移動的螢幕陣列的寬高
#define hight 20
#define wall 1
//牆#define snake 2
//蛇,空用0表示
#define food 3
//食物
#define vk_up 72
//方向鍵上下左右的第二個值,方向鍵有兩個值,第乙個是224,分別是72,...
#define vk_down 80
#define vk_left 75
#define vk_right 77
struct snakenode
;struct snakequeue
;typedef
struct snakequeue * psnakequeue;
typedef
struct snakenode * psnakenode;
psnakequeue creakesnakequeue
(int m)
;//建立乙個空隊
intensnakequeue
(psnakequeue p,
int x,
int y)
;//將新的結點壓入蛇佇列
intdesnakequeue
(psnakequeue p)
;//出隊,刪除隊頭元素
intfrontsnakequeue
(psnakequeue p,
int*a)
;//取隊頭元素,取隊尾蛇的結點的x,y,把x賦給a[0],y賦給a[1]
psnakequeue creakesnakequeue
(int m)
else
free
(p);
}return
null;}
intensnakequeue
(psnakequeue p,
int x,
int y)
return1;
}int
desnakequeue
(psnakequeue p)
}int
frontsnakequeue
(psnakequeue p,
int*a)
return1;
}//以上是佇列的基本操作,可以單獨寫成乙個.c檔案
int arr[hight]
[width]
;int fangxiang = vk_right;
int x =
5,y =
4,score =0;
//蛇頭的橫縱座標 ,成績
void
init()
;//初始化的一些操作
void
movesnake
(psnakequeue p,
int fx)
;//蛇的移動
void
fangxiangchoice()
;void
producefood()
;void
setcursorposition
(int x,
int y)
;//設定游標的位置
void
main()
}void
init()
}srand
(time
(null))
;//螢幕輸出的初始化
printf
("\n請按方向鍵 ↑ ↓ ← → 的控制上下左右 ,退出按esc鍵\n");
for(i =
0; i < hight; i++)}
}producefood()
;setcursorposition
((width+1)
*2,5
);printf
(" 得分:%d"
, score);}
void
movesnake
(psnakequeue p,
int fx)
if(arr[y]
[x]==0)
else
if(arr[y]
[x]== food)
else
}void
fangxiangchoice()
}}void
producefood()
arr[fy]
[fx]
= food;
setcursorposition
((fx+1)
*2,fy+3)
;printf
("●");
}void
setcursorposition
(int x,
int y)
;setconsolecursorposition
(houtput, pos)
;}
C語言貪吃蛇
include include include include include include define screen width 40 遊戲螢幕寬度 define screen length 15 遊戲螢幕長度 define start x 16 螢幕起始x座標 define start y ...
C語言 貪吃蛇
include include include define framewidght 22 define frameheight 22 short generaterandnumber short a,short b void posconsolecursor short x,short y voi...
C語言貪吃蛇
親測devc 編譯器完美執行 define crt secure no warnings include include include include void readini file fphead,int score,char argv 建立或開啟乙個和執行檔案對應的ini檔案,讀取最高紀錄 ...