21xrx.com
2024-11-22 04:05:26 Friday
登录
文章检索 我的文章 写文章
C++贪吃蛇游戏代码项目设计
2023-07-08 19:10:08 深夜i     --     --
C++ 游戏代码 贪吃蛇 项目设计 编程实现

贪吃蛇是一款经典的游戏,也是很多程序员用来练手的好项目。下面是一份基于C++语言的贪吃蛇游戏代码项目设计,供大家学习参考。

代码结构:

1.头文件和常量定义部分:


#include<iostream>

#include<Windows.h>

#include<conio.h>

#include<ctime>

using namespace std;

const int MAXROW=20;

const int MAXCOL=30;

const int SNAKESIZE=21;

const int FOODSIZE=2;

enum DirectionUP;

2.贪吃蛇结构体:


struct SnakeNode

  int x;

  int y;

;

3.主程序:


void main()

{

  SetConsoleTitle("贪吃蛇");

  srand((unsigned)time(0));

  int snakeLength=3;

  SnakeNode snake[SNAKESIZE];

  Direction dir=RIGHT;

  bool isDead=false;

  int fx,fy;

  createFood(snake,snakeLength,fx,fy);

  while(!isDead)

  {

    Sleep(200);

    if(_kbhit())

    {

      switch(_getch())

      {

        case 'w':

          if(dir!=DOWN)

          

            dir=UP;

          

          break;

        case 's':

          if(dir!=UP)

          

            dir=DOWN;

          

          break;

        case 'a':

          if(dir!=RIGHT)

          

            dir=LEFT;

          

          break;

        case 'd':

          if(dir!=LEFT)

          

            dir=RIGHT;

          

          break;

        default:

          break;

      }

    }

    moveSnake(snake,snakeLength,dir,isDead);

    if(eatFood(snake,fx,fy,snakeLength))

    {

      createFood(snake,snakeLength,fx,fy);

    }

    drawMap(snake,snakeLength,fx,fy);

  }

  system("pause");

}

4.贪吃蛇移动方法:


void moveSnake(SnakeNode *snake,int &snakeLength,Direction dir,bool &isDead)

{

  for(int i=snakeLength-1;i>0;i--)

  {

    snake[i].x=snake[i-1].x;

    snake[i].y=snake[i-1].y;

  }

  switch(dir)

  {

    case UP:

      snake[0].y--;

      break;

    case DOWN:

      snake[0].y++;

      break;

    case LEFT:

      snake[0].x--;

      break;

    case RIGHT:

      snake[0].x++;

      break;

    default:

      break;

  }

  if(snake[0].x<0||snake[0].x>=MAXCOL||snake[0].y<0||snake[0].y>=MAXROW)

  {

    isDead=true;

  }

  for(int i=1;i<snakeLength;i++)

  {

    if(snake[0].x==snake[i].x&&snake[0].y==snake[i].y)

    {

      isDead=true;

      break;

    }

  }

}

5.随机生成食物方法:


void createFood(SnakeNode *snake,int snakeLength,int &fx,int &fy)

{

  bool isOK=false;

  while(!isOK)

  {

    isOK=true;

    fx=rand()%(MAXCOL-FOODSIZE+1);

    fy=rand()%(MAXROW-FOODSIZE+1);

    for(int i=0;i<snakeLength;i++)

    {

      if(snake[i].x==fx&&snake[i].y==fy)

      {

        isOK=false;

        break;

      }

    }

  }

}

6.判断是否吃到食物方法:


bool eatFood(SnakeNode *snake,int fx,int fy,int &snakeLength)

{

  if(snake[0].x>=fx&&snake[0].x<=fx+FOODSIZE-1&&snake[0].y>=fy&&snake[0].y<=fy+FOODSIZE-1)

  {

    snakeLength++;

    snake[snakeLength-1].x=snake[snakeLength-2].x;

    snake[snakeLength-1].y=snake[snakeLength-2].y;

    return true;

  }

  return false;

}

7.绘制地图方法:


void drawMap(SnakeNode *snake,int snakeLength,int fx,int fy)

{

  system("cls");

  for(int i=0;i<MAXROW;i++)

  {

    for(int j=0;j<MAXCOL;j++)

    {

      if(i==0||i==MAXROW-1||j==0||j==MAXCOL-1)

      {

        cout<<"#";

      }

      else if(i>=fy&&i<fy+FOODSIZE&&j>=fx&&j<fx+FOODSIZE)

      {

        cout<<"$";

      }

      else

      {

        bool isBody=false;

        for(int k=0;k<snakeLength;k++)

        {

          if(snake[k].x==j&&snake[k].y==i)

          {

            cout<<"*";

            isBody=true;

            break;

          }

        }

        if(!isBody)

        {

          cout<<" ";

        }

      }

    }

    cout<<endl;

  }

  cout<<"分数:"<<(snakeLength-3)*10<<endl;

}

通过学习这份C++贪吃蛇游戏代码项目设计,你可以初步了解如何使用C++实现这款经典游戏,也可以进一步优化代码,增强游戏体验。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复