C++的游戏--贪吃蛇
代码如下(作者制作不易,还望关注一下):
#include<cstdio>
#include<windows.h>
#define UP 1
#define LEFT 2
#define DOWN 3
#define RIGHT 4
#define HEAD 5
#define WALL 22
struct botton{
int x;
int y;
int length;
};
struct Snake{
Snake(int a,bool b,int c);
bool life;
int Flag ;
int Length ;
int Sum ;
int color;
int speed;
POINT Head ;
POINT End ;
void Smarter() ;
void Move() ;
void Go(bool iiSmart,bool iiSmarter,bool iiMove,int up,int left,int down,int right) ;
};
void GradeIn() ;
void LevelChoose() ;
void MenuPrint() ;
void Menu() ;
void GradePrint() ;
void Setting() ;
void Prepare() ;
void Walk(int ,long&,long&) ;
void Dead() ;
void Foods() ;
void GradeOut(int) ;
void Check() ;
void Pedge(int,int,int,bool) ;
int Pposition(int,int) ;
int Pcolor(int,int) ;
char *Psame(int,char);
char *Pempty(int) ;
POINT Food ;
Snake Person(10,true,1) ;
Snake Computer(1,false,1) ;
Snake Another(14,false,1) ;
int Map[WALL][WALL] ={} ;
int Level = 0 ;
int Score[4] ={} ;
bool iBegin = true ;
bool iMenu = false ;
bool iDead = true ;
bool iFor = false ;
bool iSmart = false ;
bool iCheck = false ;
char wLevel[4][5] ={"简单","普通","困难","炼狱"} ;
Snake::Snake(int a,bool b,int c)
{
color=a;
life=b;
speed=c;
}
/* 数据导入 */
void GradeIn()
{
if ( fopen("score.txt","rb")){
fscanf(fopen("score.txt","rb"),"%d%d%d%d",&Score[0],&Score[1],&Score[2]),&Score[3] ;
}
}
/* 选择菜单 */
void Menu()
{
int one=0 ;
POINT edge[5]={{16,7},{13,10},{13,13},{23,13},{16,16}} ;
int length[5]={12,18,10,8,12} ;
Print:
system("cls") ;
printf("\n\n\n%s贪%s吃%s蛇\n\n\n\n",Pempty(17),Pempty(2),Pempty(2),Pcolor( 15, 3)) ;
printf("\n%s开始游戏\n\n",Pempty(18)) ;
printf("\n%s游戏难度 :%s\n\n",Pempty(15),wLevel[Level]) ;
printf("\n%s排行榜%s设置\n\n",Pempty(15),Pempty(4)) ;
printf("\n%s退出游戏",Pempty(18)) ;
Pedge(edge[one].x,edge[one].y,length[one],true);
Scan:
Sleep(125) ;
if ( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
void (*name[])()={Prepare,LevelChoose,GradePrint,Setting} ;
if(one<4){
name[one]() ;
}
if(one==0||one==4){
iBegin=(one==4)?false:true ;
return ;
}
one=0 ;
}
else if ((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& one!=4){
one+=(one==2)?2:1 ;
}
else if ((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& one!=0){
one-=(one==3)?2:1 ;
}
else if ((GetAsyncKeyState('D')||GetAsyncKeyState(VK_RIGHT))&& one==2){
one++ ;
}
else if ((GetAsyncKeyState('A')||GetAsyncKeyState(VK_LEFT))&& one==3){
one-- ;
}
else{
goto Scan ;
}
for(int i=0;i<5;i++){
Pedge(edge[i].x,edge[i].y,length[i],false);
}
goto Print ;
}
/* 难度选择 */
void LevelChoose()
{
Print :
system("cls") ;
printf("\n\n\n%s困 难 度 选 择\n\n\n\n",Pempty(20)) ;
for(int i=0 ;i<4 ;i++){
printf("%s%s\n\n\n",Pempty(27),wLevel[i]) ;
}
for(int i=0 ;i<2 ;i++){
printf("--------",Pposition(25,(Level+2)*3+i*2) ) ;
printf("#",Pposition(25+i*7,(Level+2)*3+1)) ;
}
Scan:
Sleep(125) ;
if ((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& Level != 3){
Level++ ;
}
else if ((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& Level != 0){
Level-- ;
}
else if ( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
return ;
}
else{
goto Scan ;
}
goto Print ;
}
/* 排行数据 */
void GradePrint()
{
system("cls") ;
printf("\n\n\n%s排 行 榜\n\n",Pempty(18)) ;
for(int i=0 ;i<3 ;i++){
printf("%s第%d名: %d\n\n",Pempty(17) ,i+1,Score[i]) ;
}
printf("\n\n\n%s知道了\n\n",Pempty(19)) ;
Pedge(17,13,10,true);
for(Sleep(125) ; (GetAsyncKeyState(' ') ||GetAsyncKeyState(VK_RETURN))== false ;Sleep(125)) ;
}
/* 系统设置 */
void Setting()
{
bool iScore=false ;
bool iOut=false ;
bool *str[8]={&iDead,&iFor,&iSmart,&Computer.life,&Another.life,&iScore,&iCheck,&iOut} ;
char Name[8][9]={"死亡限制","循环地图","智能辅助","人机模式","双人模式","缓存清理","检查模式","退出设置"} ;
int one=0 ;
Print:
system("cls") ;
printf("\n\n%s设%s置\n\n",Pempty(25),Pempty(4)) ;
for(int i=0 ;i<8 ;i++){
printf("%s%s%s",Pempty(20),Name[i],Pempty(8)) ;
printf("%s\n\n",(*str[i])?" 开":"关",Pcolor((*str[i])?3:15,(*str[i])?15:3)) ;
Pcolor(15,3) ;
}
for(int i=0 ;i<2 ;i++){
printf("--------",Pposition(34,one*2+3+i*2)) ;
printf("#",Pposition(34+i*7,one*2+4) ) ;
}
Scan:
Sleep(125) ;
if((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& one < 7){
one++ ;
}
else if((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& one > 0){
one-- ;
}
else if( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
*str[one]=(*str[one])?false:true ;
}
else{
goto Scan ;
}
if(iScore){;
Score[0] = Score[1] = Score[2] = Score[3]= 0 ;
GradeOut(0);
}
if(iOut){
return ;
}
goto Print ;
}
/* 检查文件 */
void Check()
{
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j++){
fprintf(fopen("check.txt","a"),"%c",Map[i][j]+32) ;
}
fprintf(fopen("check.txt","a"),"\n") ;
}
}
/* 主函数 */
int main()
{
system("color 3f&mode con cols=44 lines=24&title 贪吃蛇") ;
GradeIn() ;
Menu:
Menu() ;
while( iBegin){
if(Another.life==false){
Person.Go(iSmart,false,false,VK_UP,VK_LEFT,VK_DOWN,VK_RIGHT) ;
}
Person.Go(iSmart,false,true,'W','A','S','D') ;
Computer.Go(true,true,true,0,0,0,0) ;
Another.Go(iSmart,false,true,VK_UP,VK_LEFT,VK_DOWN,VK_RIGHT) ;
if ( iDead&&(Map[Person.Head.y][Person.Head.x] != HEAD || ( Computer.life&& Map[Computer.Head.y][Computer.Head.x] != HEAD) || ( Another.life&& Map[Another.Head.y][Another.Head.x] != HEAD))){
Dead() ;
}
if ( iMenu){
goto Menu ;
}
Sleep(( Level < 3) ? ( 150 - ( Level * 50)) : ( rand() % 80)) ;
if(iCheck){
Check() ;
}
}
return 0 ;
}
/* 玩家方向 */
void Snake::Go(bool iiSmart,bool iiSmarter,bool iiMove,int up,int left,int down,int right)
{
if(life){
Flag =((GetAsyncKeyState(left))&& Flag!=RIGHT)?LEFT:Flag ;
Flag = ((GetAsyncKeyState(right))&& Flag!=LEFT)?RIGHT:Flag ;
Flag = ((GetAsyncKeyState(up))&& Flag!=DOWN)?UP:Flag ;
Flag = ((GetAsyncKeyState(down))&& Flag!=UP)?DOWN:Flag ;
iMenu = (GetAsyncKeyState('R'))?true:false ;
if(iiSmarter){
Smarter() ;
}
if(iiSmart){
for(int i=0 ;i<16 ;i++){
if((Flag==LEFT&&Map[Head.y][Head.x-1])||(Flag==DOWN&&Map[Head.y+1][Head.x])||(Flag==RIGHT&&Map[Head.y][Head.x+1])||(Flag==UP&&Map[Head.y-1][Head.x])){
Flag=rand()%4+1 ;
}
}
}
if(iiMove){
Move() ;
}
}
}
/* 智能方向 */
void Snake::Smarter()
{
Map[Food.y][Food.x]=-2 ;
for(int nem=0 ;nem<50&&Map[Head.y-1][Head.x]!=-2&&Map[Head.y+1][Head.x]!=-2&&Map[Head.y][Head.x-1]!=-2&&Map[Head.y][Head.x+1]!=-2 ;nem++){
for(int i=1 ;i<WALL-1 ;i++){
for(int j=1 ;j<WALL-1 ;j++){
if(Map[i][j]==-2){
if(Map[i-1][j]==0){
Map[i-1][j]=-1 ;
}
if(Map[i+1][j]==0){
Map[i+1][j]=-1 ;
}
if(Map[i][j-1]==0){
Map[i][j-1]=-1 ;
}
if(Map[i][j+1]==0){
Map[i][j+1]=-1 ;
}
}
}
}
if(iFor){
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j+=(i==0||i==WALL-1)?1:WALL-1){
if(Map[i][j]==-1){
Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j] = (Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j]==0)?-2:Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j] ;
Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j] = (Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j]==0)?-2:Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j] ;
}
}
}
}
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j++){
Map[i][j]=(Map[i][j]==-1)?-2:Map[i][j] ;
}
}
}
if(Map[Head.y-1][Head.x]==-2){
Flag=UP ;
}
else if(Map[Head.y+1][Head.x]==-2){
Flag=DOWN ;
}
else if(Map[Head.y][Head.x-1]==-2){
Flag=LEFT ;
}
else if(Map[Head.y][Head.x+1]==-2){
Flag=RIGHT ;
}
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j++){
if(Map[i][j]==-2){
Map[i][j]=0 ;
}
}
}
}
/* 定位打印 */
int Pposition ( int x ,int y)
{
COORD Pos ={ x,y } ;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE) ;
SetConsoleCursorPosition(Handle, Pos) ;
CONSOLE_CURSOR_INFO CurSor ;
GetConsoleCursorInfo(Handle,&CurSor) ;
CurSor.bVisible = false ;
SetConsoleCursorInfo(Handle,&CurSor) ;
return 0 ;
}
/* 颜色字体 */
int Pcolor( int a ,int b)
{
HANDLE Handle=GetStdHandle(STD_OUTPUT_HANDLE) ;
SetConsoleTextAttribute(Handle, a + b * 0x10) ;
return 0 ;
}
/* 批量打印 */
char * Psame (int n,char word)
{
char * sentence=new char [n+2] ;
for(int i=0 ;i<n ;i++){
sentence[i]=word ;
}
sentence[n]=0 ;
return sentence ;
}
/* 空格打印 */
char * Pempty(int n)
{
return Psame(n,' ');
}
/* 边框打印 */
void Pedge(int x,int y,int length,bool saw)
{
for(int i=0;i<2;i++){
printf("%s",Psame(length,'-'),Pposition(x,y+i*2),Pcolor(saw?15:3,3));
printf("#",Pposition(x+i*(length-1),y+1));
}
}
/* 运行准备 */
void Prepare()
{
Person.Flag = RIGHT ;
Person.End.x = Person.Head.x = 3 ;
Person.End.y = Person.Head.y = 10 ;
Person.Sum = -3 ;
Person.Length = 0 ;
Computer.Flag = LEFT ;
Computer.End.x = Computer.Head.x = 18 ;
Computer.End.y = Computer.Head.y = 10 ;
Computer.Sum = -3 ;
Computer.Length = 0 ;
Another.Flag =(Computer.life)?DOWN : LEFT ;
Another.End.x = Another.Head.x =(Computer.life)? 11:18 ;
Another.End.y = Another.Head.y =(Computer.life)? 3:10 ;
Another.Sum = -3 ;
Another.Length = 0 ;
iMenu = false ;
system("cls") ;
for( int i = 0 ; i < WALL ; i++){
for( int j = 0 ; j < WALL ; ++j){
if( i == 0 || i == WALL -1 || j == 0 || j == WALL -1){
Map[i][j] = (iFor)?0:WALL ;
Pcolor( 15,3) ;
}
else{
Map[i][j] = 0 ;
Pcolor( 3 , 3) ;
}
printf("■") ;
}
}
Foods() ;
}
/* 蛇皮走位 */
void Walk(int Flag,long&x,long&y)
{
switch(Flag){
case LEFT:{
x-=(iFor&&x==1)?-19:1 ;
}return ;
case RIGHT:{
x+=(iFor&&x==20)?-19:1 ;
}return ;
case UP:{
y-=(iFor&&y==1)?-19:1 ;
}return ;
case DOWN:{
y+=(iFor&&y==20)?-19:1 ;
}return ;
}
}
/* 蛇的移动 */
void Snake::Move()
{
printf("●",Pcolor(color,3) ,Pposition(End.x*2,End.y) ) ;
Map[Head.y][Head.x] = Flag ;
Walk(Flag,Head.x,Head.y) ;
printf("●",Pcolor( color ,3),Pposition( Head.x * 2 ,Head.y)) ;
Map[Head.y][Head.x] += HEAD ;
if( Head.y==Food.y&& Head.x==Food.x){
Length++ ;
Foods() ;
}
if( Sum==Length){
int Temp = Map[End.y][End.x] ;
Map[End.y][End.x] = 0 ;
printf("■",Pcolor( 3 ,3), Pposition( End.x * 2 ,End.y) ) ;
Walk(Temp,End.x,End.y) ;
}
else{
Sum++ ;
}
}
/* 判断死亡 */
void Dead()
{
GradeOut(Person.Length+Another.Length) ;
printf(" Game Over !",Pcolor( 15, 3) ,Pposition( 16 , 9)) ;
Sleep(1500) ;
char wDead[3][9] ={"重新开始","返回菜单","退出游戏"} ;
int one = iBegin = false ;
Print:
system("cls") ;
printf("\n\n\n\n%s游 戏 结 束\n",Pempty(20)) ;
if(Another.life&&Person.Length>Another.Length){
printf("%s左 边 赢 了",Pempty(20)) ;
}
else if(Another.life&&Person.Length<Another.Length){
printf("%s右 边 赢 了",Pempty(20)) ;
}
printf("\n%s分数 : %d\n\n\n\n" ,Pempty(22),Person.Length+Another.Length) ;
for( int i = 0 ; i < 3 ; i++){
printf("%s%s\n\n\n",Pempty(23),wDead[i]) ;
}
for(int i=0 ;i<2 ;i++){
printf("------------",Pposition(21,one*3+9+i*2)) ;
printf("#",Pposition(21+i*11,one*3+10) ) ;
}
Scan:
Sleep(125) ;
if((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& one != 0){
one-- ;
}
else if((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& one != 2){
one++ ;
}
else if( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
switch(one){
case 0:{
iBegin = true ;
Prepare() ;
}break ;
case 1:{
iBegin = iMenu = true ;
}break ;
}
return ;
}
else{
goto Scan ;
}
goto Print ;
}
/* 随机果实 */
void Foods()
{
do{
Food.x = rand() % ( WALL - 2) +1 ;
Food.y = rand() % ( WALL - 2) +1 ;
}while( Map[Food.y][Food.x]!=0) ;
printf("★",Pcolor( 4 , 3),Pposition( Food.x * 2 ,Food.y)) ;
}
/* 分数保存 */
void GradeOut(int grade)
{
for(int i=2 ;i>=0 ;i--){
if(Score[i]<grade){
Score[i+1] = Score[i] ;
Score[i] = grade ;
}
}
fprintf(fopen("score.txt","w+"),"%d\n%d\n%d",Score[0],Score[1],Score[2]) ;
}
玩法:
W为上,A为左,S为下,D为右,Enter为确定(提示:切换英文输入法!!!)
文档不要删掉!!!
麻烦加个关注吧!作者制作不易!
C++的游戏--贪吃蛇相关推荐
- Python制作当年第一款手机游戏-贪吃蛇游戏(练习)
前言: 文章利用Python pygame做一个贪吃蛇的小游戏而且讲清楚每一段代码是用来干嘛的. 据说是贪吃蛇游戏是1976年,Gremlin公司推出的经典街机游戏,那我们今天用Python制作的这个 ...
- Unity3D_(游戏)贪吃蛇
Unity制作贪吃蛇小游戏 玩家通过"WASD"控制小蛇上下左右移动,蛇头撞倒食物,则食物被吃掉,蛇身体长一节,接着又出现食物,等待蛇来吃,如果蛇在移动中撞到墙或身体交叉蛇头撞倒自 ...
- pygame render怎么显示中文_Pygame游戏——贪吃蛇(完结)
我们的贪吃蛇游戏基本功能已经实现,可以完成吃食物,吃到食物变长,得分增加,食物再次随机出现. 但是我们有发现贪吃蛇可以穿到屏幕外面去,这显然是不合适的. 这一次我们需要完善的是: 游戏结束 游戏结束后 ...
- 游戏编程笔记-起步(一)一个简单的游戏-贪吃蛇
二 游戏编程起步 1.一个简单的游戏-贪吃蛇 1.贪吃蛇游戏剖析 1)游戏的目标.在不被撞死的前提下,吃掉奖子增加自己的长度,来完成升级. 2)游戏中的物体.蛇,墙壁,奖子. 3)动作.蛇移动,蛇吃奖 ...
- 用pygame做一个简单的python小游戏---贪吃蛇
用pygame做一个简单的python小游戏-贪吃蛇 贪吃蛇游戏博客链接:(方法一样,语言不一样) c++贪吃蛇:https://blog.csdn.net/weixin_46791942/artic ...
- 微信小游戏——贪吃蛇
博客简介 本篇博客介绍的是微信小游戏贪吃蛇的案例,详细的开发过程,并且提供代码压缩包下载. 案例简介 布局 构建对象 响应事件 蛇头对象的移动 蛇身的移动 食物刷新 绘制得分 碰撞检验 开始界面和结束 ...
- 游戏贪吃蛇(c语言)
游戏贪吃蛇(c语言) 一.项目背景 二.设计目的 三.项目功能需求 1.初始化游戏 2.控制蛇的运行轨迹 3.控制蛇的长度 4.控制食物生成 5.计算分数 6.结束条件 四.系统的功能结构图 五.总体 ...
- JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二)
目录 JAVA课程设计(小游戏贪吃蛇)完整源码附素材(一) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(三) 前言 1. 任务描述 1.1 ...
- 小游戏贪吃蛇的c++源代码
小游戏贪吃蛇的c++源代码,快来看看,还带解释 #include #include #include #include #include <conio.h> #include #inclu ...
- C#winform 经典小游戏贪吃蛇V1.0(一)
关于V1.0 为什么我给这个版本定义为V1.0嘞,因为在这个版本中仅仅实现了蛇的自动行进,按键对蛇的行进方向的操作和吃掉食物蛇身的增长等操作. 但是任何事情都必须一步一步来,当我们完成这个乞丐版的 ...
最新文章
- 修改value_Java 反射修改String引发的思考?
- golang log.Fatal() 和 panic() 函数的区别
- 大作文_p2_v1.0
- 使用spring实现邮件的发送(含测试,源码,注释)
- 软件测试的含义以及测试的对象
- 人渣scum服务器物品,人渣SCUM可以采集物品一览_可采集食物大全_可可网
- 为label或者textView添加placeHolder
- 各种 USB 数据线引脚图【总结】
- 微信小程序04 数据绑定
- CV笔记5:图像分割之最大类间方差法、自适应阈值分割(基于python-opencv实现)
- Redis雪崩效应的解决方案(转)
- 命令行排序文件夹大小
- Component xxx does not have a method xxx to handle event xxx
- 2017.9.26 noip模拟赛 总结
- excel怎么合并表格
- faceapp一直显示选择服务器,faceapp提示choosing a serve_faceapp提示choosing a serve解决办法_玩游戏网...
- 奋斗(2)第16集剧情介绍
- python选课管理系统_Python开发程序:选课系统
- 【配置JAVA环境】
- 线缆水平垂直燃烧测试及测试设备
热门文章
- Android动画学习——Tween Animation
- Jetson TK1 刷机安装Ubuntu系统与Mini PCI-e无线网卡
- 【NVIDIA Jetson TK1】一,开发板重装ubuntu系统,重新刷机,重新烧系统,恢复系统
- 哈曼推出Savari MECWAVE:超低时延边缘计算平台提供交钥匙式互联服务
- [NOIP2008]笨小猴 T1
- 光纤耦合系统的公差分析
- 路由器总是有信号无网络连接到服务器,路由器有信号没网络怎么回事?路由器没网络的解决方案...
- php phacon 关联模型吗,Phalcon模型
- 李白打酒 递归 C语言
- java 中介模式_java设计模式-中介者模式