生活随笔
收集整理的這篇文章主要介紹了
贪吃蛇2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇的貪吃蛇由于是把蛇存在數組里面輸出的時候有閃屏
所以就寫了這篇 用easyx做的圖形界面
工具 :vscode + easyX (dev運行不了 現在開發項目都是vscode哦 沒有的話下載一個)
先介紹一下牛逼人寫的貪吃蛇(鏈接里面可自行查看代碼并運行)
前方高能****
實在是太巨了 用easyx不僅畫面比較美觀,而且運用了Ai
自己一個人就可以和AI玩啦 ,我自己體驗了 一下確實厲害
鼠標控制蛇的運動(記住哈) ,然后就和微信上的小程序貪吃蛇差不多啦
鏈接入口
自己的貪吃蛇(界面比較丑。。)
代碼的話和上一篇基本沒什么區別
就是蛇的話是用方塊畫出來的需要2個點 ,所以蛇的結構體就變了 ,后面的判斷食物是否被吃,還有 蛇的運動坐標上就改也變了
然后自己的代碼(圖形界面有點丑)
(大家感興趣可以調一下)
#include <easyx.h>
#include <conio.h>
#include<conio.h>
#include<ctime>
#include<iostream>using namespace std
;
int N
= 64, M
= 48, direction
= 75, k
= 77, grade
= 0;
int head
= 4, tail
= 1, leng
, x
, y
,xx
,yy
, z
= 50000000, game
= 1,ll
= 0;
struct note
{int x1
, y1
,x2
,y2
;
}snake
[1000000]; void dely(int kk
)
{while (kk
--);
}void random()
{srand((unsigned int)time(NULL));grade
+= 20;x
= rand() % M
* 10; y
= rand() % N
* 10;xx
= (snake
[head
].x1
+ snake
[head
].x2
) / 2;yy
= (snake
[head
].y1
+ snake
[head
].y2
) / 2;while (xx
== x
&& yy
== y
){x
= rand() % M
* 10; y
= rand() % N
* 10;}
}
void start()
{snake
[1].x1
= 5, snake
[1].y1
= 5, snake
[1].x2
= 15,snake
[1].y2
= 15;snake
[2].x1
= 5, snake
[2].y1
= 15 , snake
[2].x2
= 15, snake
[2].y2
= 25;snake
[3].x1
= 5, snake
[3].y1
= 25, snake
[3].x2
= 15, snake
[3].y2
= 35;snake
[4].x1
= 5, snake
[4].y1
= 35 , snake
[4].x2
= 15, snake
[4].y2
= 45;
}int gameover()
{xx
= (snake
[head
].x1
+ snake
[head
].x2
) / 2; yy
= (snake
[head
].y1
+ snake
[head
].y2
) / 2;if (xx
<0 || yy
< 0 || xx
> M
* 10 || yy
>N
* 10) return 0;return 1;
}
void display()
{ cleardevice(); xx
= (snake
[head
].x1
+ snake
[head
].x2
) / 2;yy
= (snake
[head
].y1
+ snake
[head
].y2
) / 2;if (x
== xx
&& yy
== y
){random();tail
--;}for (int i
= tail
; i
<= head
; i
++){rectangle(snake
[i
].y1
, snake
[i
].x1
, snake
[i
].y2
, snake
[i
].x2
); }circle(y
,x
,5);dely(z
);
}void f()
{ direction
= k
;head
++; if (direction
== 72) snake
[head
].x1
= snake
[head
- 1].x1
- 10, snake
[head
].x2
= snake
[head
- 1].x2
- 10, snake
[head
].y1
= snake
[head
- 1].y1
, snake
[head
].y2
= snake
[head
- 1].y2
; if (direction
== 80) snake
[head
].x1
= snake
[head
- 1].x1
+ 10, snake
[head
].x2
= snake
[head
- 1].x2
+ 10,snake
[head
].y1
= snake
[head
- 1].y1
, snake
[head
].y2
= snake
[head
- 1].y2
; if (direction
== 75) snake
[head
].x1
= snake
[head
- 1].x1
, snake
[head
].x2
= snake
[head
- 1].x2
,snake
[head
].y1
= snake
[head
- 1].y1
-10, snake
[head
].y2
= snake
[head
- 1].y2
- 10; if (direction
== 77) snake
[head
].x1
= snake
[head
- 1].x1
, snake
[head
].x2
= snake
[head
- 1].x2
,snake
[head
].y1
= snake
[head
- 1].y1
+ 10, snake
[head
].y2
= snake
[head
- 1].y2
+ 10; tail
++;if (!gameover()){game
= 0;}else display();
}int main()
{cout
<< "\n\n\n\n\n\t\t\t 歡迎進入貪吃蛇游戲!" << endl
;cout
<< "\n\n\n\t\t\t 按任意鍵馬上開始。。。" << endl
;_getch(); system("cls"); initgraph(640, 480); setbkcolor(WHITE
); cleardevice(); setlinecolor(RED
); start();display(); random(); while (game
) {if (!_kbhit()){f();}else{k
= _getch();}}cout
<< "\n\n\n\n\n\t\t\t\t" << "你的成績 :"<< grade
;cout
<< "\n\n\n\n";return 0;
}
最后后面還要有一篇帶彩色的貪吃蛇
上鏈接
入口
最后 :不過我覺得我的代碼應該還是最好懂的叭
總結
以上是生活随笔為你收集整理的贪吃蛇2的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。