21xrx.com
2024-09-20 00:18:25 Friday
登录
文章检索 我的文章 写文章
C++小游戏编程代码大全
2023-06-27 02:02:31 深夜i     --     --
C++ 小游戏 编程代码 大全 开发

C++是一种面向对象的高级编程语言,它可以用于开发各种类型的软件和游戏。如果你正在学习C++编程,那么你可能会对如何编写小游戏感兴趣。在本文中,我们将介绍一些C++小游戏编程代码的示例,以帮助你入门和实践。

1.《贪吃蛇》

贪吃蛇是一种非常经典的小游戏,它可以帮助你理解C++的基础语法和数据结构。以下是一个简单的贪吃蛇游戏示例:


#include <iostream>

#include <conio.h>

#include <windows.h>

using namespace std;

bool gameOver;

const int width = 20;

const int height = 20;

int x, y, fruitX, fruitY, score;

int tailX[100], tailY[100];

int nTail;

enum eDirection STOP = 0;

eDirection dir;

void Setup() {

  gameOver = false;

  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':

        gameOver = true;

        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;

    default:

      break;

  }

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

    gameOver = true;

  

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

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

      gameOver = true;

    

  }

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

    score += 10;

    fruitX = rand() % width;

    fruitY = rand() % height;

    nTail++;

  }

}

int main() {

  Setup();

  while (!gameOver) {

    Draw();

    Input();

    Logic();

    Sleep(100);

  }

  return 0;

}

2.《太空侵略者》

太空侵略者是另一种经典的小游戏,它可以帮助你理解C++中的类和继承。以下是一个简单的太空侵略者游戏示例:


#include <iostream>

#include <conio.h>

#include <windows.h>

using namespace std;

const int width = 50;

const int height = 20;

int score = 0;

class Entity {

public:

  int x, y;

  int dx, dy;

  int size;

  char shape;

  Entity(int startX, int startY, int startDX, int startDY, int startSize, char startShape)

    x = startX;

    y = startY;

    dx = startDX;

    dy = startDY;

    size = startSize;

    shape = startShape;

  

  void Draw() {

    for (int i = 0; i < size; i++) {

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

        cout << shape;

      

      cout << endl;

    }

  }

  void Move() {

    x += dx;

    y += dy;

    if (x < 0 || x >= width - size)

      dx = -dx;

    

    if (y < 0 || y >= height - size)

      dy = -dy;

    

  }

};

class Player : public Entity {

public:

  Player(int startX, int startY) : Entity(startX, startY, 0, 0, 3, 'M') {}

  void Input() {

    if (_kbhit()) {

      switch (_getch())

        case 'a':

          dx = -1;

          break;

        case 'd':

          dx = 1;

          break;

        case 'w':

          dy = -1;

          break;

        case 's':

          dy = 1;

          break;

      

    }

  }

  void CheckCollision(Entity* other) {

    if (x < other->x + other->size && x + size > other->x && y < other->y + other->size && y + size > other->y) {

      score += 10;

      other->x = rand() % (width - other->size);

      other->y = rand() % (height - other->size);

    }

  }

};

int main() {

  Entity* entities[10];

  Player* player = new Player(width / 2 - 1, height - 4);

  for (int i = 0; i < 10; i++) {

    entities[i] = new Entity(rand() % (width - 2) + 1, rand() % (height - 2) + 1, rand() % 2 == 0 ? -1 : 1, rand() % 2 == 0 ? -1 : 1, 2, 'E');

  }

  while (true) {

    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 == player->y && j == player->x)

          cout << player->shape;

         else {

          bool print = false;

          for (int k = 0; k < 10; k++) {

            if (entities[k]->x == j && entities[k]->y == i) {

              cout << entities[k]->shape;

              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;

    Sleep(100);

    for (int i = 0; i < 10; i++) {

      entities[i]->Move();

      player->CheckCollision(entities[i]);

    }

    player->Input();

    player->Move();

  }

  return 0;

}

以上两个示例游戏代码提供了相对简单但完整的游戏编程参考,希望能够帮助你在C++编程中更好地理解和实践游戏开发。

  
  

评论区

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