c/c++ 贪吃蛇控制台版
贪吃蛇控制台版(操作系统win7 64位;编译环境gcc, vs2017通过,其它环境未测试 不保证一定通过)
运行效果:
- #include <iomanip>
- #include <windows.h>
- #include <conio.h>
- using namespace std;
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//全局句柄
- class snake;
- void Init_Console();//控制台初始化
- void gotoPos(SHORT x = , SHORT y = );//光标坐标控制
- void Init_Map();//画边框
- void KeyConctrl(snake &,int);//键盘控制函数
- void is_gameover();//结束提示
- void Prompt_info(int, int);//提示信息
- void dis_date(int, int, double, int);//得分信息
- bool gameOver = false;
- bool withdraw = false;
- class snake
- {
- private:
- bool flash;
- int speed, length, print_bit, clear_bit;
- COORD in_Pos, bit_Pos;
- COORD _Pos[];
- enum direction{go_stop = , go_left, go_right, go_up, go_down}dir;
- public:
- snake(SHORT x = , SHORT y = )
- {
- clear_bit = print_bit = length = speed = ;
- in_Pos.X = x; in_Pos.Y = y; bit_Pos.X = ; bit_Pos.Y = ;
- flash = true ;
- dir = go_stop;
- _Pos[].X = in_Pos.X;
- _Pos[].Y = in_Pos.Y;
- }
- ~snake(){}
- void up_speed()
- {
- if(this->speed < )(this->speed)++;
- }
- double get_speed()
- {
- return this->speed;
- }
- int get_length()
- {
- return this->length;
- }
- //
- void dir_control(char _dir)
- {
- switch(_dir)
- {
- case 's': this->dir = go_stop;
- break;
- case 'l': if(this->dir != go_right)this->dir = go_left;
- break;
- case 'r': if(this->dir != go_left)this->dir = go_right;
- break;
- case 'u': if(this->dir != go_down)this->dir = go_up;
- break;
- case 'd': if(this->dir != go_up)this->dir = go_down;
- break;
- }
- }
- //光标位置
- void setPos(COORD pos)
- {
- SetConsoleCursorPosition(hOut, pos);
- }
- //碰撞检测
- void check_bit()
- {
- if(in_Pos.X == bit_Pos.X && in_Pos.Y == bit_Pos.Y)
- {
- length++;
- if((length % == ))
- {
- if(this->speed < )this->speed++;
- }
- if(length == )
- {
- cout << "游戏通关!" ;
- }
- do
- {
- srand(time(NULL));
- bit_Pos.X = + (rand()%)*;
- bit_Pos.Y = + rand()%;
- }while(check_snk(bit_Pos));
- }
- else
- {
- cle();
- clear_bit++;
- }
- }
- //撞蛇 撞边检测
- bool check_snk(COORD snk_Pos)
- {
- //边界检测
- if(snk_Pos.Y <= || (snk_Pos.Y >= ) || (snk_Pos.X <= ) || snk_Pos.X >= )
- {
- return true;
- }
- for(int i = clear_bit; i <= print_bit; i++)
- {
- if(_Pos[i].X == snk_Pos.X && _Pos[i].Y == snk_Pos.Y) return true;
- }
- return false;
- }
- //显示
- void dis()
- {
- if(!flash)
- {
- setPos(bit_Pos);
- SetConsoleTextAttribute(hOut, 0x0e);
- cout << " ";
- flash = true;
- }
- else
- {
- setPos(bit_Pos);
- SetConsoleTextAttribute(hOut, 0x0e);
- cout << "■";
- flash = false;
- }
- setPos(in_Pos);
- SetConsoleTextAttribute(hOut, 0x09);
- cout << "■";
- }
- void cle()
- {
- setPos(_Pos[clear_bit]);
- SetConsoleTextAttribute(hOut, 0x05);
- cout << " ";
- }
- void save_date()
- {
- if(print_bit == )
- {
- for(int i = ; i <= length; i++)
- {
- _Pos[i].X = _Pos[clear_bit + i].X;
- _Pos[i].Y = _Pos[clear_bit + i].Y;
- }
- clear_bit = ;
- print_bit = length;
- }
- print_bit++;
- _Pos[print_bit].X = in_Pos.X;
- _Pos[print_bit].Y = in_Pos.Y;
- }
- // //移动
- bool move()
- {
- switch(this->dir)
- {
- case go_stop:
- break;
- case go_left:
- in_Pos.X -= ;
- if(check_snk(in_Pos))
- {
- return true;
- }
- dis();
- save_date();
- check_bit();
- break;
- case go_right:
- in_Pos.X += ;
- if(check_snk(in_Pos))
- {
- return true;
- }
- dis();
- save_date();
- check_bit();
- break;
- case go_up:
- in_Pos.Y--;
- if(check_snk(in_Pos))
- {
- return true;
- }
- dis();
- save_date();
- check_bit();
- break;
- case go_down:
- in_Pos.Y++;
- if(check_snk(in_Pos))
- {
- return true;
- }
- dis();
- save_date();
- check_bit();
- break;
- }
- return false;
- }
- };
- int main()
- {
- do
- {
- Init_Console();
- Init_Map();
- Prompt_info(, );
- snake s(, );
- clock_t t_in, t_out;
- t_in = clock();
- s.dis();
- //
- while (!gameOver)
- {
- if (_kbhit())
- {
- KeyConctrl(s, _getch());
- }
- t_out = clock();
- dis_date(, , s.get_speed(), s.get_length());
- if (t_out - t_in > ((0.25 - (s.get_speed() / )) * CLOCKS_PER_SEC))
- {
- t_in = t_out;
- gameOver = s.move();
- if (gameOver)
- {
- is_gameover();
- }
- }
- Sleep();
- }
- //
- while (gameOver)
- {
- if (_kbhit())
- {
- switch (_getch())
- {
- case 'y':
- case 'Y':
- gameOver = false;
- system("cls");
- break;
- case 'n':
- case 'N':
- gameOver = false;
- withdraw = true;
- break;
- }
- }
- Sleep();
- }
- }while (!withdraw);
- gotoPos(, );
- return ;
- }
- //控制台初始化
- void Init_Console()
- {
- SetConsoleTitleA("Console_贪吃蛇");
- COORD dSiz = {, };
- SetConsoleScreenBufferSize(hOut, dSiz);//设置窗口缓冲区大小
- CONSOLE_CURSOR_INFO _guan_biao = {, FALSE};//设置光标大小,隐藏光标
- SetConsoleCursorInfo(hOut, &_guan_biao);
- system("color 0f");//设置画布颜色
- }
- //光标位置
- void gotoPos(SHORT x, SHORT y)
- {
- COORD pos = {x, y};
- SetConsoleCursorPosition(hOut, pos);
- }
- //画边框
- void Init_Map()
- {
- //SetConsoleTextAttribute(hOut, 0xF0); //设置前景色,背景色
- system("cls");
- //左边框
- for(int i = ; i < ; i++)
- {
- cout << endl << " ●";
- }
- //上边框
- gotoPos(, );
- for(int i = ; i < ; i++)
- {
- cout << "●";
- }
- //下边框
- gotoPos(, );
- for(int i = ; i < ; i++)
- {
- cout << "●";
- }
- //右边框
- for(SHORT i = ; i <= ; i++)
- {
- gotoPos(, i);
- cout << "●";
- }
- }
- //键盘控制函数
- void KeyConctrl(snake &_snk, int _key)
- {
- switch(_key)
- {
- case ' ':
- _snk.dir_control('s');
- break;
- case 'w':
- case 'W':
- case : _snk.dir_control('u');
- break;
- case 'a':
- case 'A':
- case : _snk.dir_control('l');
- break;
- case 'd':
- case 'D':
- case : _snk.dir_control('r');
- break;
- case 's':
- case 'S':
- case : _snk.dir_control('d');
- break;
- case '+': _snk.up_speed();
- break;
- default: break;
- }
- }
- //结束提示
- void is_gameover()
- {
- gotoPos(, );
- SetConsoleTextAttribute(hOut, 0xec);
- cout << "game over!";
- gotoPos(, );
- cout << "Y重新开始/N退出";
- SetConsoleTextAttribute(hOut, 0x0f);
- }
- //提示信息
- void Prompt_info(int _x, int _y)
- {
- SetConsoleTextAttribute(hOut, 0xB);
- gotoPos(_x+, _y+);
- cout << "■游戏说明:";
- gotoPos(_x+, _y+);
- cout << "A.每得500分移速自动加1";
- gotoPos(_x+, _y+);
- cout << "B.可手动加速,最高移速5";
- gotoPos(_x+, _y+);
- cout << "■操作说明:";
- gotoPos(_x+, _y+);
- cout << "□向左移动:← A";
- gotoPos(_x+, _y+);
- cout << "□向右移动:→ D";
- gotoPos(_x+, _y+);
- cout << "□向下移动:↓ S";
- gotoPos(_x+, _y+);
- cout << "□向上移动:↑ W";
- gotoPos(_x+, _y+);
- cout << "□控制加速:+";
- gotoPos(_x+, _y+);
- cout << "□暂停游戏:空格";
- gotoPos(_x+, _y+);
- cout << "□开始游戏:任意方向键";
- gotoPos(_x+, );
- cout <<"■By: Flowingwind 18.01.11";
- }
- //速度积分显示
- void dis_date(int _x, int _y, double Sped, int Score)
- {
- SetConsoleTextAttribute(hOut, 0xB);
- gotoPos(_x+, _y+);
- cout << setw() << "◆ 移动速度: ";
- SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
- cout << Sped;
- gotoPos(_x+, _y+);
- SetConsoleTextAttribute(hOut, 0xB);
- cout << "◆ 当前积分: ";
- SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
- cout << setw() << Score * ;
- }
代码应该还可以 做一些精简化处理!!!暂时先就这样了;
save_date()保存数据函数对内存做了一定优化处理,以达到使用较小内存可永久运行程序。
(虽然现在的电脑内存根本不用考虑这个,但想起我第一次玩贪吃蛇,还是在一款 掌机上玩的,有机会能拷贝上去也不错)
c/c++ 贪吃蛇控制台版的更多相关文章
- 原生js写的贪吃蛇网页版游戏特效
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <bo ...
- js贪吃蛇-简单版
分享个用原生js写的贪吃蛇,最近在学java,按照当年写的 js的思路,转换成java,换汤不换药 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- 贪吃蛇java版
主要的蛇的类 import java.awt.Color; import java.awt.Graphics; import java.awt.HeadlessException; import ja ...
- 如何用python制作贪吃蛇以及AI版贪吃蛇
用python制作普通贪吃蛇 哈喽,大家不知道是上午好还是中午好还是下午好还是晚上好! 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很 ...
- C语言 小游戏之贪吃蛇
还记得非常久曾经听群里人说做贪吃蛇什么的,那时候大一刚学了C语言,认为非常难,根本没什么思路. 前不久群里有些人又在谈论C语言贪吃蛇的事了,看着他们在做,我也打算做一个出来. 如今大三,经过了这一年半 ...
- 原生JavaScript贪吃蛇
在实例开发过程中还是能认识到很多不足的,并且加强了一些基础. 简单写一下制作过程: 1.创建画布 2.创建蛇和老鼠 坐标不能重叠 3.让蛇移动起来 4.添加死亡方法 5.添加转点坐标和方向 6.添加吃 ...
- C语言 贪吃蛇
贪吃蛇(单人版): 本人先来介绍一个函数 -- bioskey函数: int bioskey (int cmd) 参数 (cmd) 基本功能 0 返回下一个从键盘键入的值(若不键入任何值,则将等下一个 ...
- 原生Js贪吃蛇游戏实战开发笔记
前言 本课程是通过JavaScript结合WebAPI DOM实现的一版网页游戏---贪吃蛇的开发全过程,采用面向以象的思想设计开发.通过这个小游戏的开发, 不仅可以掌握JS的语法的应用,还可以学会D ...
- 贪吃蛇(C语言版)链表实现
贪吃蛇 gitee:贪吃蛇C语言版: Snake 蛇的结构 typedef struct Snake { int x; int y; struct Snake *next; }; 游戏开始欢迎界面 / ...
随机推荐
- java傻瓜简单100%一定看的懂新手安装教程
1.java官网 最新的不是很稳定 http://www.oracle.com/technetwork/java/javase/downloads/index.html 一直点下一步就可以,但别忘 ...
- vue项目的骨架及常用组件介绍
vue项目基础结构 一个vue的项目,我觉得最小的子集其实就是{vue,vue-router,component},vue作为基础库,为我们提供双向绑定等功能.vue-router连接不同的" ...
- C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))
C. Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k diffe ...
- [Phonegap+Sencha Touch] 移动开发26 Android下的sencha touch程序,转屏时,Ext.Viewport不能触发orientationchange事件的解决的方法
Sencha touch 2.4.2 已经解决问题了. 比方你为Ext.Viewport的orientationchange事件加入了一个监听方法: Ext.Viewport.on('orientat ...
- 查询linux版本命令
[环境] Ubuntu [本文命令记录] uname -a lsb_release cat /etc/issue cat /proc/version [截图效果] (1)uname -a (2)lsb ...
- 【转载】Java系列笔记(3) - Java 内存区域和GC机制
Java系列笔记(3) - Java 内存区域和GC机制 转载:原文地址http://www.cnblogs.com/zhguang/p/3257367.html 目录 Java垃圾回收概况 Java ...
- java 集合学习笔记
1.Collection(单列结合) List(有序,数据可重复) ArrayList:底层数据结构是数组,查询快,增删慢,线程不安全,效率高. Vector:底层数据结构是数组,查询快,增删慢,线程 ...
- Redis 订阅发布 - Jedis实现
Redis 订阅发布 - Jedis实现 我想到使用Redis的订阅发布模式是用来解决推送问题的-. 对于概念性的叙述,多多少少还是要提一下的: 什么是Redis发布订阅?Redis发布订阅是一种 ...
- ListView用法总结C#
ListView是个较为复杂的控件 网上教程写的很乱,C#中文资料太匮乏了,小白叔叔觉得有必要自己出一份了. http://blog.sina.com.cn/s/blog_43eb83b901 ...
- Intellij IDEA 像eclipse那样给maven添加依赖
打开pom.xml,在它里面使用快捷键:ALT+Insert ---->点击dependency 再输入想要添加的依赖关键字,比如:输个spring 出现下图: 根据需求选择版本,完成以后 ...