【C++】学习笔记(二)----C++飞机大战

c++飞机大战

第一次写博客可能会有一堆毛病请见谅
建议:先练习贪吃蛇再练习飞机大战
game.cpp文件

#include<iostream>
#include"Gamemanage.h"
using namespace std;
int main()
{
    Gamemanage play;
    play.game();
    return 0;
}

Gamemanage.h文件

#pragma once
#include<iostream>
using namespace std;
class Gamemanage
{
public:
	Gamemanage();
	struct Play
	{
		string name;
		int score;
		string difficulty;
		struct Play* next;
	};
	void game();                     //运行程序
	void menu(int choice);           //主菜单
	void help_menu();                //帮助菜单
	void settings_menu(int choice);  //设置菜单
	void start_game();               //开始游戏
	void game_message();             //游戏信息
	void pause_game();               //暂停游戏
	void gameover();                 //游戏结束
	void game_settings();            //游戏设置
private:
	int m_iscore;           //游戏分数
	string m_idifficulty;   //游戏难度(简单,普通,困难)
};

Gamemanage.cpp文件

#include<iostream>
#include"Gamemanage.h"
#include<iostream>
#include<ctime>
#include <conio.h>
#include<string>
#include<Windows.h>
#include"Wall.h"
#include"Airplane.h"
#include"Enemy.h"
#include"Attack.h"
#include"gamemanage.h"
using namespace std;
void gotoxy(HANDLE hOut, int x, int y)
{
    COORD pos;
    pos.X = x; //横坐标
    pos.Y = y; //纵坐标
    SetConsoleCursorPosition(hOut, pos);
}
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
void HideCursor();	//隐藏光标
Gamemanage::Gamemanage()
{
    m_iscore = 0;               //初始化分数
    m_idifficulty = "简单";     //初始化难度
}
//运行程序
void Gamemanage::game()
{
    int choice = 1;//开始菜单中控制选择的变量
    while (true)
    {
        system("cls");//清屏
        Gamemanage::menu(choice);
        //判断是否为回车键(Enter 的ASCII码为 13),确定选择的模式
        int move = _getch();
        if (move != 13)
        {
            switch (move)//判断上下移动
            {
            case 'w':choice -= 1; if (choice < 1)choice = 4; break;
            case 's':choice += 1; if (choice > 4)choice = 1; break;
            case 'W':choice -= 1; if (choice < 1)choice = 4; break;
            case 'S':choice += 1; if (choice > 4)choice = 1; break;
            }
            continue;
        }
        //按了回车确定模式后进行以下内容
        if (choice == 1)//开始游戏
        {
            Gamemanage::start_game();
        }
        else if (choice == 2)//帮助
        {
            Gamemanage::help_menu();
        }
        else if (choice == 3)//设置
        {
            Gamemanage::game_settings();
        }
        else if (choice == 4)//退出游戏
        {
            exit(0);
        }

    }
}
//主菜单
void Gamemanage::menu(int choice)
{
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
    cout << "*\t\t飞机大战\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t 开始    ";
    if (choice == 1)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t 帮助    ";
    if (choice == 2)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t 设置    ";
    if (choice == 3)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t 退出    ";
    if (choice == 4)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t    制作人:gongliangliang\t*" << endl;
    cout << "*\t    版本:飞机大战1.0\t\t*" << endl;
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
}
//帮助菜单
void Gamemanage::help_menu()
{
    system("cls");//清屏
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t简单难度\t\t*" << endl;
    cout << "*\t   敌人移动速度: 慢\t\t*" << endl;
    cout << "*\t   敌人生成速度: 慢\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t普通难度\t\t*" << endl;
    cout << "*\t   敌人移动速度: 一般\t\t*" << endl;
    cout << "*\t   敌人生成速度: 一般\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t困难难度\t\t*" << endl;
    cout << "*\t   敌人移动速度: 快\t\t*" << endl;
    cout << "*\t   敌人生成速度: 快\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
    system("pause");
}
//设置菜单
void Gamemanage::settings_menu(int choice)
{
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
    cout << "*\t\t《游戏设置》  \t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t 简单模式";
    if (choice == 1)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t 普通模式";
    if (choice == 2)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t 困难模式";
    if (choice == 3)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t 返回  ";
    if (choice == 4)cout << " <<\t\t*" << endl;
    else cout << "  \t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t    当前模式:" << m_idifficulty << "\t\t*" << endl;
    cout << "*\t    制作人:gongliangliang\t*" << endl;
    cout << "*\t    版本:贪吃蛇1.0\t\t*" << endl;
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
}
//开始游戏
void Gamemanage::start_game()
{
    system("cls");                   //清屏
    HideCursor();	                 //隐藏光标
    srand((unsigned int)time(NULL)); //添加随机种子

    Wall wall;
    Airplane airplane(wall);
    Enemy enemy(wall);
    Attack attack(wall);

    wall.drawWall();
    int x = 24, y = 23;      //飞机初始坐标
    airplane.move(x, y);
    bool is_exist = false;
    this->m_iscore = 0;      //使该局游戏分数归零
    int n1 = 0;
    int n2 = 0;
    int n3 = 0;
    int enemy_speed = 0;
    if (m_idifficulty == "简单")
    {
        enemy_speed = 30;
    }
    else if (m_idifficulty == "普通")
    {
        enemy_speed = 10;
    }
    else
    {
        enemy_speed = 5;
    }
    while (!is_exist)
    {
        game_message();
        //飞机运行模块
        if (_kbhit())
        {
            char move = _getch();
            if (move == 13)//enter确认
            {
                pause_game();
            }
            else if (move == 32)//空格攻击
            {
                attack.add_ammo(x + 1, y - 1, "o");
            }
            else if (move == 27)//退出 
            {
                return;
            }
            else//移动 
            {
                airplane.del_move(x, y);
                switch (move)//判断上下移动
                {
                case 'w':y -= 1; if (y < 2)y = 2; break;
                case 'W':y -= 1; if (y < 2)y = 2; break;
                case 's':y += 1; if (y > 23)y = 23; break;
                case 'S':y += 1; if (y > 23)y = 23; break;
                case 'a':x -= 1; if (x < 1)x = 1; break;
                case 'A':x -= 1; if (x < 1)x = 1; break;
                case 'd':x += 1; if (x > 48)x = 48; break;
                case 'D':x += 1; if (x > 48)x = 48; break;
                default:break;
                }
                airplane.move(x, y);
                continue;
            }
        }
        //子弹运行模块
        if (n1 == 0)
        {
            attack.move_ammo();
        }
        n1++;
        if (n1 == 5)
        {
            n1 = 0;
            Sleep(50);
        }
        //敌人运行模块
        if (n2 == 0)
        {
            enemy.move();
            enemy.set_foe();
        }
        n2++;
        if (n2 == enemy_speed)
        {
            n2 = 0;
            Sleep(50);
        }
        //判断存活模块
        is_exist = airplane.is_exist(x, y);
        this->m_iscore += attack.is_crash();
    }
    //游戏结束
    gameover();
}
//游戏信息
void Gamemanage::game_message()
{
    gotoxy(hOut, 52, 0);
    for (int i = 19; i > 0; i--)
    {
        cout << "-";
    }
    gotoxy(hOut, 52, 25);
    for (int i = 19; i > 0; i--)
    {
        cout << "-";
    }
    for (int i = 0; i < 26; i++)
    {
        gotoxy(hOut, 52, i);
        cout << "|";
    }
    for (int i = 0; i < 26; i++)
    {
        gotoxy(hOut, 70, i);
        cout << "|";
    }
    gotoxy(hOut, 56, 3);
    cout << "a(A):向左";
    gotoxy(hOut, 56, 5);
    cout << "d(D):向右";
    gotoxy(hOut, 56, 7);
    cout << "w(W):向上";
    gotoxy(hOut, 56, 9);
    cout << "s(S):向下";
    gotoxy(hOut, 56, 11);
    cout << "Enter: 暂停";
    gotoxy(hOut, 56, 13);
    cout << "ESE:退出";
    gotoxy(hOut, 56, 15);
    cout << "空格:攻击";
    gotoxy(hOut, 56, 17);
    cout << "难度:  " << this->m_idifficulty;
    gotoxy(hOut, 56, 19);
    cout << "分数:  " << this->m_iscore << endl;
}
//暂停游戏
void Gamemanage::pause_game()
{
    gotoxy(hOut, 56, 21);
    cout << "  暂停....." << endl;
    char c = _getch();
    while (c != 13)
    {
        c = _getch();
    }
    gotoxy(hOut, 56, 21);
    cout << "           " << endl;
}
//游戏结束
void Gamemanage::gameover()
{
    system("cls");//清屏
    string str = "Game Over!";
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "*\t\t\t\t\t*" << endl;
    cout << "* * * * * * * * * * * * * * * * * * * * *" << endl;
    gotoxy(hOut, 16, 4);
    for (int i = 0; i < str.size(); i++)
    {
        Sleep(80);
        cout << str[i];
    }
    gotoxy(hOut, 0, 9);
    system("pause");

}
//游戏设置
void Gamemanage::game_settings()
{
    int choice = 1;//游戏设置中的控制选择的变量
    while (true)
    {
        system("cls");//清屏
        Gamemanage::settings_menu(choice);
        //判断是否为回车键(Enter 的ASCII码为 13),确定选择的模式
        int move = _getch();
        if (move != 13)
        {
            switch (move)//判断上下移动
            {
            case 'w':choice -= 1; if (choice < 1)choice = 4; break;
            case 's':choice += 1; if (choice > 4)choice = 1; break;
            case 'W':choice -= 1; if (choice < 1)choice = 4; break;
            case 'S':choice += 1; if (choice > 4)choice = 1; break;
            }
            continue;
        }
        if (choice == 1)
        {
            m_idifficulty = "简单";
        }
        else if (choice == 2)
        {
            m_idifficulty = "普通";
        }
        else if (choice == 3)
        {
            m_idifficulty = "困难";
        }
        else if (choice == 4)
        {
            return;
        }
    }
}
//隐藏光标
void HideCursor()
{

    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };

    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

Wall.h文件

#pragma once
#include<iostream>
using namespace std;

class Wall
{
public:
	enum
	{
		ROW = 26, //行数
		COL = 52  //列数
	};
	Wall();                            //初始化墙壁
	void drawWall();                   //显示墙壁
	void setWall(int x, int y, bool a);//设置二维数组中该位置的符号
	char getWall(int x, int y);        //根据索引来获取当前位置的符号
private:
	char gameArray[26][52];            //背景二维数组
};

Wall.cpp文件

#include<iostream>
#include"wall.h"
using namespace std;

//初始化墙壁
Wall::Wall() //初始化墙壁,用二维数组
{
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			//放墙壁的地方
			if (i == 0 || j == 0 || i == ROW - 1 || j == COL - 1)
			{
				gameArray[i][j] = '*';
			}
			else
			{
				gameArray[i][j] = ' ';
			}
		}
	}
}
//显示墙壁
void Wall::drawWall()
{
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COL; ++j)
		{
			cout << gameArray[i][j];
		}
		cout << endl;
	}
}
//设置二维数组中该位置的符号
void Wall::setWall(int x, int y, bool a)
{
	if (a)
	{
		gameArray[y][x] = '#';
	}
	else
		gameArray[y][x] = ' ';
}
//根据索引来获取当前位置的符号
char Wall::getWall(int x, int y)
{
	return this->gameArray[y][x];
}

Airplane.h文件

#pragma once
#include<iostream>
#include<string>
#include"wall.h"
using namespace std;
class Airplane
{
public:
	Airplane(Wall& tempwall);   //初始化
	void move(int x, int y);    //显示移动后的图像
	void del_move(int x, int y);//删除移动前的图像
	bool is_exist(int x, int y);//判断是否存活
	Wall& wall;
};

Airplane.cpp文件

#include"Airplane.h"
#include"wall.h"
#include<string>
#include<Windows.h>


void gotoxy1(HANDLE hOut1, int x, int y)
{
	COORD pos;
	pos.X = x; //横坐标
	pos.Y = y; //纵坐标
	SetConsoleCursorPosition(hOut1, pos);
}
HANDLE hOut1 = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
//初始化
Airplane::Airplane(Wall& tempwall) :wall(tempwall)
{
	
}
//显示移动后的图像
void Airplane::move(int x, int y)
{
	//该位置为链表中的(x,y)
	//  |
	//  V
	//   #
	//  ###
	gotoxy1(hOut1, x, y);
	cout << " #" << endl;
	gotoxy1(hOut1, x, y + 1);
	cout << "###" << endl;
}
//删除移动前的图像
void Airplane::del_move(int x, int y)
{
	gotoxy1(hOut1, x, y);
	cout << "  " << endl;
	gotoxy1(hOut1, x, y + 1);
	cout << "   " << endl;
}
//判断是否存活
bool Airplane::is_exist(int x, int y)
{
	//飞机只有一种死亡方式:被敌人碰撞所以只要判断是否碰撞即可
	if (wall.getWall(x + 1, y) == '#' || wall.getWall(x, y + 1) == '#' || wall.getWall(x + 2, y + 1) == '#')
	{
		return true;
	}
	else
	{
		return false;
	}
}

Enemy.h文件

#pragma once
#include<iostream>
#include"wall.h"
using namespace std;
class Enemy//敌人
{
public:
	//       敌人外形
	//         ###
	//         ###
	//         ###
	struct Foe
	{
		int x;
		int y;
		struct Foe* next;
	};
	Enemy(Wall& tempwall); //初始化
	void set_foe();        //设置敌人
	void del_foe(Foe* foe);//删除敌人
	void move();           //敌人的移动
	Wall& wall;
private:
	struct Foe* head;
};

Enemy.cpp文件

#include"Enemy.h"
#include"wall.h"
#include<string>
#include<Windows.h>

void gotoxy2(HANDLE hOut2, int x, int y)
{
	COORD pos;
	pos.X = x; //横坐标
	pos.Y = y; //纵坐标
	SetConsoleCursorPosition(hOut2, pos);
}
HANDLE hOut2 = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量
//初始化
Enemy::Enemy(Wall& tempwall) :wall(tempwall)
{
	head = new Foe;
	head->next = NULL;
}
//设置敌机及添加敌机
void Enemy::set_foe()
{
	int x = 0;
	int y = 2;
	while (true)
	{
		x = rand() % 49;
		//如果随机的位置能够生成敌机,就重新生成随机数
		//敌机的(x,y)是指中心位置的'#'
		//    ###
		//    ###
		//    ###
		if (//隔开各个敌机防止判断子弹碰撞时找不到碰撞位置
			wall.getWall(x - 2, y + 1) == ' ' && wall.getWall(x + 2, y + 1) == ' ' &&
			//判断生成的位置是否存在敌机或者与其它敌机重合
			wall.getWall(x - 1, y - 1) == ' ' && wall.getWall(x, y - 1) == ' ' && wall.getWall(x + 1, y - 1) == ' ' &&
			wall.getWall(x - 1, y) == ' ' && wall.getWall(x, y) == ' ' && wall.getWall(x + 1, y + 1) == ' ' &&
			wall.getWall(x - 1, y + 1) == ' ' && wall.getWall(x, y + 1) == ' ' && wall.getWall(x + 1, y + 1) == ' ')
		{
			//把敌机数据添加到背景的二维数值中用来判断敌机是否与子弹碰撞和敌机是否存在
			wall.setWall(x - 1, y - 1, true); wall.setWall(x, y - 1, true); wall.setWall(x + 1, y - 1, true);
			wall.setWall(x - 1, y, true); wall.setWall(x, y, true); wall.setWall(x + 1, y, true);
			wall.setWall(x - 1, y + 1, true); wall.setWall(x, y + 1, true); wall.setWall(x + 1, y + 1, true);
			//创建新结点
			Foe* newfoe = new Foe;
			newfoe->next = NULL;
			newfoe->x = x;
			newfoe->y = y;
			//找到尾节点
			Foe* temp = head;
			if (head->next != NULL)
			{
				while (temp->next != NULL)
				{
					temp = temp->next;
				}
			}
			temp->next = newfoe;
			break;
		}

	}
}
//删除敌机
void Enemy::del_foe(Foe* foe)//传入要删除的节点
{
	Foe* temp1 = NULL;
	Foe* temp2 = NULL;
	Foe* temp3 = NULL;

	while (foe != NULL)
	{
		temp2 = foe;
		foe = foe->next;
		temp3 = foe;
		foe = head;
		while (foe != NULL)
		{
			if (foe->next == temp2)
			{
				temp1 = foe;
				break;
			}
			foe = foe->next;
		}
		break;
		foe = foe->next;
	}
	temp1->next = temp3;
	//删除链表中的敌人数据时同时把背景的二维数组中的敌人数据也删除
	wall.setWall(temp2->x - 1, temp2->y - 1, false); wall.setWall(temp2->x, temp2->y - 1, false); wall.setWall(temp2->x + 1, temp2->y - 1, false);
	wall.setWall(temp2->x - 1, temp2->y, false); wall.setWall(temp2->x, temp2->y, false); wall.setWall(temp2->x + 1, temp2->y, false);
	wall.setWall(temp2->x - 1, temp2->y + 1, false); wall.setWall(temp2->x, temp2->y + 1, false); wall.setWall(temp2->x + 1, temp2->y + 1, false);
	delete temp2;
}
//敌机的移动
void Enemy::move()
{
	Foe* temp = head->next;
	if (head->next != NULL)
	{
		while (temp != NULL)
		{
			//消除敌机移动前屏幕上的图像
			gotoxy2(hOut2, temp->x - 1, temp->y - 1);
			cout << "   " << endl;
			gotoxy2(hOut2, temp->x - 1, temp->y);
			cout << "   " << endl;
			gotoxy2(hOut2, temp->x - 1, temp->y + 1);
			cout << "   " << endl;
			//敌机存储在背景里面数据为空意味着敌机被子弹消灭所以要清楚该节点
			if (wall.getWall(temp->x, temp->y) == ' ')
			{
				Foe* temp1 = temp;
				temp = temp->next;
				del_foe(temp1);
				continue;
			};
			//消除移动前敌机在背景二维数组的信息
			wall.setWall(temp->x - 1, temp->y - 1, false); wall.setWall(temp->x, temp->y - 1, false); wall.setWall(temp->x + 1, temp->y - 1, false);
			wall.setWall(temp->x - 1, temp->y, false); wall.setWall(temp->x, temp->y, false); wall.setWall(temp->x + 1, temp->y, false);
			wall.setWall(temp->x - 1, temp->y + 1, false); wall.setWall(temp->x, temp->y + 1, false); wall.setWall(temp->x + 1, temp->y + 1, false);

			temp->y = temp->y + 1;
			//敌机到下边界自动删除
			if (temp->y == 24)
			{
				Foe* temp1 = temp;
				temp = temp->next;
				del_foe(temp1);
			}
			else
			{
				//在屏幕上显示敌机移动后图像
				gotoxy2(hOut2, temp->x - 1, temp->y - 1);
				cout << "###" << endl;
				gotoxy2(hOut2, temp->x - 1, temp->y);
				cout << "###" << endl;
				gotoxy2(hOut2, temp->x - 1, temp->y + 1);
				cout << "###" << endl;
				//把移动后的敌机数据添加到背景二维数值中
				wall.setWall(temp->x - 1, temp->y - 1, true); wall.setWall(temp->x, temp->y - 1, true); wall.setWall(temp->x + 1, temp->y - 1, true);
				wall.setWall(temp->x - 1, temp->y, true); wall.setWall(temp->x, temp->y, true); wall.setWall(temp->x + 1, temp->y, true);
				wall.setWall(temp->x - 1, temp->y + 1, true); wall.setWall(temp->x, temp->y + 1, true); wall.setWall(temp->x + 1, temp->y + 1, true);

				temp = temp->next;
			}
		}

	}
}

Attack.h文件

#pragma once
#include<iostream>
#include<string.h>
#include"Wall.h"
using namespace std;
class Attack
{
public:
	struct Ammo
	{
		string type;     //弹药类型
		int x;         //弹药横向位置
		int y;         //弹药数值方向位置
		struct Ammo* next;
	};
	Attack(Wall& tempwall);
	//添加弹药
	void add_ammo(int x, int y, string type);
	//删除弹药
	void del_ammo(Ammo* ammo);
	//弹药移动
	void move_ammo();
	//判断弹药是否想碰撞
	int  is_crash();
	Wall& wall;
private:
	struct Ammo* head;      //头指针
};

Attack.cpp文件

#include<iostream>
#include<windows.h>
#include"Attack.h"
using namespace std;
void gotoxy3(HANDLE hOut3, int x, int y)
{
	COORD pos;
	pos.X = x; //横坐标
	pos.Y = y; //纵坐标
	SetConsoleCursorPosition(hOut3, pos);
}
HANDLE hOut3 = GetStdHandle(STD_OUTPUT_HANDLE);//定义显示器句柄变量]
Attack::Attack(Wall& tempwall) :wall(tempwall)
{
	head = new Ammo;
	head->next = NULL;
}
//添加弹药
void Attack::add_ammo(int x, int y, string type)
{
	//创建新结点
	Ammo* newammo = new Ammo;
	newammo->next = NULL;
	newammo->x = x;
	newammo->y = y;
	newammo->type = type;
	//找到尾节点
	Ammo* temp = head;
	if (head->next != NULL)
	{
		while (temp->next != NULL)  //找到尾节点 
		{
			temp = temp->next;
		}
	}
	temp->next = newammo;
}
//删除弹药
void Attack::del_ammo(Ammo* ammo)
{
	Ammo* temp1 = NULL;
	Ammo* temp2 = NULL;
	Ammo* temp3 = NULL;

	while (ammo != NULL)
	{
		temp2 = ammo;
		ammo = ammo->next;
		temp3 = ammo;
		ammo = head;
		while (ammo != NULL)
		{
			if (ammo->next == temp2)
			{
				temp1 = ammo;
				break;
			}
			ammo = ammo->next;
		}
		break;
		ammo = ammo->next;
	}
	temp1->next = temp3;
	delete temp2;
}
//弹药移动
void Attack::move_ammo()
{
	Ammo* temp = head->next;
	if (head->next != NULL)
	{
		while (temp != NULL)
		{
			//消除移动前的图像
			gotoxy3(hOut3, temp->x, temp->y);
			cout << " " << endl;
			temp->y = temp->y - 1;
			if (temp->y == 23 || temp->y == 0)//子弹到边界自动删除
			{
				Ammo* temp1 = temp;
				temp = temp->next;
				del_ammo(temp1);
			}
			else
			{
				gotoxy3(hOut3, temp->x, temp->y);
				cout << temp->type;
				temp = temp->next;
			}
		}

	}
}
//判断弹药是否想碰撞
int Attack::is_crash()
{
	int n = 0;//记录消灭个数
	Ammo* temp = head->next;
	if (head->next != NULL)
	{
		while (temp != NULL)
		{
			//        ###
			//        ###
			//        ###
			//        |
			//子弹碰到敌人该位置
			if (wall.getWall(temp->x, temp->y) == '#' &&
				wall.getWall(temp->x - 1, temp->y) == ' ' &&
				wall.getWall(temp->x + 1, temp->y) == '#')
			{
				//清除该位置敌机在背景二维数组的信息
				wall.setWall(temp->x, temp->y - 2, false); wall.setWall(temp->x + 1, temp->y - 2, false); wall.setWall(temp->x + 2, temp->y - 2, false);
				wall.setWall(temp->x, temp->y - 1, false); wall.setWall(temp->x + 1, temp->y - 1, false); wall.setWall(temp->x + 2, temp->y - 1, false);
				wall.setWall(temp->x, temp->y, false); wall.setWall(temp->x + 1, temp->y, false); wall.setWall(temp->x + 2, temp->y, false);
				Ammo* temp1 = temp;
				temp = temp->next;
				del_ammo(temp1);
				n++;
			}
			//        ###
			//        ###
			//        ###
			//         |
			//子弹碰到敌人该位置
			else if (wall.getWall(temp->x, temp->y) == '#' &&
				wall.getWall(temp->x - 1, temp->y) == '#' &&
				wall.getWall(temp->x + 1, temp->y) == '#')
			{
				//清除该位置敌机在背景二维数组的信息
				wall.setWall(temp->x - 1, temp->y - 2, false); wall.setWall(temp->x, temp->y - 2, false); wall.setWall(temp->x + 1, temp->y - 2, false);
				wall.setWall(temp->x - 1, temp->y - 1, false); wall.setWall(temp->x, temp->y - 1, false); wall.setWall(temp->x + 1, temp->y - 1, false);
				wall.setWall(temp->x - 1, temp->y, false); wall.setWall(temp->x, temp->y, false); wall.setWall(temp->x + 1, temp->y, false);
				Ammo* temp1 = temp;
				temp = temp->next;
				del_ammo(temp1);
				n++;
			}
			//        ###
			//        ###
			//        ###
			//          |
			//子弹碰到敌人该位置
			else if (wall.getWall(temp->x, temp->y) == '#' &&
				wall.getWall(temp->x - 1, temp->y) == '#' &&
				wall.getWall(temp->x + 1, temp->y) == ' ')
			{
				//清除该位置敌机在背景二维数组的信息
				wall.setWall(temp->x - 2, temp->y - 2, false); wall.setWall(temp->x - 1, temp->y - 2, false); wall.setWall(temp->x, temp->y - 2, false);
				wall.setWall(temp->x - 2, temp->y - 1, false); wall.setWall(temp->x - 1, temp->y - 1, false); wall.setWall(temp->x, temp->y - 1, false);
				wall.setWall(temp->x - 2, temp->y, false); wall.setWall(temp->x - 1, temp->y, false); wall.setWall(temp->x, temp->y, false);
				Ammo* temp1 = temp;
				temp = temp->next;
				del_ammo(temp1);
				n++;
			}
			else
				temp = temp->next;
		}

	}
	return n;
}

以下为运行效果图
在这里插入图片描述
在这里插入图片描述

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值