当前位置:网站首页>C语言贪吃蛇
C语言贪吃蛇
2022-06-22 15:03:00 【小木荣】
一、说明
- 编译环境:vs 2019
- 需要安装 eazyx(几秒钟就行)
- 代码注释详细
- 成像图

二、制作思路
- 界面
大小、颜色 - 蛇的初始化
蛇身形状、长度,出现在地图的位置,一开始往哪个方向移动 - 食物的随机出现
播种:srand((unsigned int)time(NULL));
随机函数:rand() - 蛇的行动操作(移动和吃食物)
WASD
不能直接走与自身方向相反的方向,如:
向右走时不能向左 - 蛇死亡条件
撞边界、撞自己 - 得分的计算
吃食物得分,打印在界面 - 如何画蛇、画食物
三、代码
//在代码中取消 Unicode 编码的宏定义,让后续编译都以 MBCS 编码进行
#undef UNICODE
#undef _UNICODE
#include<stdio.h>
#include<conio.h> // _getch _kbhit
#include<time.h>
#include<graphics.h> //需安装easyX
//界面大小,可直接修改
#define M 600
#define N 400
typedef struct {
int x, y;
}point;//坐标xy,与数学的坐标略有不同
struct snake {
point xy[100];
int position;
int lenth;
}snake;
struct food {
int flag = 0;//判断食物是否存在
point fdxy;
int grade = 0;
}food;
enum position {
up, down, left, right };//枚举
//蛇,初始化蛇的位置
void startsnake()
{
//蛇头
snake.xy[0].x = 20;
snake.xy[0].y = 0;
snake.xy[1].x = 10;
snake.xy[1].y = 0;
snake.xy[2].x = 0;
snake.xy[2].y = 0;
//蛇初始化方向
snake.position = right;
snake.lenth = 3;
}
//画蛇(要安装easy_X),颜色会不断变化
void drawsnake()
{
for (int i = 0; i < snake.lenth; i++)
{
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10);
}
}
void movesnake()
{
//蛇身往前移动一格
for (int i = snake.lenth - 1; i > 0; i--)
{
snake.xy[i].x = snake.xy[i - 1].x;
snake.xy[i].y = snake.xy[i - 1].y;
}
//蛇头方向移动
switch (snake.position)
{
case up:
snake.xy[0].y = snake.xy[0].y - 10; break;
case down:
snake.xy[0].y = snake.xy[0].y + 10; break;
case left:
snake.xy[0].x = snake.xy[0].x - 10; break;
case right:
snake.xy[0].x = snake.xy[0].x + 10; break;
}
}
//随机生成食物
void showfood()
{
food.fdxy.x = rand() % (M/10) * 10;//0~590
food.fdxy.y = rand() % (N/10) * 10;//0~390
//循环用于判断是否与蛇重合
for (int i = 0; i < snake.lenth; i++)
{
if (snake.xy[i].x == food.fdxy.x && snake.xy[i].y == food.fdxy.y)
{
food.fdxy.x = rand() % 60 * 10;
food.fdxy.y = rand() % 40 * 10;
}
}
}
//
void drawfood()
{
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
fillrectangle(food.fdxy.x, food.fdxy.y, food.fdxy.x + 10, food.fdxy.y + 10);
}
void eatfood()
{
if (snake.xy[0].x == food.fdxy.x && snake.xy[0].y == food.fdxy.y)
{
snake.lenth++;
food.flag = 0;//这里看下main函数
food.grade += 10;
}
}
//键盘操作
void keydown()
{
char dqown = _getch();
switch (dqown)
{
case 'W':
case 'w':
if (snake.position != down)
snake.position = up;
break;
case 'A':
case 'a':
if (snake.position != right)
snake.position = left;
break;
case 'S':
case 's':
if (snake.position != up)
snake.position = down;
break;
case 'D':
case 'd':
if (snake.position != left)
snake.position = right;
break;
case '9':Sleep(5000);//按下 9 暂停 5 秒 (可自行更改)
}
}
void showgrade()
{
char Grade[20] = "";
sprintf_s(Grade, "grade:%d", food.grade);
outtextxy(250, 20, Grade);
}
//撞墙则死,碰自己不死
void dead()
{
if (snake.xy[0].x == M || snake.xy[0].x < 0 || snake.xy[0].y < 0 || snake.xy[0].y == N)
{
char over[20] = "Game Over!";
outtextxy(250, 50, over);
system("pause");
exit(0);
}
}
int main()
{
srand((unsigned int)time(NULL));//播种
initgraph(M, N); //画面大小,可自行随意更改
setbkcolor(RGB(110, 120, 119));//背景颜色
//初始化蛇
startsnake();
drawsnake();
while (1)
{
cleardevice();//清屏
movesnake();
drawsnake();
if (food.flag == 0)//判断是否生成食物
{
showfood();
food.flag = 1;
}
drawfood();
if (_kbhit())//判断是否键盘操作
{
keydown();
}
eatfood();
showgrade();
dead();
Sleep(100);//这个可以看作蛇的移动速度
}
return 0;
}
边栏推荐
- Navicat Premium 连接Oracle 数据库(图文教程)
- Swift -- save print log to sandbox
- Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization
- 实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)
- Cross border integration, creativity and innovation to help improve the influence of cultural tourism night tour
- 什么是 SAP ABAP? 类型、ABAP 完整形式和含义
- [Shanda conference] private chat channel webrtc tools
- Solve the problem of MySQL remote login permission
- ORB_VI思想框架
- Quick sort_ sort
猜你喜欢

【一起上水硕系列】Day Three - video

SAP ABAP 中的 Smart Forms-014

vector的模拟实现

数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖

谷歌浏览器的小细节

Focus on creating a net red product. The xinjietu x70s is newly launched, starting from 87900

SAP ABAP BAPI-016

Process address space

Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express

SAP ABAP 对话框编程教程:中的模块池-09
随机推荐
Make the text template in pycharm project support jinjia2 syntax
数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
webDriver以及Selenium使用总结
SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
ArcGIS JS之 4.23之IIS本地部署与问题解决
[leetcode] 9. Palindromes
School enterprise alliance is on the way! Huawei cloud gaussdb has come to universities again
Swift -- save print log to sandbox
A simple understanding of hill ordering
阿里云中间件的开源往事
Jenkins automatically triggers compilation by checking code submissions
Quickly play ci/cd graphical choreography
nvarchar和varchar的区别
Odoo local document function development record
SAP ABAP 数据字典教程 SE11:表、锁定对象、视图和结构 -03
SAP价值流程&帮助请求流程-011
【一起上水硕系列】Day Three - video
[Shanda conference] project initialization
校企联合在路上!华为云GaussDB又来高校啦
转:杰克·韦尔奇:战略就是要少点沉思,敏于行动