c语言链表贪吃蛇脚本之家,C++控制台实现贪吃蛇游戏
本文实例为大家分享了C++实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
刚学完了C语言,便尝试的写了贪吃蛇的代码,但是效果不佳,很多的bug,所以,这个学了C++,便重新的写了这个小游戏,用类来封装!
先是头文件:
struct Snake
{
int x, y;
};
class snake
{
public:
snake() //构造函数
{
length = 3;
s[2].x = 10;
s[2].y = 10;
s[1].x = 9;
s[1].y = 10;
s[0].x = 8;
s[0].y = 10;
up = right = left = down = 0;
}
~snake(){}
void display(); //显示蛇身函数
void Rightmove(); //右移函数
void Leftmove(); //左移函数
void Upmove(); //上移函数
void Downmove(); //下移函数
int cheak(); //检查是否撞墙或撞到自身
void creat_food(); //产生食物
int eat_food(); //吃食物
private:
struct Snake s[100]; //先定义蛇身最长100
int length; //当前蛇长度
int x3, y3; //食物坐标
int up, down, right, left; //蛇的状态,是上移还是下移或...
};
void make_frame(); //打印框架的函数
void show(); //游戏开始倒计时函数
void gameover(); //游戏结束函数
下面是各个函数的实现的cpp文件:
# include
# include
# include
# include "snake.h"
# define MaxLen 20
# define MaxWen 30
using namespace std;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄
void gotoxy(HANDLE hOut, int x, int y) //输出位置的函数
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
}
void snake::display() //打印蛇身
{
for (int i = length - 1; i >= 0; i--)
{
if (i == length - 1) //打印蛇头
{
gotoxy(hOut, s[i].x, s[i].y);
cout << char(15);
}
else //打印蛇身
{
gotoxy(hOut, s[i].x, s[i].y);
cout << '*';
}
}
gotoxy(hOut, 0, 22);
}
void snake::Rightmove() //右移
{
right = 1; up = down = left = 0;
int x1, x2, y1, y2;
x1 = x2 = s[length - 1].x;
y1 = y2 = s[length - 1].y;
s[length - 1].x++; //蛇头x坐标自增
for (int i = length - 2; i >= 0; i--) //除了蛇头,其他的结点都等于它的上一个结点的坐标
{
x2 = s[i].x; y2 = s[i].y;
s[i].x = x1; s[i].y = y1;
x1 = x2; y1 = y2;
}
gotoxy(hOut, x2, y2); //消除蛇移动遗留的 ‘*'
cout << ' ';
}
void snake::Leftmove() //左移
{
left = 1; right = up = down = 0;
int x1, x2, y1, y2;
x1 = x2 = s[length - 1].x;
y1 = y2 = s[length - 1].y;
s[length - 1].x--; //同上
for (int i = length - 2; i >= 0; i--)
{
x2 = s[i].x; y2 = s[i].y;
s[i].x = x1; s[i].y = y1;
x1 = x2; y1 = y2;
}
gotoxy(hOut, x2, y2); //同上
cout << ' ';
}
void snake::Downmove() //下移
{
down = 1; right = up = left = 0;
int x1, x2, y1, y2;
x1 = x2 = s[length - 1].x;
y1 = y2 = s[length - 1].y;
s[length - 1].y++; //同上
for (int i = length - 2; i >= 0; i--)
{
x2 = s[i].x; y2 = s[i].y;
s[i].x = x1; s[i].y = y1;
x1 = x2; y1 = y2;
}
gotoxy(hOut, x2, y2); //同上
cout << ' ';
}
void snake::Upmove() //上移
{
up = 1; down = right = left = 0;
int x1, x2, y1, y2;
x1 = x2 = s[length - 1].x;
y1 = y2 = s[length - 1].y;
s[length - 1].y--; //同上
for (int i = length - 2; i >= 0; i--)
{
x2 = s[i].x; y2 = s[i].y;
s[i].x = x1; s[i].y = y1;
x1 = x2; y1 = y2;
}
gotoxy(hOut, x2, y2); //同上
cout << ' ';
}
int snake::cheak()
{
int flag = 0;
for (int i = length - 2; i >= 0; i--) //是否撞到自身
{
if (s[i].x == s[length - 1].x && s[i].y == s[length - 1].y)
{
flag = 1; //是,标识符为1
break;
}
}
if (flag == 1 || (s[length - 1].x >= 30 + 1 || s[length - 1].x < 4) || (s[length - 1].y <= 1 || s[length - 1].y >= 20))
{
return 0; //检测是否撞自身,或者撞墙
}
else
{
return 1;
}
}
void snake::creat_food() //产生食物坐标
{
xy: x3 = (rand() % (25)) + 3;
y3 = (rand() % (17)) + 2;
for (int i = length - 1; i >= 0; i--) //检查食物是否在蛇身上
{
if (s[i].x == x3 && s[i].y == y3) //是就重新产生食物坐标
goto xy;
}
gotoxy(hOut, x3, y3); //显示食物
cout << '*';
}
int snake::eat_food()
{
if (s[length - 1].x == x3 && s[length - 1].y == y3) //蛇头碰到食物
{
if (up == 1) //如果蛇是在上移,增加一个结点,为蛇头的上一个结点
{
s[length].x = x3;
s[length].y = y3 - 1;
}
else if (down == 1) //同上
{
s[length].x = x3;
s[length].y = y3 + 1;
}
else if (right == 1) //同上
{
s[length].x = x3 + 1;
s[length].y = y3;
}
else if (left == 1) //同上
{
s[length].x = x3 - 1;
s[length].y = y3;
}
length++; //蛇长加1
return 1;
}
else
return 0;
}
void make_frame() //打印框架函数
{
cout << " 贪吃蛇游戏" << endl;
gotoxy(hOut, 2, 1);
cout << "╔";
for (int i = 4; i < 2 + MaxWen; i++)
{
gotoxy(hOut, i, 1);
printf("=");
}
for (int i = 2; i < MaxLen; i++)
{
gotoxy(hOut, 2, i);
printf("║");
}
gotoxy(hOut, 2 + MaxWen, 1);
printf("╗");
for (int i = 2; i < MaxLen; i++)
{
gotoxy(hOut, 2 + MaxWen, i);
printf("║");
}
gotoxy(hOut, 2, MaxLen);
printf("╚");
gotoxy(hOut, 2 + MaxWen, MaxLen);
printf("╝");
for (int i = 4; i < 2 + MaxWen; i++)
{
gotoxy(hOut, i, MaxLen);
printf("=");
}
}
void show() //显示操作方法和游戏开始倒计时
{
gotoxy(hOut, 35, 5);
cout << "↑:" << 'w';
gotoxy(hOut, 35, 6);
cout << "←:" << 'a';
gotoxy(hOut, 35, 7);
cout << "↓:" << 's';
gotoxy(hOut, 35, 8);
cout << "→:" << 'd';
gotoxy(hOut, 16, 5);
cout << '3';
Sleep(1000);
gotoxy(hOut, 16, 5);
cout << '2';
Sleep(1000);
gotoxy(hOut, 16, 5);
cout << '1';
Sleep(1000);
gotoxy(hOut, 16, 5);
cout << ' ';
}
void gameover() //游戏结束函数
{
system("cls");
system("color 3B");
gotoxy(hOut, 14, 5);
cout << " GAME OVER!";
gotoxy(hOut, 14, 6);
cout << "PLAY AGAIN ? Y(yes) \ N(no)";
}
主函数的cpp文件:
# include
# include
# include
# include "snake.h"
using namespace std;
char ch;
int main()
{
while (1)
{
snake sn; //声明对象
system("cls"); //清屏
system("color 3B"); //背景和字体颜色调整
make_frame(); //打印框架
sn.display(); //显示蛇
show(); //游戏开始
sn.creat_food(); //产生食物
while (sn.cheak()) //检查是否死亡
{
sn.Rightmove(); //右移
sn.display(); //显示蛇身
if (sn.eat_food()) //检查是否吃到食物
{
sn.creat_food(); //重新产生食物
sn.display();
}
Sleep(500); //等待500Ms
p: if (_kbhit()) //是否有按键
{
ch = _getch();
if (ch == 97 || ch == 100)
goto p;
if (ch == 115 || ch == 119)
break;
}
}
pp: switch (ch) //有按键
{
case 119: //上移的情况
{
while (sn.cheak())
{
sn.Upmove();
sn.display();
if (sn.eat_food())
{
sn.creat_food();
sn.display();
Sleep(300);
}
Sleep(500);
pw: if (_kbhit())
{
ch = _getch();
if (ch == 119 || ch == 115)
goto pw;
if (ch == 97 || ch == 100)
goto pp;
}
}
}break;
case 97: //左移的情况
{
while (sn.cheak())
{
sn.Leftmove();
sn.display();
if (sn.eat_food())
{
sn.creat_food();
sn.display();
}
Sleep(500);
pa: if (_kbhit())
{
ch = _getch();
if (ch == 97 || ch == 100)
goto pa;
if (ch == 115 || ch == 119)
goto pp;
}
}
}break;
case 115: //下移的情况
{
while (sn.cheak())
{
sn.Downmove();
sn.display();
if (sn.eat_food())
{
sn.creat_food();
sn.display();
Sleep(300);
}
Sleep(500);
ps: if (_kbhit())
{
ch = _getch();
if (ch == 115 || ch == 119)
goto ps;
if (ch == 97 || ch == 100)
goto pp;
}
}
}break;
case 100: //右移的情况
{
while (sn.cheak())
{
sn.Rightmove();
sn.display();
if (sn.eat_food())
{
sn.creat_food();
sn.display();
}
Sleep(500);
pd: if (_kbhit())
{
ch = _getch();
if (ch == 100 || ch == 97)
goto pd;
if (ch == 119 || ch == 115)
goto pp;
}
}
}break;
default:
break;
}
gameover(); //显示游戏结束,是否重玩
py: ch = _getch();
if (ch == 110) //否
{
system("cls");
break;
}
else if (ch == 121) //是
continue;
else
goto py;
}
return 0;
}
下面是游戏的截图:
控制台的实现,不是很美观,主要是由于上下和左右的间隙不一样大,所以看起来不是很好看,但总体还是实现了贪吃蛇!
关于C++小游戏的更多精彩内容请点击专题: 《C++经典小游戏》 学习了解
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
c语言链表贪吃蛇脚本之家,C++控制台实现贪吃蛇游戏相关推荐
- 用c语言单链表编写贪吃蛇程序6,C语言链表实现贪吃蛇游戏
阅读学习了源代码,并做了简单的注释和修改,里面只用了链表数据结构,非常适合C语言入门者学习阅读. 程序可在VS2013下编译运行. #include #include #include #includ ...
- c语言链表写贪吃蛇思路,C语言构建的链表贪吃蛇
用C语言链表写的贪吃蛇(程序设计时做的,做的不好大佬勿喷) 借助游戏内容分析贪吃蛇所需的功能主要包括这几块: 移动光标模块 打印地图模块和基本规则信息 读取最高分文件 打印初始蛇模块 打印时给予蛇的初 ...
- c语言-链表-贪吃蛇
1. c语言-链表-贪吃蛇 坐标的结构体-COORD COORD c;c.X = x;c.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_ ...
- c语言制作贪吃小白入门,小白入门——easyx界面版“贪吃蛇”的C语言实现(详细)...
小白入门--easyx界面版"贪吃蛇"的C语言实现(详细) 作者:顽石 前言:C语言初学者都是在控制台上开发一些小游戏什么,由于没有界面(比如图片.音乐,不能做出拥有自己风格的程序 ...
- C语言贪吃蛇详解4,c语言贪吃蛇详解4.食物的投放与蛇的变长
c语言贪吃蛇详解4.食物的投放与蛇的变长 前几天的实验室培训课后作业我布置了贪吃蛇,今天有时间就来写一下题解.我将分几步来教大家写一个贪吃蛇小游戏.由于大家c语言未学完,这个教程只涉及数组和函数等知识 ...
- C语言处理贪吃蛇游戏蛇的长度,c语言贪吃蛇详解4.食物的投放与蛇的变长
c语言贪吃蛇详解4.食物的投放与蛇的变长 前几天的实验室培训课后作业我布置了贪吃蛇,今天有时间就来写一下题解.我将分几步来教大家写一个贪吃蛇小游戏.由于大家c语言未学完,这个教程只涉及数组和函数等知识 ...
- C语言贪吃蛇如何让蛇一直前进,c++贪吃蛇代码中,哪条代码是让蛇知道前进的
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include using namespace std; // 刷新当前屏幕 inline voi ...
- C语言链表的来源分析
C语言中的链表是重点,也是难点,而且意义非凡.对链表的的抽象和恐惧是源于对它的来龙去脉的不明白.所以很有必要对它的发展渊源做透彻分析. 链表的单位是节点,而节点源于复合数据类型:结构体: 节点和结构体 ...
- java用链表做学生系统_C语言链表实现学生管理系统
本文实例为大家分享了C语言链表实现学生管理系统的具体代码,供大家参考,具体内容如下 #include #include #include #include #include #include usin ...
最新文章
- Select函数实现原理分析
- vue.js反编译_基于electron-vue开发的微信小程序反编译客户端
- ArrayList的泛型可以不写吗
- 利用Excel VBA批量计算气象数据多个台站多年来春季和冬季降水量和平均气温
- .Net性能调优-垃圾回收!!!最全垃圾回收来了
- [转帖]关于win7共享的问题和解答
- mysql mgr CONSuL_Mysql MGR + Consul + Consul-template + Haproxy 搭建mysql 高可用集群 (三)...
- linux bash技巧_Bash提示技巧和窍门
- C#匿名对象在其它方法体内怎么取到相应的值(不想建立对应的类并转化的情况下)?...
- 台达DVP50MC11T与威纶触摸屏ModbusTCP通信
- 【JQuery】两种失焦事件的使用
- 在centos7.7安装搜狗输入法踩坑日记
- 【微信小程序】全局变量的定义与使用
- SpringEL 表达式语言(Spring Expression Language)
- pyecharts将html转换图片,如何把pyecharts的炫酷延续到PPT里?
- 如何用C语言汉字编码输出汉字,【C语言学习】C语言汉字编码。。。C语言中汉字的输入...
- SharedPreferences使用及原理
- HTML实例1 _网页文章
- 二重调度(一):什么是二重调度?
- 视频教程-数字成像系统-其他
热门文章
- 五位专家跟你讲讲为啥Python更适合做AI/机器学习
- AliOS Things 基于组件化思想的多bin特性
- 2021银行共探转型新动能:大行酝酿质变 小行跨越数字鸿沟
- 云安全的新战场上,要靠什么来抵御威胁
- 阿里云数字政府市场份额第一,同比增速102.57%
- linux系统无法识别固态硬盘_linux查看硬盘是不是ssd
- 如何选择python书籍_关于 Python 的经典入门书籍有哪些?
- 小程序 获取腾讯地图计算两经纬度的实际距离(可批量)_多地打卡
- 工作流实战_05_flowable 流程定义的挂起与激活
- 工作流实战_04_flowable 流程的模板的图片和xml显示