21xrx.com
2024-09-20 00:01:33 Friday
登录
文章检索 我的文章 写文章
使用C++编写贪吃蛇游戏:最简单代码
2023-06-26 22:27:22 深夜i     --     --
C++ 贪吃蛇 游戏 编写 最简单代码

贪吃蛇游戏是一款经典的游戏,其规则简单且易于上手,深受玩家们的喜爱。本文将介绍使用C++编写贪吃蛇游戏的最简单代码。

首先,我们需要定义贪吃蛇的基本属性。具体来说,我们需要定义贪吃蛇的长度、头部方向、身体坐标等。接下来,我们需要定义食物的属性,包括其坐标和是否被贪吃蛇吃掉。

接着,我们需要编写游戏主体。我们需要定义游戏画面、贪吃蛇的行动方式、食物的位置随机生成、贪吃蛇吃掉食物后长度增加等等。最后,我们需要编写游戏结束的条件,即当贪吃蛇碰到边界或者碰到自己身体时,游戏结束。

下面是具体的代码实现:


#include <iostream>

#include <windows.h>

#include <conio.h>

#include <stdlib.h>

using namespace std;

const int width = 20;

const int height = 20;

int x, y, fx, fy, score;

int snakex[100], snakey[100], snakelen, direction;

void gotoxy(int x, int y)

{

  COORD pos = x;

  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}

void hidecursor()

{

  CONSOLE_CURSOR_INFO cursor_info = 1;

  SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

void init()

{

  snakelen = 5;

  direction = 3;

  x = width / 2;

  y = height / 2;

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

    snakex[i] = x - i, snakey[i] = y;

  fx = rand() % (width - 2) + 1, fy = rand() % (height - 2) + 1;

}

void draw()

{

  gotoxy(0, 0);

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

  {

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

    {

      gotoxy(j, i);

      if (i == 0 || i == height || j == 0 || j == width)

      

        cout << "#";

      

      else if (i == y && j == x)

      {

        cout << "*";

      }

      else if (i == fy && j == fx)

      

        cout << "o";

      

      else

      {

        bool print = false;

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

          if (snakey[k] == i && snakex[k] == j)

            cout << "*",

            print = true;

        if (!print)

          cout << " ";

      }

    }

  }

  gotoxy(0, height + 1);

  cout << "Score: " << score;

}

void input()

{

  if (_kbhit())

  {

    switch (_getch())

    

    case 'a':

      direction = 0;

      break;

    case 'd':

      direction = 1;

      break;

    case 'w':

      direction = 2;

      break;

    case 's':

      direction = 3;

      break;

    

  }

}

void logic()

{

  int temp1 = snakex[0], temp2 = snakey[0];

  int temp3, temp4;

  snakex[0] += direction == 0 ? -1 : direction == 1 ? 1 : 0;

  snakey[0] += direction == 2 ? -1 : direction == 3 ? 1 : 0;

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

  {

    temp3 = snakex[i], temp4 = snakey[i];

    snakex[i] = temp1, snakey[i] = temp2;

    temp1 = temp3, temp2 = temp4;

  }

  if (snakex[0] == 0 || snakex[0] == width - 1 || snakey[0] == 0 || snakey[0] == height - 1)

  {

    cout << "Game Over!\n";

    exit(0);

  }

  if (snakex[0] == fx && snakey[0] == fy)

  {

    score += 10;

    snakelen++;

    fx = rand() % (width - 2) + 1, fy = rand() % (height - 2) + 1;

  }

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

    if (snakex[0] == snakex[i] && snakey[0] == snakey[i])

    {

      cout << "Game Over!\n";

      exit(0);

    }

}

int main()

{

  hidecursor();

  init();

  while (true)

  {

    draw();

    input();

    logic();

    Sleep(60);

  }

  return 0;

}

通过上述代码,我们就可以编写出最简单的贪吃蛇游戏。在代码中,我们使用了Windows.h库和Conio.h库,这两个库分别用于控制控制台窗口和控制键盘输入。在定义函数时,我们使用了gotoxy()函数和hidecursor()函数,分别用于定位光标位置和隐藏光标。在游戏主体中,我们通过三个函数实现了游戏的显示、输入和逻辑处理。最后,在main()函数中,我们将上述函数组合起来实现循环进行游戏并打印结果。

  
  

评论区

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