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

本文实例讲述了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.len - 1].x;
   ++b.len;
  }
  if(DOWN == b.Direction)
  {
   b.pos[b.len].y = b.pos[b.len - 1].y + 1;
   b.pos[b.len].x = b.pos[b.len - 1].x;
   ++b.len;
  }
  if(LEFT == b.Direction)
  {
   b.pos[b.len].x = b.pos[b.len - 1].x - 2;
   b.pos[b.len].y = b.pos[b.len - 1].y;
   ++b.len;
  }
  if(RIGHT == b.Direction)
  {
   b.pos[b.len].x = b.pos[b.len - 1].x + 2;
   b.pos[b.len].y = b.pos[b.len - 1].y;
   ++b.len;
  }
 }
}

效果图如下所示:

希望本文所述对大家的C++程序设计有所帮助。

(0)

相关推荐

  • VC++中的字体设置方法详解

    VC++中static text字体改变 窗口都有2个和字体有关的函数:CWnd::GetFont()和SetFont(CFont*, BOOL);1)CFont* pFont = m_static.GetFont(); 2)LOGFONT LogFont;pFont->GetLogFont(&LogFont); 3)对LogFont直接操纵修改里面的字体选项 //如LogFont.lfUnderline = 1;设置下划线 LogFont.lfHeight=30;       //字体大小

  • C++实现基于控制台界面的吃豆子游戏

    本文实例讲述了C++实现基于控制台界面的吃豆子游戏.分享给大家供大家参考.具体分析如下: 程序运行界面如下所示: ESC键可退出游戏. main.cpp源文件如下: #include "lib.h" #pragma once extern int level; int main() { FOOD food; WALL wall; BODY CurPos; HALL hall; int iExit = 0; while(1) { if(iExit) break; Init(&fo

  • C++实现简单的扫雷游戏(控制台版)

    C++新手的代码,请各位多包涵. 用C++写的一个简单的控制台版扫雷游戏.玩家通过输入方块的坐标来翻开方块. 只是一个雏形,能够让玩家执行翻开方块的操作并且判断输赢,还未添加标记方块.游戏菜单.记录游戏时间.重新开一局等等的功能. 玩家输入坐标的方式来翻开方块只适用于小型的"雷区",若"雷区"大了,用坐标会变得很不方便. 代码片段扫雷V1.1 #include<stdio.h> #include<Windows.h> #define YELL

  • 原创的C语言控制台小游戏

    最开始左上色块被感染,通过切换颜色,不断感染同色色块.亮点是可以切换图案,设置方块个数和最大限制次数.整体还是比较满意,希望大神指教. #include <stdio.h> #include <windows.h> #include <conio.h> #include <time.h> #include <stdlib.h> int DIFFICULT=44; int count=0 ; int TYPE_SHAPE=2 ; int flag=

  • C/C++实现控制台输出不同颜色字体的方法

    本文实例讲述了C/C++实现控制台输出不同颜色字体的方法.分享给大家供大家参考,具体如下: 在控制台输出不同颜色的字 效果 代码: #include "stdio.h" #include "windows.h" int main(int argn, char **argv) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); printf("Hello&q

  • 用VC++6.0的控制台实现2048小游戏的程序

    首先感谢这位大侠的无私分享,仔细学习这个程序以后收获很多,试着添加一些注释 源程序是从开源中国看到的,原作者是 刘地(sir?) 地址为http://www.oschina.net/code/snippet_593413_46040 geek_monkey于2015年3月5日为拜读该程序,受益匪浅 为了方便自己,以及更多初学者阅读,我试着写了写了注释供参考 我是C语言初学者,如有错误希望指正.轻喷 复制代码 代码如下: #include <stdlib.h> #include <stdi

  • c语言在控制台判定鼠标左键的小例子

    复制代码 代码如下: // temp1.cpp : Defines the entry point for the console application. // //#include <stdafx.h> #include <windows.h> #include <conio.h> #include <stdlib.h> #include<stdio.h> int main(int argc, char* argv[]) {  SetCons

  • C语言控制台版2048小游戏

    效果不好,见谅,没事就写了一个!!! /** * @author Routh * @main.c * @date 2014, 4, 26 */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> // console width #define CONSOLE_WIDTH 80 #define BOX_WIDTH 10 int BOX[4][4] = {

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

    本文实例讲述了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; H

  • 基于C语言实现贪吃蛇小游戏

    本文实例为大家分享了C语言实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 1.目标要求: 1.上下左右控制蛇头转向2.若蛇头碰到食物,长度加一3.若蛇头碰到边框.碰到自身或蛇回头,游戏结束 2.C语言代码: #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #define interface_x 19//画布行数 #define interface_y

  • 基于pygame实现贪吃蛇小游戏示例

    目录 游戏截图 引入库 初始化模型 获取键盘事件 移动贪吃蛇 吃食物逻辑 碰撞到自身逻辑 结束游戏 显示文字函数 完整代码 游戏截图 引入库 import copy import random import sys import pygame 初始化模型 # 蛇模型 snake_list = [[10, 10]] # 食物的模型 x = random.randint(10, 490) y = random.randint(10, 490) food_point = [x, y] food_r,

  • 基于Python实现贪吃蛇小游戏(附源码)

    目录 前言 主要设计 应用知识点 1.python知识点 2.pygamezero知识点 功能截图 代码实现 1.蛇的表示 2.蛇的前进移动 3.控制移动方向 4.游戏失败 5.食物的随机出现 6.游戏得分 源码 总结 前言 这几年人工智能技术大发展,Python因此几乎成了第一位的语言.实际上,多年来,它不仅在软件工程师中得到广泛使用,也是各行业通用的专家语言,就是说,不管孩子以后做什么,都可能用得着.准备针对我自己上小学的孩子,每周抽出些时间,通过学习他们感兴趣的小游戏,逐步把python知

  • 基于MFC实现贪吃蛇小游戏

    本文实例为大家分享了MFC实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 一.功能描述 (1)通过"START"."PAUSE"."EXIT"三个控件,控制游戏的开始.暂停和终止. (2)通过四个方向键控制蛇的运动方向,吃掉随机出现的豆子. (3)吃到豆子蛇身会变长,同时得分增加:碰到壁或者自身则该轮游戏结束. (4)游戏开始.暂停.结束时进行提示. 二.概要设计 1.系统框架 2.游戏流程 三.主要功能实现 1.主要函数 (1)Test

  • C++代码实现贪吃蛇小游戏

    本文实例为大家分享了C++实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 1.游戏描述 贪吃蛇可谓是从小玩到大的经典趣味小游戏,蛇每吃到一次食物,身体就会长一节,如果撞到墙或者撞到自身,游戏结束. 2.代码实现 1.首先需要思考的问题是如何指定位置输出字符?这时候就有一个非常强大的函数叫 gotoxy() ,现在库函数里边已经没有了,只能我们自己实现,代码中注释很完整,自行阅读即可. 2.实现了指哪画哪的目标之后,就可以开始游戏内容制作了.首先便是圈地,即画地图,一个简简单单的循环就能安排

  • C语言单链表贪吃蛇小游戏

    C语言实现单链表控制台贪吃蛇小游戏 编译环境:vs2019 需求: 统计游戏开始后的时间,控制贪吃蛇:吃到食物蛇身加长,得分加一:碰墙或蛇头碰到身体减一条生命:生命消耗完则结束游戏. 思路: 使用wasd键控制蛇的移动方向,蛇头碰到食物得分加一,并在地图上随机产生一个食物,累加得分,碰墙或碰自己减一条生命,并初始化整条蛇,生命值为0时结束游戏. 做法: 使用单链表控制贪吃蛇移动的核心思想就是:链表存储贪吃蛇所有坐标,每次循环贪吃蛇不断向一个方向插入一个新的结点作为新的蛇头,按下按键控制新蛇头产生

  • C语言实现贪吃蛇小游戏

    本文实例为大家分享了C语言实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 一.程序实现的原理: 1.构造蛇身:定义一个坐标数组,存放的是蛇的每一节蛇身所在的坐标位置.这样就将移动蛇身的操作转换为移动数组的操作,将吃食物增加蛇身体长度的操作转换为在数组后面追加元素的操作. 2.移动效果:每次移动时,将每一节蛇身(蛇头除外)依次往前移动一节,然后擦去蛇的最后一节,最后确定蛇头的方向,再绘制一个蛇头.这样就会显示一个移动效果. 3.身体增加效果:每次移动时候,判断蛇头是否碰到了食物,如果碰到了食

  • C++实现简单贪吃蛇小游戏

    本文实例为大家分享了C++实现简单贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 1 贪吃蛇游戏原理 1.1 构造蛇身:定义一个坐标数组,存放的是蛇的每一节蛇身所在的坐标位置. 1.2 移动效果:每次移动时,将每一节蛇身(蛇头除外)依次往前移动一节,去掉蛇的最后一节,确定蛇的方向 1.3 移动判断:每次移动时,判断蛇头是否触碰食物,如果碰到了食物,只进行前移蛇身和增加蛇头的操作,不进行擦除蛇尾的操作 2游戏设计 2.1 游戏首页 2.2 开始游戏 2.3 退出游戏 3.游戏实现 3.1 游戏首

  • JS+Canvas实现贪吃蛇小游戏

    今天呢,主要和小伙伴们分享一下一个贪吃蛇游戏从构思到实现的过程~因为我不是很喜欢直接PO代码,所以只copy代码的童鞋们请出门左转不谢. 按理说canvas与其应用是老生常谈了,可我在准备阶段却搜索不到有用的资料(不是代码!),所以说呢,只能自力更生 -_- 首先是大致要考虑的东西: 1.要有蛇(没蛇怎么叫贪吃蛇). 2.然后要有地图(蛇是不能上天的). 3.不能水平\垂直掉头(如果想掉头,需要至少变换方位并且至少移动一格才可). 4.食物(不然怎么贪吃). 5.吃了食物要变长(这才是精髓).

随机推荐