上一篇的贪吃蛇由于是把蛇存在数组里面输出的时候有闪屏
所以就写了这篇 用easyx做的图形界面
工具 :vscode + easyX (dev运行不了 现在开发项目都是vscode哦 没有的话下载一个)
前方高能****
实在是太巨了 用easyx不仅画面比较美观,而且运用了Ai
自己一个人就可以和AI玩啦 ,我自己体验了 一下确实厉害
鼠标控制蛇的运动(记住哈) ,然后就和微信上的小程序贪吃蛇差不多啦
链接入口
代码的话和上一篇基本没什么区别
就是蛇的话是用方块画出来的需要2个点 ,所以蛇的结构体就变了 ,后面的判断食物是否被吃 还要 蛇的运动坐标上就改变了一点 ,需要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(); //清屏
//因为在ezsyx中蛇是矩形画出来的 所以要判断 矩形和圆形的食物是否重合(坐标是否在一个点上)
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); //4个坐标 就是矩形的左顶点和 右下的点 画出来
}
circle(y,x,5);
dely(z);
}
void f() //方向函数吧 不知道起名啥了
{
direction = k;
head++; //矩形画图是4个点 所以要改变4个点
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(); //和getchar类似 得到键盘的一个字符 不会的百度哦
system("cls"); // 清屏
initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素
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;
}
最后后面还要有一篇带彩色的贪吃蛇
上链接
入口
最后 :不过我觉得我的代码应该还是最好懂的叭