• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > C++基于控制台实现的贪吃蛇小游戏

C++基于控制台实现的贪吃蛇小游戏

作者:司青 字体:[增加 减小] 来源:互联网 时间:2017-05-28

司青 通过本文主要向大家介绍了c++小游戏程序,c++小游戏源代码,c++小游戏,用c++编写小游戏,c++小游戏程序代码等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了C++基于控制台实现的贪吃蛇小游戏。分享给大家供大家参考。具体实现方法如下:

#include <windows.h>
#include <time.h>
#include <stdio.h>
#define MAX   100
#define UP    1
#define DOWN  2
#define LEFT   3
#define RIGHT  4
#define MOVING 5
#define STOP   0
HANDLE hMain_Out = NULL;
HANDLE hMain_In = NULL;
struct Pos
{
 int x;
 int y;
};
struct Body
{
 int state;
 int len;
 int Direction;
// int ZZZZ;
// int HHHH;
 Pos pos[MAX];
};
Pos NewPos[MAX];
Pos Food;
SMALL_RECT Wall;
int count = 0;
int grade = 0;
int level = 1;
int amount = 0;
int speed = 200;
void Init(Body &b);
void Print(const Body &b);
void Print(int x,int y);
void Move(Body &b);
void Clean(int x,int y);
void Clean(const Body &b);
void ShowInfo();
int GetDirection(Body &b);
void TurnRound(int Direction,Body &b);
void PosCopy(Body &b,Pos NewPos[]);
void MoveBody(Body &b);
void HideCursor();
void CreateWall();
void CreateFood();
bool IsKnock_Food(const Body &b);
bool IsKnock_Wall(const Body &b);
void AddBody(Body &b);

int main()
{
 Body b;
 Init(b);
 Print(b);
 HideCursor();
 while(TRUE)
 {
  Sleep(speed);
  Move(b);
  GetDirection(b);
 }
 return 0;
}

void Init(Body &b)
{
 b.len = 3;
 b.Direction = RIGHT;
 b.state = STOP;
 b.pos[0].x = 2;
 b.pos[0].y = 1;
 b.pos[1].x = 4;
 b.pos[1].y = 1;
 b.pos[2].x = 6;
 b.pos[2].y = 1;
 hMain_Out = GetStdHandle(STD_OUTPUT_HANDLE);
 hMain_In = GetStdHandle(STD_INPUT_HANDLE);
 CreateWall();
 CreateFood();
 ShowInfo();
}
void Print(const Body &b)
{
 COORD coord;
 for(int ix = b.len -1;ix >= 0;--ix)
 {
  coord.X = b.pos[ix].x;
  coord.Y = b.pos[ix].y;
  SetConsoleCursorPosition(hMain_Out,coord);
  printf("●");
 }
}
void Move(Body &b)
{
 ShowInfo();
 if(IsKnock_Wall(b))
 {
  MessageBox(NULL,"You are dead !","Oh my God",0);
  exit(0);
 }
 if(IsKnock_Food(b))
 {
  if(amount > 5)
  {
   ++level;
   amount = 0;
   speed-= 50;
  }
  AddBody(b);
  grade += 10;
  ++amount;
  Clean(Food.x,Food.y);
  CreateFood();
 }
 if(STOP == b.state)
 {
  if(RIGHT == b.Direction)
  {
   for(int ix = 0;ix < b.len;++ix)
   {
    Clean(b.pos[ix].x,b.pos[ix].y);
    b.pos[ix].x+=2;
   }
  }
  if(UP == b.Direction)
  {
   for(int ix = 0;ix < b.len;++ix)
   {
    Clean(b.pos[ix].x,b.pos[ix].y);
    b.pos[ix].y--;
   }
  }
  if(DOWN == b.Direction)
  {
   for(int ix = 0;ix < b.len;++ix)
   {
    Clean(b.pos[ix].x,b.pos[ix].y);
    b.pos[ix].y++;
   }
  }
  if(LEFT == b.Direction)
  {
   for(int ix = 0;ix < b.len;++ix)
   {
    Clean(b.pos[ix].x,b.pos[ix].y);
    b.pos[ix].x-=2;
   }
  }
 }

 if(MOVING == b.state)
 {
  PosCopy(b,NewPos);
  if(UP == b.Direction)
  {
   if(b.len == count)
   {
    b.state = STOP;
    b.Direction = UP;
    count = 0;
   }
   if(count < b.len && MOVING == b.state)
   {
    b.pos[b.len - 1].y--;
    Clean(b.pos[0].x,b.pos[0].y);
    MoveBody(b);
    Print(b);
   }
  }
  if(DOWN == b.Direction)
  {
   if(b.len == count)
   {
    b.state = STOP;
    b.Direction = DOWN;
    count = 0;
   }
   if(count < b.len && MOVING == b.state)
   {
    b.pos[b.len - 1].y++;
    Clean(b.pos[0].x,b.pos[0].y);
    MoveBody(b);
    Print(b);
   }
  }
  if(LEFT == b.Direction)
  {
   if(b.len == count)
   {
    b.state = STOP;
    b.Direction = LEFT;
    count = 0;
   }
   if(count < b.len && MOVING == b.state)
   {
    b.pos[b.len - 1].x-=2;
    Clean(b.pos[0].x,b.pos[0].y);
    MoveBody(b);
    Print(b);
   }
  }
  if(RIGHT == b.Direction)
  {
   if(b.len == count)
   {
    b.state = STOP;
    b.Direction = RIGHT;
    count = 0;
   }
   if(count < b.len && MOVING == b.state)
   {
    b.pos[b.len - 1].x+=2;
    Clean(b.pos[0].x,b.pos[0].y);
    MoveBody(b);
    Print(b);
   } 
  }
 }
 Print(b);
}
void Clean(int x,int y)
{
 COORD c;
 c.X = x;
 c.Y = y;
 SetConsoleCursorPosition(hMain_Out,c);
 printf(" ");
}
void Clean(const Body &b)
{
 for(int ix = 0;ix < b.len;++ix)
 {
  Clean(b.pos[ix].x,b.pos[ix].y);
 }
}
int GetDirection(Body &b)
{
 if(GetAsyncKeyState(VK_UP))
 {
  count = 0;
  TurnRound(UP,b);
 }
 if(GetAsyncKeyState(VK_DOWN))
 {
  count = 0;
  TurnRound(DOWN,b);
 }
 if(GetAsyncKeyState(VK_LEFT))
 {
  count = 0;
  TurnRound(LEFT,b);
 }
 if(GetAsyncKeyState(VK_RIGHT))
 {
  count = 0;
  TurnRound(RIGHT,b);
 }
 return 0;
}
void TurnRound(int d,Body &b)
{
 switch(d)
 {
 case UP:
  if(RIGHT == b.Direction || LEFT == b.Direction)
  {
   PosCopy(b,NewPos);
   --b.pos[b.len -1].y;
   Clean(b.pos[0].x,b.pos[0].y);
   MoveBody(b);
   Print(b);
   b.Direction = d;
   b.state = MOVING;
  }
  break;
 case DOWN:
  if(RIGHT == b.Direction || LEFT == b.Direction)
  {
   PosCopy(b,NewPos);
   ++b.pos[b.len -1].y;
   Clean(b.pos[0].x,b.pos[0].y);
   MoveBody(b);
   Print(b);
   b.Direction = d;
   b.state = MOVING;
  }
  break;
 case LEFT:
  if(UP == b.Direction || DOWN == b.Direction)
  {
   PosCopy(b,NewPos);
   b.pos[b.len -1].x-=2;
   Clean(b.pos[0].x,b.pos[0].y);
   MoveBody(b);
   Print(b);
   b.Direction = d;
   b.state = MOVING;
  }
  break;
 case RIGHT:
  if(UP == b.Direction || DOWN == b.Direction)
  {
   PosCopy(b,NewPos);
   b.pos[b.len -1].x+=2;
   Clean(b.pos[0].x,b.pos[0].y);
   MoveBody(b);
   Print(b);
   b.Direction = d;
   b.state = MOVING;
  }
  break;
 default:
  break;
 }
}
void PosCopy(Body &b,Pos NewPos[])
{
 for(int ix = 0;ix < b.len;++ix)
 {
  NewPos[ix].x=0;
  NewPos[ix].y=0;
 }
 for(int ix = 0;ix <b.len;++ix)
 {
  NewPos[ix] = b.pos[ix];
 }
}

void MoveBody(Body &b)
{
 for(int ix = b.len - 1;ix > 0;--ix)
 {
  b.pos[ix - 1] = NewPos[ix];
 }
 ++count;
 PosCopy(b,NewPos);
}

void HideCursor()
{
 CONSOLE_CURSOR_INFO info;
 GetConsoleCursorInfo(hMain_Out,&info);
 info.bVisible = FALSE;
 SetConsoleCursorInfo(hMain_Out,&info);
}

void CreateWall()
{
 CONSOLE_SCREEN_BUFFER_INFO info;
 GetConsoleScreenBufferInfo(hMain_Out,&info);
 info.srWindow.Right-=19;
 info.srWindow.Bottom-=5;
 Wall = info.srWindow;
 for(int i = 0;i <= info.srWindow.Right;i+=2)
 {
  Print(i,info.srWindow.Top);
  Print(i,info.srWindow.Bottom);
 }
 for(int y = 0;y <= info.srWindow.Bottom;++y)
 {
  Print(0,y);
  Print(info.srWindow.Right,y);
 }
}

void Print(int x,int y)
{
 COORD c;
 c.X = x;
 c.Y = y;
 SetConsoleCursorPosition(hMain_Out,c);
 printf("■");
}

void CreateFood()
{
 srand(unsigned(time(NULL)));
 unsigned x_t = RAND_MAX / Wall.Right;
 unsigned y_t = RAND_MAX / Wall.Bottom;
 while(true)
 {
  int x = rand() / x_t;
  int y = rand() / y_t;
  Food.x = x - 4;
  Food.y = y - 4;
  if((0 == Food.x % 2) && (0 == Food.y % 2))
  {
   if(Food.x < 5)
   {
    Food.x+=8;
   }
   if(Food.y<5)
   {
    Food.y+=8;
   }
   Print(Food.x,Food.y);
   break;
  }
 }
}

bool IsKnock_Food(const Body &b)
{
 if(b.pos[b.len - 1].x == Food.x && b.pos[b.len - 1].y== Food.y)
 {
  return true;
 }
 else
 {
  return false;
 }
}

bool IsKnock_Wall(const Body &b)
{
 if(0 == b.pos[b.len - 1].x || 0 == b.pos[b.len - 1].y || Wall.Right == b.pos[b.len - 1].x || Wall.Bottom == b.pos[b.len - 1].y)
 {
  return true;
 }
 Pos Head = b.pos[b.len - 1];
 for(int ix = 0;ix <= b.len - 3;++ix)
 {
  if(Head.x == b.pos[ix].x && Head.y == b.pos[ix].y)
  {
   return true;
  }
 }
 return false;
}

void ShowInfo()
{
 COORD c;
 c.X = Wall.Right + 2;
 c.Y = 3;
 SetConsoleCursorPosition(hMain_Out,c);
 printf("  分数:%d",grade);
 c.Y+=10;
 SetConsoleCursorPosition(hMain_Out,c);
 printf("  难度等级:%d",level);

}

void AddBody(Body &b)
{
 if(b.len < MAX)
 {
  if(UP == b.Direction)
  {
   b.pos[b.len].y = b.pos[b.len - 1].y - 1;
   b.pos[b.len].x = b.pos[b.le



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • c++ 端口扫描程序实现案例
  • 在C++程序中开启和禁用Windows设备的无线网卡的方法
  • 深入解析C++程序中激发事件和COM中的事件处理
  • 使用C++程序获取新浪行情数据的方法
  • 探究在C++程序并发时保护共享数据的问题
  • C++抽奖程序实现方法
  • C++基于控制台实现的贪吃蛇小游戏
  • C++键盘记录程序代码
  • C++实现闹钟程序的方法

相关文章

  • 2017-05-28Cocos2d-x UI开发之CCControlButton控件类实例
  • 2017-05-28纯C语言:递归组合数源码分享
  • 2017-05-28复数乘法中的结构体赋值实现代码
  • 2017-05-28C语言实现程序开机自启动
  • 2017-05-28详解C++中shared_ptr的使用教程
  • 2017-05-28详解C语言中的ttyname()函数和isatty()函数的用法
  • 2017-05-28C++实现打印1到最大的n位数
  • 2018-08-06支持C++,python,java等语言的跨平台的农历库(天文历算法)
  • 2017-05-28解析C++函数的默认参数和占位参数及较之C语言的拓展
  • 2017-05-28解析C++中不能重载为友元函数的四个运算符

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 解析c语言switch中break语句的具体作用
    • tc编译的dos程序和vc编译的win32控制台程序的异同
    • c_str()的用法详细解析
    • C++设计模式之外观模式
    • 基于堆的基本操作的介绍
    • C语言实现统计字符串单词数
    • 浅谈使用Rapidxml 库遇到的问题和分析过程(分享)
    • C++设计模式之观察者模式
    • 斐波那契数列 优化矩阵求法实例
    • c语言 time.h 详细介绍

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有