21xrx.com
2024-09-19 08:53:01 Thursday
登录
文章检索 我的文章 写文章
C++贪吃蛇代码及分析
2023-07-11 21:07:11 深夜i     --     --
C++ 贪吃蛇 代码 分析 游戏设计

贪吃蛇是一种经典游戏,它的规则简单易懂,但是操作却比较复杂,需要一定的技巧和耐心。在编程方面,很多人都想自己写一个贪吃蛇游戏。下面给大家分享一份C++贪吃蛇代码及分析,希望能够帮助到大家。

代码:


#include <iostream>

#include <conio.h>

#include <windows.h>

#include <cstdio>

#include <cstdlib>

#include <ctime>

using namespace std;

bool change;

void gotoxy(int x,int y)//光标跳转

{

   HANDLE screen_handle = GetStdHandle(STD_OUTPUT_HANDLE);

   COORD pos=x;

   SetConsoleCursorPosition(screen_handle,pos);            

}

class Food

{

public:

  int x,y;

  void show_food()const

  {

    gotoxy(x,y);

    printf("@");

  }

};

class Snake

{

public:

  struct SnakeNode

  {

    int x,y;

    SnakeNode *next;

  }*head ,*tail;

  int front_direction;  

  Snake()tail=NULL;front_direction=4;

  void start()//构建蛇

  {

    head=new SnakeNode;

    head->x=16;

    head->y=5;

    SnakeNode*p=new SnakeNode;

    p->x=17;

    p->y=5;

    head->next=p;//头指针指向下一个节点

    p->next=NULL;//尾指针则指向NULL

    SnakeNode*p1=new SnakeNode;

    p1->x=18;

    p1->y=5;

    p->next=p1;

    p1->next=NULL;

    tail=p1;

  }

  void show_snake()const//输出蛇

  {

    SnakeNode*p=head;

    while(p)

    {

      gotoxy(p->x,p->y);

      if(p==head)

      {

        printf("@");//蛇头

      }

      else

      {

        printf("*");//蛇身

      }

      p=p->next;

    }

  }

  void add_snake()//增加蛇

  {

    SnakeNode*p=new SnakeNode;

    p->x=tail->x;

    p->y=tail->y+1;//新的节点在尾巴的正下方

    tail->next=p;

    p->next=NULL;

    tail = p;

   }

  void move_snake()//移动蛇

  {

    add_snake();//先新增一个节点

    SnakeNode*p=head;

    while(p)//遍历一下所有的节点,将当前节点的坐标赋值为前一个节点的坐标

    {

      SnakeNode*temp=p->next;

      p->next=temp->next;

      temp->next=NULL;

      if(p==head)

      

        temp->next=head;

        head=temp;

       

      else

      

        delete p;

      

      p=temp;

    }

    switch(front_direction)//控制蛇移动的方向

    {

      case 1:head->x-=1;break;

      case 2:head->x+=1;break;

      case 3:head->y-=1;break;

      case 4:head->y+=1;break;

    }

    if(head->x==0)head->x=30;

    if(head->x==31)head->x=1;

    if(head->y==0)head->y=20;

    if(head->y==21)head->y=1;

   }

   bool judge_food(Food&fd)//判断食物的位置

   {

    SnakeNode*p=head;

    while(p)

    {

      if(p->x==fd.x&&p->y==fd.y)

      {

        fd.x=rand()%30+1;//生成新的食物位置

        fd.y=rand()%20+1;

        add_snake();//增加一个节点

        return true;//返回吃到食物

      }

      p=p->next;

    }

    return false;//没有吃到食物

   }

   bool judge_wall()const//判断是否触碰到墙壁

   {

    if(head->x==1||head->x==30||head->y==1||head->y==20)

    

      return true;

    

    return false;

   }

   bool judge_snake()const//判断是否吃到自己

   {

    SnakeNode*p=head->next;

    while(p)

    {

      if(head->x==p->x&&head->y==p->y)

      

        return true;

      

      p=p->next;

     }

    return false;

   }

};

int main()

{

  int score=0;

  Snake snake;

  snake.start();

  Food fd;

  fd.x=10;

  fd.y=10;

  snake.show_snake();

  fd.show_food();

  char key;

  while(1)

  {

    if(kbhit())

    {

      key=getch();

      if(key=='w'&&snake.front_direction!=4)snake.front_direction=1;

      if(key=='s'&&snake.front_direction!=1)snake.front_direction=4;

      if(key=='a'&&snake.front_direction!=2)snake.front_direction=3;

      if(key=='d'&&snake.front_direction!=3)snake.front_direction=2;

    }

    else

    { 

      sleep(200);//中场休息

      system("cls");//清除上一次输出的东西

      snake.move_snake();

      if(snake.judge_food(fd))//如果吃到了食物

      {

        fd.show_food();//输出新的食物

        score++;

      }

      if(snake.judge_wall()||snake.judge_snake())

      {

        gotoxy(15,10);

        printf("GAME OVER!!!\n");

        gotoxy(15,11);

        printf("your score is %d !!!\n",score);

        break;

      }

      snake.show_snake();

      gotoxy(34,1);

      printf("score :%d ",score);//输出分数

      fd.show_food();

    }

  }

  system("pause");

  return 0;

}

分析:

上述代码中,主要使用了三个类,分别是Food、Snake和SnakeNode。其中Food类用来生成食物,并在屏幕上显示;SnakeNode类用来定义蛇的每个节点的属性,以及它们之间的联系;而Snake类则是整个蛇的类,它封装了蛇的各种动作,比如增加节点、移动、显示等。

代码中主要使用了gotoxy函数来控制光标的位置,以实现在屏幕上输出蛇和食物的功能。同时,利用Sleep函数来控制每次移动的时间,以实现蛇的运动效果。

在主函数中,利用kbhit函数来判断用户是否按下了键盘,并通过getch函数获取到用户按下的键。这样用户就可以通过键盘来控制蛇的移动方向了。另外,使用了rand函数来生成随机位置的食物,并且通过多个判断函数来检测蛇是否碰到了墙壁或者吃到了自己。

总的来说,这份C++贪吃蛇代码实现了基本的功能,并且代码结构清晰,容易理解。对于刚刚开始学习C++的朋友来说,这是一个不错的练手项目,可以在实践中加深对C++编程的理解。

  
  

评论区

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