当前位置:网站首页>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;
}
边栏推荐
- 期货怎么开户?网上期货开户安全吗?
- Quickly play ci/cd graphical choreography
- A simple understanding of hill ordering
- Research on ICT: domestic databases focus on the ICT market, and Huawei Gauss is expected to become the strongest
- Jenkins automatically triggers compilation by checking code submissions
- 转:杰克·韦尔奇:战略就是要少点沉思,敏于行动
- Mysql触发器
- SAP ABAP 表控制与示例-07
- 预约打新债到底安不安全呀?是不是靠谱的?
- Binary search (integer binary)
猜你喜欢

ArcGIS JS之 4.23之IIS本地部署与问题解决

The odoo system sets priorities for the views independently developed by the original model

推進兼容適配,使能協同發展 GBase 5月適配速遞

#进程地址空间

Odoo local document function development record

Jenkins automatically triggers compilation by checking code submissions

SAP ABAP 对话框编程教程:中的模块池-09

Bridging the gap between open source databases and database services

odoo系统对原有模型单独开发的视图设置优先级

pymssql模块使用指南
随机推荐
B树和B+树
数睿数据受邀参与南通企业数字化转型研讨会
【山大会议】WebRTC基础之对等体连接
String的模拟实现
Odoo local document function development record
SAP ABAP 中的用户出口和客户出口-015
ironSource Luna 推出苹果搜索广告限时优惠,注册即享3个月免费服务
数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右
vector的模拟实现
Scala language learning-04-function passed in as parameter function as return value
84. (cesium chapter) movement of cesium model on terrain
84.(cesium篇)cesium模型在地形上运动
New load balancing webclient CRUD
程序替换函数
【山大会议】私人聊天频道 WebRTC 工具类
【山大会议】软件性能优化及bug修复
mysql - sql执行过程
Pymssql Module User Guide
Wechat applet avatar pendant production
【山大会议】一些基本工具类定义