21xrx.com
2024-11-22 09:58:36 Friday
登录
文章检索 我的文章 写文章
「C++简单游戏代码大全」
2023-06-25 16:57:36 深夜i     --     --
C++ 游戏代码 简单 大全

C++是一种高级编程语言,广泛用于开发各种类型的应用程序,包括游戏。如果你对游戏编程有兴趣,那么C++简直是必须掌握的语言。

为了让大家更好地掌握C++游戏编程,本文整理了一些简单的游戏代码,供大家参考学习。

1. 打飞机游戏

这是一个经典的打飞机游戏,代码非常简单。游戏中,我们需要控制一个小飞机躲开敌机的攻击,并尽可能地击落敌机。代码如下:


#include <iostream>

#include <conio.h>

#include <windows.h>

using namespace std;

const int width = 20;

const int height = 20;

int x, y;

int fruitX, fruitY;

int score;

enum eDirection STOP = 0;

eDirection dir;

void Setup()

{

  dir = STOP;

  x = width / 2;

  y = height / 2;

  fruitX = rand() % width;

  fruitY = rand() % height;

  score = 0;

}

void Draw()

{

  system("cls");

  for (int i = 0; i < width+2; i++)

    cout << "#";

  cout << endl;

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

  {

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

    {

      if (j == 0)

        cout << "#";

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

        cout << "O";

      else if (i == fruitY && j == fruitX)

        cout << "F";

      else

        cout << " ";

      if (j == width - 1)

        cout << "#";

    }

    cout << endl;

  }

  for (int i = 0; i < width+2; i++)

    cout << "#";

  cout << endl;

  cout << "Score:" << score << endl;

}

void Input()

{

  if (_kbhit())

  {

    switch (_getch())

    

    case 'a':

      dir = LEFT;

      break;

    case 'd':

      dir = RIGHT;

      break;

    case 'w':

      dir = UP;

      break;

    case 's':

      dir = DOWN;

      break;

    case 'x':

      dir = STOP;

      break;

    

  }

}

void Logic()

{

  switch (dir)

  {

  case LEFT:

    x--;

    break;

  case RIGHT:

    x++;

    break;

  case UP:

    y--;

    break;

  case DOWN:

    y++;

    break;

  }

  if (x > width || x < 0 || y > height || y < 0)

    Setup();

  if (x == fruitX && y == fruitY)

  {

    score += 10;

    fruitX = rand() % width;

    fruitY = rand() % height;

  }

}

int main()

{

  Setup();

  while (true)

  {

    Draw();

    Input();

    Logic();

    Sleep(10);

  }

  return 0;

}

2. 贪吃蛇游戏

贪吃蛇是另一个非常受欢迎的游戏,同样非常适合作为学习C++的编程练习。游戏中,你需要控制一个蛇去吃食物,并不断变长。如果蛇头碰到了墙壁或自己的身体,那么游戏就结束了。代码如下:


#include <iostream>

#include <conio.h>

#include <windows.h>

using namespace std;

const int width = 20;

const int height = 20;

int x, y;

int fruitX, fruitY;

int score;

int tailX[100], tailY[100];

int nTail;

enum eDirection UP;

eDirection dir;

void Setup()

{

  dir = STOP;

  x = width / 2;

  y = height / 2;

  fruitX = rand() % width;

  fruitY = rand() % height;

  score = 0;

}

void Draw()

{

  system("cls");

  for (int i = 0; i < width+2; i++)

    cout << "#";

  cout << endl;

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

  {

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

    {

      if (j == 0)

        cout << "#";

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

        cout << "O";

      else if (i == fruitY && j == fruitX)

        cout << "F";

      else

      {

        bool print = false;

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

        {

          if (tailX[k] == j && tailY[k] == i)

          

            cout << "o";

            print = true;

          

        }

        if (!print)

          cout << " ";

      }

      if (j == width - 1)

        cout << "#";

    }

    cout << endl;

  }

  for (int i = 0; i < width+2; i++)

    cout << "#";

  cout << endl;

  cout << "Score:" << score << endl;

}

void Input()

{

  if (_kbhit())

  {

    switch (_getch())

    

    case 'a':

      dir = LEFT;

      break;

    case 'd':

      dir = RIGHT;

      break;

    case 'w':

      dir = UP;

      break;

    case 's':

      dir = DOWN;

      break;

    case 'x':

      dir = STOP;

      break;

    

  }

}

void Logic()

{

  int prevX = tailX[0];

  int prevY = tailY[0];

  int prev2X, prev2Y;

  tailX[0] = x;

  tailY[0] = y;

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

  {

    prev2X = tailX[i];

    prev2Y = tailY[i];

    tailX[i] = prevX;

    tailY[i] = prevY;

    prevX = prev2X;

    prevY = prev2Y;

  }

  switch (dir)

  {

  case LEFT:

    x--;

    break;

  case RIGHT:

    x++;

    break;

  case UP:

    y--;

    break;

  case DOWN:

    y++;

    break;

  }

  if (x > width || x < 0 || y > height || y < 0)

    Setup();

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

    if (tailX[i] == x && tailY[i] == y)

      Setup();

  if (x == fruitX && y == fruitY)

  {

    score += 10;

    fruitX = rand() % width;

    fruitY = rand() % height;

    nTail++;

  }

}

int main()

{

  Setup();

  while (true)

  {

    Draw();

    Input();

    Logic();

    Sleep(10);

  }

  return 0;

}

以上就是两个简单的C++游戏代码,希望大家可以学习到有用的知识!

  
  

评论区

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