21xrx.com
2025-04-02 12:53:42 Wednesday
文章检索 我的文章 写文章
"C++编写的飞机大战游戏代码"
2023-06-27 01:04:21 深夜i     13     0
C++ 飞机大战游戏 代码 游戏开发 程序设计

C++编写的飞机大战游戏代码

飞机大战游戏是一个经典的游戏,它利用了游戏玩家的操作反应能力和思维能力,让人感到快乐和满足。如果想学习C++编程语言,可以尝试编写一个飞机大战游戏,这有助于提高你的编程技能和沉淀你的经验。下面就是一个简单的C++编写的飞机大战游戏代码。

在编写游戏代码之前,首先需要列出游戏的需求和流程。飞机大战游戏的需求和流程如下:

需求:

- 游戏场景是一个空中的背景,有无数个障碍物随机出现。玩家操作飞机,避开障碍物并打落敌人的飞机,获取游戏分数。

- 游戏分数需要记录下来,玩家可以在游戏结束后看到自己的得分。

- 玩家可以通过键盘操作飞机的移动,并发射子弹攻击敌人的飞机。

- 敌人的飞机需要随机出现,并向着玩家的飞机移动,直到碰撞才会导致游戏结束。

流程:

- 游戏开始,初始化场景和玩家的飞机,开始循环。

- 循环中实时更新玩家的飞机位置,添加子弹和障碍物,判断敌人是否出现并移动。

- 判断玩家的飞机是否碰到了障碍物或敌人,如果是则游戏结束。

- 玩家射杀敌人的飞机可以得到游戏分数,分数需要实时显示出来。

- 循环结束后,显示游戏结束画面和玩家得分。

接下来是C++编写的飞机大战游戏代码,代码包括三个文件:main.cpp、Plane.h、Plane.cpp。其中main.cpp是程序入口,Plane.h和Plane.cpp分别定义了飞机类的属性和方法。

main.cpp:

#include <iostream>
#include <iomanip>
#include <ctime>
#include "Plane.h"
using namespace std;
const int MAX_BULLET_NUM = 10;
const int MAX_OBSTACLE_NUM = 50;
const int MAX_ENEMY_NUM = 5;
const int MAX_SCORE_NUM = 100;
const int FRAME_RATE = 30;
void gameLoop(int, int);
void showGameover(int);
int main() {
  srand(time(NULL));
  int width = 60, height = 20;
  gameLoop(width, height);
  return 0;
}
void gameLoop(int width, int height) {
  Plane* myplane = new Plane(width / 2, height - 2);
  Plane* enemies[MAX_ENEMY_NUM];
  for (int i = 0; i < MAX_ENEMY_NUM; i++) {
    enemies[i] = NULL;
  }
  int bullet_num = 0;
  Plane* bullets[MAX_BULLET_NUM];
  for (int i = 0; i < MAX_BULLET_NUM; i++) {
    bullets[i] = NULL;
  }
  int obstacle_num = 0;
  Plane* obstacles[MAX_OBSTACLE_NUM];
  for (int i = 0; i < MAX_OBSTACLE_NUM; i++) {
    obstacles[i] = NULL;
  }
  int score = 0, score_num = 0;
  while (true) {
    myplane->updatePlane();
    if (rand() % 100 < 5) {
      for (int i = 0; i < MAX_ENEMY_NUM; i++) {
        if (enemies[i] == NULL) {
          enemies[i] = new Plane(rand() % width, 0);
          break;
        }
      }
    }
    for (int i = 0; i < MAX_ENEMY_NUM; i++) {
      if (enemies[i] != NULL) {
        enemies[i]->movePlane(myplane);
      }
    }
    if (rand() % 100 < 10) {
      for (int i = 0; i < MAX_OBSTACLE_NUM; i++) {
        if (obstacles[i] == NULL) {
          obstacles[i] = new Plane(rand() % width, 0);
          break;
        }
      }
    }
    for (int i = 0; i < MAX_BULLET_NUM; i++) {
      if (bullets[i] != NULL) {
        bullets[i]->movePlane(myplane);
        for (int j = 0; j < MAX_ENEMY_NUM; j++) {
          if (enemies[j] != NULL && bullets[i]->checkCollision(enemies[j])) {
            score += 10;
            delete enemies[j];
            enemies[j] = NULL;
            delete bullets[i];
            bullets[i] = NULL;
            break;
          }
        }
        for (int j = 0; j < MAX_OBSTACLE_NUM; j++) {
          if (obstacles[j] != NULL && bullets[i]->checkCollision(obstacles[j])) {
            delete bullets[i];
            bullets[i] = NULL;
            break;
          }
        }
      }
    }
    if (rand() % 100 < 15) {
      score_num++;
      if (score_num >= MAX_SCORE_NUM)
        score_num = 0;
      
    }
    if (rand() % 100 < 5) {
      for (int i = 0; i < MAX_BULLET_NUM; i++) {
        if (bullets[i] == NULL) {
          bullets[i] = new Plane(myplane->x, myplane->y - 1);
          break;
        }
      }
    }
    for (int i = 0; i < MAX_ENEMY_NUM; i++) {
      if (enemies[i] != NULL && enemies[i]->checkCollision(myplane)) {
        showGameover(score);
        return;
      }
    }
    for (int i = 0; i < MAX_OBSTACLE_NUM; i++) {
      if (obstacles[i] != NULL && obstacles[i]->checkCollision(myplane)) {
        showGameover(score);
        return;
      }
    }
    cout << "Score: " << setw(5) << setfill('0') << score_num << " ";
    cout << "Health: " << setw(2) << setfill('0') << myplane->health << endl;
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        if (i == myplane->y && j == myplane->x)
          cout << "A";
         else {
          bool printed = false;
          for (int k = 0; k < MAX_ENEMY_NUM; k++) {
            if (enemies[k] != NULL && enemies[k]->x == j && enemies[k]->y == i)
              cout << "E";
              printed = true;
              break;
            
          }
          if (!printed) {
            for (int k = 0; k < MAX_BULLET_NUM; k++) {
              if (bullets[k] != NULL && bullets[k]->x == j && bullets[k]->y == i)
                cout << "|";
                printed = true;
                break;
              |";
                printed = true;
                break;
              }
            }
          }
          if (!printed) {
            for (int k = 0; k < MAX_OBSTACLE_NUM; k++) {
              if (obstacles[k] != NULL && obstacles[k]->x == j && obstacles[k]->y == i)
                cout << "#";
                printed = true;
                break;
              
            }
          }
          if (!printed)
            cout << ".";
          
        }
      }
      cout << endl;
    }
    for (int i = 0; i < MAX_BULLET_NUM; i++) {
      if (bullets[i] != NULL && (bullets[i]->y < 0 || bullets[i]->y >= height)) {
        delete bullets[i];
        bullets[i] = NULL;
      }
    }
    for (int i = 0; i < MAX_ENEMY_NUM; i++) {
      if (enemies[i] != NULL && enemies[i]->y >= height) {
        delete enemies[i];
        enemies[i] = NULL;
        score_num += 10;
        if (score_num >= MAX_SCORE_NUM) {
          score += 10;
          score_num = 0;
        }
      }
    }
    for (int i = 0; i < MAX_OBSTACLE_NUM; i++) {
      if (obstacles[i] != NULL && obstacles[i]->y >= height) {
        delete obstacles[i];
        obstacles[i] = NULL;
      }
    }
    Sleep(1000 / FRAME_RATE);
    system("cls");
  }
}
void showGameover(int score) {
  system("cls");
  cout << "Game Over!" << endl;
  cout << "Score: " << score << endl;
}

Plane.h:

#pragma once
#include <cstdlib>
#include <iostream>
#include <Windows.h>
using namespace std;
class Plane {
public:
  int x, y;
  int health;
  int speed;
  Plane(int, int);
  ~Plane();
  void updatePlane();
  void movePlane(Plane*);
  bool checkCollision(Plane*);
  void damagePlane(int);
private:
  int last_update_frame;
};
Plane::Plane(int init_x, int init_y) {
  x = init_x;
  y = init_y;
  health = 3;
  speed = 1;
  last_update_frame = GetTickCount();
}
Plane::~Plane()
void Plane::updatePlane() {
  if (GetTickCount() - last_update_frame >= 1000 / FRAME_RATE) {
    last_update_frame = GetTickCount();
    if (health > 0) {
      int key = 0;
      if (kbhit()) {
        key = getch();
      }
      if (key == ' ')
        health--;
      
      if (key == 'a' && x > 0)
        x--;
      
      if (key == 'd' && x < 59) {
        x++;
      }
      if (key == 'w' && y > 0)
        y--;
      
      if (key == 's' && y < 19) {
        y++;
      }
    } else
      x = -1;
      y = -1;
    
  }
}
void Plane::movePlane(Plane* target) {
  if (y < target->y && y < 19) {
    y += speed;
  }
  if (y > target->y && y > 0)
    y -= speed;
  
  if (x < target->x && x < 59) {
    x += speed;
  }
  if (x > target->x && x > 0)
    x -= speed;
  
}
bool Plane::checkCollision(Plane* target) {
  return (abs(x - target->x) < 2) && (abs(y - target->y) < 2);
}
void Plane::damagePlane(int damage)
  health -= damage;

Plane.cpp:

#include "Plane.h"
Plane::Plane(int init_x, int init_y) {
  x = init_x;
  y = init_y;
  health = 3;
  speed = 1;
  last_update_frame = GetTickCount();
}
Plane::~Plane()
void Plane::updatePlane() {
  if (GetTickCount() - last_update_frame >= 1000 / FRAME_RATE) {
    last_update_frame = GetTickCount();
    if (health > 0) {
      int key = 0;
      if (kbhit()) {
        key = getch();
      }
      if (key == ' ')
        health--;
      
      if (key == 'a' && x > 0)
        x--;
      
      if (key == 'd' && x < 59) {
        x++;
      }
      if (key == 'w' && y > 0)
        y--;
      
      if (key == 's' && y < 19) {
        y++;
      }
    } else
      x = -1;
      y = -1;
    
  }
}
void Plane::movePlane(Plane* target) {
  if (y < target->y && y < 19) {
    y += speed;
  }
  if (y > target->y && y > 0)
    y -= speed;
  
  if (x < target->x && x < 59) {
    x += speed;
  }
  if (x > target->x && x > 0)
    x -= speed;
  
}
bool Plane::checkCollision(Plane* target) {
  return (abs(x - target->x) < 2) && (abs(y - target->y) < 2);
}
void Plane::damagePlane(int damage)
  health -= damage;

以上就是一个简单的C++编写的飞机大战游戏代码,你可以在此代码上进行修改和扩展,实现更多的游戏需求和功能。

  
  

评论区

请求出错了