21xrx.com
2025-04-04 02:54:55 Friday
文章检索 我的文章 写文章
简单易学:C++贪吃蛇代码双人对战
2023-07-01 03:20:12 深夜i     38     0
C++ 贪吃蛇代码 双人对战 简单易学 游戏编程

贪吃蛇是一款经典的小游戏,让人们在怀旧的同时也能感受到游戏的愉悦。而今天我们将介绍一种简单易学的C++贪吃蛇代码,同时还支持双人对战。

首先,我们需要了解一些基本概念。贪吃蛇游戏的核心是在一个空间内实现蛇的移动,同时吃掉食物加分。当蛇碰到边界或自己的身体时,游戏结束。而双人对战则加入了两条蛇之间互相攻击的元素。

下面是一份简单的C++代码,可供参考:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
const int n = 25, m = 50;
int x1, y1, dx1, dy1, len1 = 2, score1 = 0, x2, y2, dx2, dy2, len2 = 2, score2 = 0, speed = 100;
bool gameover1 = false, gameover2 = false;
char a[n][m];
void init1()
{
  x1 = n / 2;
  y1 = m / 3;
  dx1 = 0;
  dy1 = 1;
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      if (i == 0 || i == n - 1 || j == 0 || j == m - 1)
        a[i][j] = '*';
      else
        a[i][j] = ' ';
    }
  }
  a[x1][y1] = '#';
  a[x1][y1 + 1] = '#';
}
void init2()
{
  x2 = n / 2;
  y2 = m / 3 * 2;
  dx2 = 0;
  dy2 = -1;
  a[x2][y2] = '#';
  a[x2][y2 - 1] = '#';
}
void draw()
{
  system("cls");
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      cout << a[i][j];
    }
    cout << endl;
  }
  cout << "Score1: " << score1 << endl << "Score2: " << score2 << endl;
}
void input()
{
  if (_kbhit())
  {
    char ch = _getch();
    if (ch == 'a')
    {
      if (dy1 != 1)
      
        dx1 = 0;
        dy1 = -1;
      
    }
    if (ch == 'd')
    {
      if (dy1 != -1)
      
        dx1 = 0;
        dy1 = 1;
      
    }
    if (ch == 'w')
    {
      if (dx1 != 1)
      
        dx1 = -1;
        dy1 = 0;
      
    }
    if (ch == 's')
    {
      if (dx1 != -1)
      
        dx1 = 1;
        dy1 = 0;
      
    }
    if (ch == 'j')
    {
      if (dy2 != 1)
      
        dx2 = 0;
        dy2 = -1;
      
    }
    if (ch == 'l')
    {
      if (dy2 != -1)
      
        dx2 = 0;
        dy2 = 1;
      
    }
    if (ch == 'i')
    {
      if (dx2 != 1)
      
        dx2 = -1;
        dy2 = 0;
      
    }
    if (ch == 'k')
    {
      if (dx2 != -1)
      
        dx2 = 1;
        dy2 = 0;
      
    }
    if (ch == 'q')
      gameover1 = true;
    if (ch == 'p')
      gameover2 = true;
  }
}
void logic()
{
  int nx1 = x1 + dx1;
  int ny1 = y1 + dy1;
  int nx2 = x2 + dx2;
  int ny2 = y2 + dy2;
  if (nx1 >= n - 1 || nx1 < 0 || ny1 >= m - 1 || ny1 < 0 || a[nx1][ny1] == '#')
    gameover1 = true;
  if (nx2 >= n - 1 || nx2 < 0 || ny2 >= m - 1 || ny2 < 0 || a[nx2][ny2] == '#')
    gameover2 = true;
  if (a[nx1][ny1] == '$')
  {
    score1++;
    len1++;
    speed = max(speed - 10, 50);
    a[nx1][ny1] = '#';
    if (nx2 == nx1 && ny2 == ny1)
    {
      score2--;
      if (len2 > 2)
      {
        len2--;
        for (int i = 0; i < n; i++)
        {
          for (int j = 0; j < m; j++)
          {
            if (a[i][j] == '*' || a[i][j] == ' ' || a[i][j] == '$')
              continue;
            if (a[i][j] == '#')
              a[i][j] = ' ';
          }
        }
        int t = len2;
        int x = x2;
        int y = y2 - 1;
        while (t--)
        {
          a[x][y] = '#';
          y--;
        }
      }
      else
      
        gameover2 = true;
      
    }
  }
  else
  {
    a[x1 + dx1 * (len1 - 1)][y1 + dy1 * (len1 - 1)] = ' ';
    for (int i = len1 - 1; i > 0; i--)
    {
      a[x1 + dx1 * i][y1 + dy1 * i] = a[x1 + dx1 * (i - 1)][y1 + dy1 * (i - 1)];
    }
    a[x1][y1] = ' ';
    x1 = nx1;
    y1 = ny1;
    a[x1][y1] = '#';
  }
  if (a[nx2][ny2] == '$')
  {
    score2++;
    len2++;
    speed = max(speed - 10, 50);
    a[nx2][ny2] = '#';
    if (nx2 == nx1 && ny2 == ny1)
    {
      score1--;
      if (len1 > 2)
      {
        len1--;
        for (int i = 0; i < n; i++)
        {
          for (int j = 0; j < m; j++)
          {
            if (a[i][j] == '*' || a[i][j] == ' ' || a[i][j] == '$')
              continue;
            if (a[i][j] == '#')
              a[i][j] = ' ';
          }
        }
        int t = len1;
        int x = x1;
        int y = y1 + 1;
        while (t--)
        {
          a[x][y] = '#';
          y++;
        }
      }
      else
      
        gameover1 = true;
      
    }
  }
  else
  {
    a[x2 + dx2 * (len2 - 1)][y2 + dy2 * (len2 - 1)] = ' ';
    for (int i = len2 - 1; i > 0; i--)
    {
      a[x2 + dx2 * i][y2 + dy2 * i] = a[x2 + dx2 * (i - 1)][y2 + dy2 * (i - 1)];
    }
    a[x2][y2] = ' ';
    x2 = nx2;
    y2 = ny2;
    a[x2][y2] = '#';
  }
  Sleep(speed);
}
void run()
{
  while (!gameover1 && !gameover2)
  {
    draw();
    input();
    logic();
  }
  if (gameover1)
    cout << "Player1 lost!" << endl;
  if (gameover2)
    cout << "Player2 lost!" << endl;
}
int main()
{
  init1();
  init2();
  run();
  return 0;
}

该代码实现了一个25x50的游戏空间,同时在游戏开始时分别初始化了两条蛇,支持双人游戏。通过键盘输入控制两条蛇的移动并在相应条件下进行判断得分。整个游戏运行的逻辑在run函数中。同时,通过不断地调整speed变量的值来实现游戏节奏。

总的来说,这份贪吃蛇代码相对较为简单,适合初学者学习和实践,不需要过多的编程经验。希望读者们通过学习该代码加深对C++的了解,同时也能在游戏的快乐中度过时光。

  
  

评论区

请求出错了