当前位置: 首页 > news >正文

easyx图形库基础4:贪吃蛇

贪吃蛇

  • 一实现贪吃蛇:
    • 1.绘制网格:
    • 1.绘制蛇:
    • 3.控制蛇的默认移动向右:
    • 4.控制蛇的移动方向:
    • 5.生成食物
    • 6.判断蛇吃到食物并且长大。
    • 7.判断游戏结束:
    • 8.重置函数:
  • 二整体代码:

一实现贪吃蛇:

1.绘制网格:

请添加图片描述

#define wide 800
#define high 600#define Frame 20//1.绘制边框
void DrawFrame()
{setlinecolor(BLACK);setlinestyle(PS_SOLID, 1);for (int i = 1; i < wide / Frame; i++){line(Frame * i, 0, Frame * i, 600);}for (int i = 1; i < high / Frame; i++){line(0, Frame * i,800 , Frame * i);}
}

1.绘制蛇:

请添加图片描述

int main()
{initgraph(wide,high);setbkcolor(RGB(188, 227, 245));cleardevice();//初始化蛇的长度int length = 5;//初始化蛇的坐标:sn snake[5] = { {7,6} ,{6,6}, {5,6},{4,6},{3,6} };while (1){cleardevice();BeginBatchDraw();//1.绘制边框DrawFrame();//2.绘制蛇DrawSnake(snake,length);FlushBatchDraw();Sleep(40);}EndBatchDraw();getchar();closegraph();return 0;
}
//2.绘制蛇
void DrawSnake(sn* snake,int length)
{setfillcolor(GREEN);for (int i = 0; i < length; i++){//先绘制头int x1 = snake[i].x;int y1 = snake[i].y;solidrectangle(x1 * Frame, y1 * Frame, (x1 + 1) * Frame, (y1 + 1) * Frame);}
}

3.控制蛇的默认移动向右:

请添加图片描述

void SnakeMove(sn* snake, int length)
{//默认蛇一开始是一直向右移动的;sn newsnake = { (snake->x)+1,(snake->y)};//2.绘制蛇DrawSnake(snake, length);FlushBatchDraw();//进行蛇的移动;for (int i = length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;
}

4.控制蛇的移动方向:

void SnakeMove(sn* snake, int length,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, length);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;
}

5.生成食物

1.食物不可以生成到画布的外面;
2.不可以生成在蛇的身体上面;
3.食物是随机生成的;

//4.生成食物:
void DrawFeeFood(sn* snake, int length, FD* food)
{		int fx = 0;int fy = 0;int flag = 0;while (1){//食物坐标的随机生成:food->x = rand() % (wide / Frame);food->y= rand() % (high / Frame);//2.不可以生成在蛇的身体上面,遍历蛇的身体。for (int i = 0; i < length; i++){if (((food->x) == (snake[i].x)) && ((food->y) == (snake[i].y))){flag++;}}if (flag == 0){break;}}
}
//3.控制蛇的移动
void SnakeMove(sn* snake, int length,FD* food,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, length);//2.绘制食物:if (food->x == 0 && food->y == 0){DrawFeeFood(snake, length, food);}setfillcolor(YELLOW);solidrectangle((food->x) * Frame, (food->y) * Frame, (food->x+1) * Frame, (food->y+1) * Frame);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;
}

6.判断蛇吃到食物并且长大。

请添加图片描述

//3.控制蛇的移动
void SnakeMove(sn* snake, int* length,FD* food,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, *length);//3.绘制食物:if (food->x == 0 && food->y == 0){DrawFeeFood(snake, length, food);}setfillcolor(YELLOW);solidrectangle((food->x) * Frame, (food->y) * Frame, (food->x+1) * Frame, (food->y+1) * Frame);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = *length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;//判断蛇是否吃到了食物和生长:Grow(snake, length, food);
}
void Grow(sn* snake, int* length, FD* food)
{//保存一下尾的数值sn end = snake[(*length) - 1];if ((snake[0].x == food->x) && (snake[0].y == food->y)){//重置食物数值food->x = 0;food->y = 0;//添加尾节点;*length = (*length) + 1;snake[*length] = end;}
}

7.判断游戏结束:

1.蛇头碰到墙壁:
2.蛇头碰到蛇身体:

//5.判断游戏结束
bool Gameover(sn* snake, int* length)
{//1.碰到墙壁if ((snake[0].x >= wide / Frame) || (snake[0].x < 0) || (snake[0].y >= high / Frame) || (snake[0].y < 0)){return true;}else{//2.碰到身体for (int i = 1; i < (*length); i++){if (((snake[0].x)==(snake[i].x))&&((snake[0].y) == (snake[i].y)))return true;}}return false;
}

8.重置函数:

//初始化函数
void initgame(int* length,int* vx,int* vy,sn* snake,FD* food)
{//初始化蛇的长度*length = 5;//初始化蛇的水平初始速度*vx = 1;*vy = 0;//初始化蛇的坐标:snake[0] = { 7,6 };snake[0] = { 6,6 };snake[0] = { 5,6 };snake[0] = { 4,6 };snake[0] = { 3,6 };//初始化食物的坐标;*food = { 0,0 };
}

二整体代码:

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<easyx.h>
#include<math.h>
#include<stdbool.h>
#include<conio.h>
#include<time.h>#define wide 800
#define high 600
#define Frame 20typedef struct snake {int x;int y;
}sn;
typedef struct Food {int x;int y;
}FD;
//1.绘制边框
void DrawFrame()
{setlinecolor(BLACK);setlinestyle(PS_SOLID, 1);for (int i = 1; i < wide / Frame; i++){line(Frame * i, 0, Frame * i, 600);}for (int i = 1; i < high / Frame; i++){line(0, Frame * i,800 , Frame * i);}
}//2.绘制蛇
void DrawSnake(sn* snake,int length)
{setfillcolor(GREEN);for (int i = 0; i < length; i++){//先绘制头int x1 = snake[i].x;int y1 = snake[i].y;solidrectangle(x1 * Frame, y1 * Frame, (x1 + 1) * Frame, (y1 + 1) * Frame);}
}//3.生成食物:
void DrawFeeFood(sn* snake, int *length, FD* food)
{		int fx = 0;int fy = 0;int flag = 0;while (1){//食物坐标的随机生成:food->x = rand() % (wide / Frame);food->y= rand() % (high / Frame);//2.不可以生成在蛇的身体上面,遍历蛇的身体。for (int i = 0; i < *length; i++){if (((food->x) == (snake[i].x)) && ((food->y) == (snake[i].y))){flag++;}}if (flag == 0){break;}}
}//4判断蛇吃到食物并且长大。
void Grow(sn* snake, int* length, FD* food)
{//保存一下尾的数值sn end = snake[(*length) - 1];if ((snake[0].x == food->x) && (snake[0].y == food->y)){//重置食物数值food->x = 0;food->y = 0;//添加尾节点;*length = (*length) + 1;snake[*length] = end;}
}//初始化函数
void initgame(int* length,int* vx,int* vy,sn* snake,FD* food)
{//初始化蛇的长度*length = 5;//初始化蛇的水平初始速度*vx = 1;*vy = 0;//初始化蛇的坐标:snake[0] = { 7,6 };snake[0] = { 6,6 };snake[0] = { 5,6 };snake[0] = { 4,6 };snake[0] = { 3,6 };//初始化食物的坐标;*food = { 0,0 };
}
//5.判断游戏结束
bool Gameover(sn* snake, int* length)
{//1.碰到墙壁if ((snake[0].x >= wide / Frame) || (snake[0].x < 0) || (snake[0].y >= high / Frame) || (snake[0].y < 0)){return true;}else{//2.碰到身体for (int i = 1; i < (*length); i++){if (((snake[0].x)==(snake[i].x))&&((snake[0].y) == (snake[i].y)))return true;}}return false;
}//6.控制蛇的移动
void SnakeMove(sn* snake, int* length,FD* food,int* vx,int* vy)
{//默认蛇一开始是一直向右移动的;//2.绘制蛇DrawSnake(snake, *length);//3.绘制食物:if (food->x == 0 && food->y == 0){DrawFeeFood(snake, length, food);}setfillcolor(YELLOW);solidrectangle((food->x) * Frame, (food->y) * Frame, (food->x+1) * Frame, (food->y+1) * Frame);FlushBatchDraw();//进行蛇的移动;if (_kbhit()){char ch = _getch();//蛇是不可以直接走回头路的:switch (ch){case'A':case'a':if (*vx == 1 && vy == 0)break;*vx = -1;*vy = 0;break;case'W':case'w':if (*vx == 0 && *vy == 1)break;*vx = 0;*vy = -1;break;case'D':case'd':if (*vx == -1 && *vy == 0)break;*vx = 1;*vy = 0;break;case'S':case's':if (*vx == 0 && *vy == -1)break;*vx = 0;*vy = 1;break;}}//控制蛇节点变化sn newsnake = { (snake->x)+(*vx) ,(snake->y)+(*vy)};for (int i = *length - 1; i >= 1; i--){snake[i] = snake[i - 1];}snake[0] = newsnake;//判断蛇是否吃到了食物和生长:Grow(snake, length, food);//判断蛇是否碰到了墙壁和自己if (Gameover(snake, length)){initgame(length, vx, vy, snake, food);//Sleep(100);}}int main()
{initgraph(wide,high);setbkcolor(RGB(188, 227, 245));//获取当前时间作为随机数种子:srand((unsigned int)time(NULL));cleardevice();//初始化蛇的长度int length = 5;//初始化蛇的水平初始速度int vx = 1;int vy = 0;//初始化蛇的坐标:sn snake[100] = { {7,6} ,{6,6}, {5,6},{4,6},{3,6} };//初始化食物的坐标;FD food = {0,0};while (1){cleardevice();BeginBatchDraw();//1.绘制边框DrawFrame();//2.控制蛇的移动和节点的变化;SnakeMove(snake, &length,&food,&vx,&vy);Sleep(150);}EndBatchDraw();getchar();closegraph();return 0;
}
http://www.lryc.cn/news/129105.html

相关文章:

  • 哈夫曼树(赫夫曼树、最优树)详解
  • 智慧建筑工地平台,通过信息化技术、物联网、人工智能技术,实现对施工全过程的实时监控、数据分析、智能管理和优化调控
  • Springboot 实践(8)springboot集成Oauth2.0授权包,对接spring security接口
  • OpenCV-Python中的图像处理-GrabCut算法交互式前景提取
  • leetcode原题 后继者:找出二叉搜索树中指定节点的“下一个”节点
  • pyqt5 QlineEdit 如何设置只能输入数字
  • ubuntu中安装python
  • LeetCode150道面试经典题-- 快乐数(简单)
  • 科研论文配图----第一章笔记
  • OpenHarmony Meetup 广州站 OpenHarmony正当时—技术开源
  • 如何使用PHP Smarty模板实现静态页面生成
  • 【 Cocos Creator 项目实战】益智游戏《2048》(附带完整源码工程)
  • 剑指Offer68-II.二叉树的最近公共祖先 C++
  • 【JAVA】我们该如何规避代码中可能出现的错误?(一)
  • openLayers实战(八):坐标系及其转换
  • DAY06_SpringBoot—简介基础配置yaml多环境开发配置整合第三方技术
  • 无涯教程-Perl - setpwent函数
  • 代码随想录-数组篇
  • vue3+element-plus表格默认排序default-sort失效问题
  • CH32V203 单片机 I2C 使用
  • 链表OJ题
  • Llama 2免费托管及API提供
  • 回到未来:使用马尔可夫转移矩阵分析时间序列数据
  • vue element 多图片组合预览
  • Vue2集成Echarts实现可视化图表
  • 3 Python的数据类型
  • new String()到底创建了几个对象
  • 第五十五天
  • 【推荐】深入浅出benan的生命周期
  • mysql使用redis+canal实现缓存一致性