21xrx.com
2024-09-17 03:50:56 Tuesday
登录
文章检索 我的文章 写文章
"C++实现贪吃蛇游戏代码"
2023-07-09 17:47:39 深夜i     --     --
C++ 贪吃蛇 游戏 代码 实现

贪吃蛇游戏是一个广受欢迎的休闲游戏,现在我们可以用C++语言实现这个经典的游戏。

首先,我们需要定义蛇的运动轨迹和食物的位置。在这个游戏中,蛇的身体由一系列点组成,每个点对应蛇的一个身体部分。当蛇吃到食物时,蛇的身体会增加一个点。食物是随机出现在屏幕上的,每次蛇吃掉食物后会重新生成一个食物。

接下来,我们需要定义蛇的运动规则。蛇可以向上、下、左、右四个方向运动,玩家可以使用键盘操作蛇的方向。当蛇碰到边界或碰到自己的身体时,游戏结束。

我们可以使用C++的图形库来实现游戏的界面。在每次游戏运行时,我们会显示蛇和食物在屏幕上的位置,并且根据玩家的操作刷新蛇的位置和状态。

实现贪吃蛇游戏的代码很简单,首先需要定义蛇的数据结构,然后定义食物的位置和生成规则。接下来,在游戏循环中更新蛇的位置和状态,并且检测蛇是否吃到了食物或发生碰撞。最后,在游戏结束时,输出游戏得分并询问玩家是否继续游戏。

下面是一个简单的C++实现的贪吃蛇游戏代码:


#include <graphics.h>

#include <conio.h>

#include <time.h>

#define UP 1

#define DOWN 2

#define LEFT 3

#define RIGHT 4

#define WIDTH 600

#define HEIGHT 600

#define SIZE 20

struct snake

{

  int x, y;

  struct snake *next;

};

struct food

  int x;

int main()

{

  // 初始化图形库

  initgraph(WIDTH, HEIGHT);

  // 设置随机数种子

  srand((unsigned)time(NULL));

  // 初始化蛇和食物

  struct snake *head = (struct snake*)malloc(sizeof(struct snake));

  head->x = WIDTH / 2;

  head->y = HEIGHT / 2;

  head->next = NULL;

  struct snake *tail = head;

  struct food f;

  f.x = (rand() % (WIDTH / SIZE)) * SIZE;

  f.y = (rand() % (HEIGHT / SIZE)) * SIZE;

  // 初始化游戏状态

  int direction = RIGHT;

  int score = 0;

  char score_str[10];

  // 游戏循环

  while (1)

  {

    // 画蛇和食物

    setfillcolor(BLUE);

    struct snake *p = head;

    while (p)

    {

      solidrectangle(p->x, p->y, p->x + SIZE, p->y + SIZE);

      p = p->next;

    }

    setfillcolor(RED);

    solidrectangle(f.x, f.y, f.x + SIZE, f.y + SIZE);

    // 判断蛇是否吃到了食物,更新分数和蛇的状态

    if (head->x == f.x && head->y == f.y)

    {

      score++;

      sprintf(score_str, "%d", score);

      f.x = (rand() % (WIDTH / SIZE)) * SIZE;

      f.y = (rand() % (HEIGHT / SIZE)) * SIZE;

      struct snake *node = (struct snake*)malloc(sizeof(struct snake));

      node->x = tail->x;

      node->y = tail->y;

      node->next = NULL;

      tail->next = node;

      tail = node;

    }

    // 绘制分数

    settextstyle(20, 0, _T("Arial"));

    outtextxy(10, 10, score_str);

    // 控制蛇的运动方向

    if (kbhit())

    {

      int key = getch();

      if ((key == 'w' || key == 'W') && direction != DOWN)

      

        direction = UP;

      

      else if ((key == 's' || key == 'S') && direction != UP)

      

        direction = DOWN;

      

      else if ((key == 'a' || key == 'A') && direction != RIGHT)

      

        direction = LEFT;

      

      else if ((key == 'd' || key == 'D') && direction != LEFT)

      

        direction = RIGHT;

      

    }

    // 更新蛇的位置和状态

    struct snake *prev = NULL;

    p = head;

    while (p)

    {

      if (p == head)

      {

        // 更新蛇头的位置

        if (direction == UP)

        

          p->y -= SIZE;

        

        else if (direction == DOWN)

        {

          p->y += SIZE;

        }

        else if (direction == LEFT)

        

          p->x -= SIZE;

        

        else if (direction == RIGHT)

        {

          p->x += SIZE;

        }

        // 判断蛇是否超出边界

        if (p->x < 0 || p->y < 0 || p->x >= WIDTH || p->y >= HEIGHT)

        {

          outtextxy(WIDTH / 2 - 50, HEIGHT / 2, _T("Game Over!"));

          getchar();

          return 0;

        }

      }

      else

      

        // 更新蛇身体的位置

        p->x = prev->x;

        p->y = prev->y;

      

      prev = p;

      p = p->next;

    }

    // 判断蛇是否碰到自己的身体

    p = head->next;

    while (p)

    {

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

      {

        outtextxy(WIDTH / 2 - 50, HEIGHT / 2, _T("Game Over!"));

        getchar();

        return 0;

      }

      p = p->next;

    }

    // 延迟一段时间

    Sleep(100);

    // 清除屏幕

    cleardevice();

  }

  // 关闭图形库

  closegraph();

  return 0;

}

总的来说,这个C++实现的贪吃蛇游戏代码简单易懂,适合初学者练习和学习。同时,我们也可以通过这个实例来加深对C++语言的理解和运用。

  
  

评论区

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