随机输出字符串的文字

// 随机输出字符串的文字.cpp : 定义控制台应用程序的入口点。
//

// 飞机大战(敌机放子弹).cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <vector>
#include <time.h>
#include <string>
using namespace std;

#define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code)&0x8000?1:0
int _tmain(int argc, _TCHAR* argv[])
{
	//定义飞机头结构体
	struct SPlaneHead
	{
		int nPlaneHeadRow;
		int nPlaneHeadCol;

	};
	//头部初始化
	SPlaneHead sPlaneHead;
	sPlaneHead.nPlaneHeadRow = 12;
	sPlaneHead.nPlaneHeadCol = 8;


	//机身结构体
	struct SPlaneBody
	{
		int nPlaneBodyRow;
		int nPlaneBodyCol;

	};
	//机身初始化
	SPlaneBody arrPlane[3];

	//机身数组
	for (int i = 0; i < 3; i++)
	{
		arrPlane[i].nPlaneBodyRow = 13;
		arrPlane[i].nPlaneBodyCol = 7 + i;
	}

	//子弹结构体
	struct SBullet
	{
		int nBulletRow;
		int nBulletCol;
	};

	//初始化子弹
	//SBullet sBullet[100];

	//用容器初始化子弹,注意数据类型为结构体类型
	vector<SBullet> vecBullet;

	//敌机结构体
	struct SEnemyNode
	{
		int nEnemyNodeRow;
		int nEnemyNodeCol;
	};

	struct SEnemy
	{
		string strPic; //在结构体中定义敌人的值(要区别于字符串的值)
		SEnemyNode arrEnemyNode[4]; //在结构体中定义结构体数组!!
	};

	vector<SEnemy> vecEnemy;
	string arrPic[4] = { "恭", "喜", "发", "财" }; //初始化字符串的值

	//初始化敌机
	/*
	arrEnemyNode[0].nEnemyNodeRow = 1;
	arrEnemyNode[0].nEnemyNodeCol = 7;
	arrEnemyNode[1].nEnemyNodeRow = 1;
	arrEnemyNode[1].nEnemyNodeCol = 8;
	arrEnemyNode[2].nEnemyNodeRow = 1;
	arrEnemyNode[2].nEnemyNodeCol = 9;
	arrEnemyNode[3].nEnemyNodeRow = 2;
	arrEnemyNode[3].nEnemyNodeCol = 8;
	*/


	//int nLen = 1; //子弹数量
	int nDir = 0; //用于控制子弹向上移动
	int nDir1 = 0; //用于控制敌机向下运动
	int nTime = 0; //用于统计帧数
	int nEnemyTime = 0;

	srand(time(nullptr));

	int arrMap[15][20] = {
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
		{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
	};


	while (true)
	{

		nTime++; //每一次循环统计一次次数
		if (nTime >= 5)
		{
			nTime = 0; //归零

			/*
			nLen++;  //每10帧,子弹数量(长度)再增加1

			//设置子弹位置
			for (int i = 0; i < 100; i++)
			{
			//子弹位置为飞机头部的位置
			sBullet[i].nBulletRow = sPlaneHead.nPlaneHeadRow;
			sBullet[i].nBulletCol = sPlaneHead.nPlaneHeadCol;
			}
			*/

			//容器塞入数据
			//压入新子弹
			SBullet sBullet;//实例化
			sBullet.nBulletRow = sPlaneHead.nPlaneHeadRow;
			sBullet.nBulletCol = sPlaneHead.nPlaneHeadCol;

			vecBullet.push_back(sBullet);

		}

		nEnemyTime++;
		if (nEnemyTime>10)
		{
			nEnemyTime = 0;
			//定义敌机的中心坐标,用于敌机的初始化
			int nCenterRow = 1;
			int nCenterCol = rand() % 15 + 2;

			SEnemy enemy;
			int nIndex = rand() % 4; //定义字符串数组的索引,随机一个图片
			enemy.strPic = arrPic[nIndex]; //把字符串的值赋给敌人的值

			enemy.arrEnemyNode[0].nEnemyNodeRow = 1;
			enemy.arrEnemyNode[0].nEnemyNodeCol = nCenterCol - 1;
			enemy.arrEnemyNode[1].nEnemyNodeRow = 1;
			enemy.arrEnemyNode[1].nEnemyNodeCol = nCenterCol;
			enemy.arrEnemyNode[2].nEnemyNodeRow = 1;
			enemy.arrEnemyNode[2].nEnemyNodeCol = nCenterCol + 1;
			enemy.arrEnemyNode[3].nEnemyNodeRow = 2;
			enemy.arrEnemyNode[3].nEnemyNodeCol = nCenterCol;

			vecEnemy.push_back(enemy);

		}

		if (KEY_DOWN(VK_LEFT))
		{
			arrPlane[0].nPlaneBodyCol--;
			arrPlane[1].nPlaneBodyCol--;
			arrPlane[2].nPlaneBodyCol--;
			sPlaneHead.nPlaneHeadCol--;

			/*
			for (int i = 0; i < 100; i++)
			{
			sBullet[i].nBulletCol--;
			}
			*/
		}
		else if (KEY_DOWN(VK_RIGHT))
		{
			arrPlane[0].nPlaneBodyCol++;
			arrPlane[1].nPlaneBodyCol++;
			arrPlane[2].nPlaneBodyCol++;
			sPlaneHead.nPlaneHeadCol++;
		}

		//子弹向上运动
		if (nDir == 0)
		{
			for (int i = 0; i < vecBullet.size(); i++)
			{
				vecBullet[i].nBulletRow--;
			}
		}

		//敌机向下运动
		if (nDir1 == 0)
		{
			for (int i = 0; i < vecEnemy.size(); i++)  //等下要改为vector.size()
			{
				for (int j = 0; j < 4; j++)
				{
					vecEnemy[i].arrEnemyNode[j].nEnemyNodeRow++;
				}

			}
		}

		//不撞墙
		if (1 == arrMap[arrPlane[0].nPlaneBodyRow][arrPlane[0].nPlaneBodyCol])
		{
			arrPlane[0].nPlaneBodyCol++;
			arrPlane[1].nPlaneBodyCol++;
			arrPlane[2].nPlaneBodyCol++;
			sPlaneHead.nPlaneHeadCol++;
		}
		if (1 == arrMap[arrPlane[2].nPlaneBodyRow][arrPlane[2].nPlaneBodyCol])
		{
			arrPlane[0].nPlaneBodyCol--;
			arrPlane[1].nPlaneBodyCol--;
			arrPlane[2].nPlaneBodyCol--;
			sPlaneHead.nPlaneHeadCol--;
		}


		//渲染
		//二维数组的遍历
		for (int i = 0; i < 15; i++) //行数为15
		{
			for (int j = 0; j < 20; j++)//列数为20
			{

				bool DrawPlane = false;
				for (int k = 0; k < 3; k++)
				{
					if (i == arrPlane[k].nPlaneBodyRow && j == arrPlane[k].nPlaneBodyCol)
					{
						DrawPlane = true;
						break;
					}
				}

				/*
				bool DrawBullet = false;
				for (int m = 0; m < 100; m++)
				{
				if (i == sBullet[m].nBulletRow && j == sBullet[m].nBulletCol)
				{
				DrawBullet = true;
				break;
				}
				}
				*/

				bool DrawBullet = false;
				for (int m = 0; m < vecBullet.size(); m++)  //这里不能是100 因为数量是随着帧数变化 这里定义为长度
				{
					if (i == vecBullet[m].nBulletRow && j == vecBullet[m].nBulletCol)
					{
						DrawBullet = true;
						break;
					}
				}

				bool DrawEnemy = false;
				int nEnemyIndex = 0;//定义一个变量用于存放m
				for (int m = 0; m < vecEnemy.size(); m++)
				{
					for (int n = 0; n < 4; n++)
					{
						if (i == vecEnemy[m].arrEnemyNode[n].nEnemyNodeRow && j == vecEnemy[m].arrEnemyNode[n].nEnemyNodeCol)
						{
							DrawEnemy = true;
							nEnemyIndex = m; //把m的值存起来
							break;
						}

					}
				}


				//判断第i行第j列应该画什么
				if (1 == arrMap[i][j])//把数字为1的地方替换为■,作为墙壁
				{
					cout << "■";
				}
				else if (DrawPlane)
				{
					cout << "■";
				}
				else if (i == sPlaneHead.nPlaneHeadRow && j == sPlaneHead.nPlaneHeadCol)
				{
					cout << "■";
				}
				else if (DrawBullet)
				{
					cout << "弹";
				}
				else if (DrawEnemy)
				{
					//cout << "▼";
					cout << vecEnemy[nEnemyIndex].strPic; //打印出随机的字符串值
				}
				else
				{
					cout << "  ";//数字为0的地方设为空格
				}

			}
			cout << endl;//换行,排列成二维数组的形状
		}
		system("cls");

	}
	return 0;
}

上一篇:Centos7安装MySQL(多图)


下一篇:2021-2022学年英语周报八年级第17期答案及试题