C++控制台射击游戏

本人在小学编的游戏,内容分为两个cpp文件,可以把两个文件合到一起,因为代码写得太烂了

就不进行代码解释了,注:还需要添加一个name.txt文件加入两个玩家的名称

直接免费下载网址:【免费】C++控制台射击小游戏(双人版)_c++小游戏代码资源-CSDN文库

tool.cpp

#include<iostream>
#include<string>
#include<windows.h>
#include <vector>
#include <algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
void ycgb(void)
{
			HANDLE				hOut;
	CONSOLE_CURSOR_INFO	curInfo;
	
	hOut=GetStdHandle(STD_OUTPUT_HANDLE); 
	curInfo.dwSize=1;
	curInfo.bVisible=0;
	SetConsoleCursorInfo(hOut,&curInfo);
}
//************************************
void opengame(void)
{
	system("color 0E");
	system("mode con cols=55 lines=25");
	cout<<endl;
	cout<<"╔";
	for(int i=1;i<=25;i++)
	{
		cout<<"═";
	}
	cout<<"╗"<<endl;
	for(int a=1;a<=20;a++)
	{
	cout<<"║";
	if(a!=5&&a!=10&&a!=15)
	{
	  for(int b=1;b<=25*2;b++)
	   {
		   cout<<" ";
	   }
}
if(a==5)
{
	cout<<"                   1.开始游戏"<<"                     ";
}
if(a==10)
{
	cout<<"                   2.游戏设置"<<"                     ";
}
if(a==15)
{
	cout<<"                   3.积分商店"<<"                     ";
}
	cout<<"║"<<endl;
}
cout<<"╚";
	for(int i=1;i<=25;i++)
	{
		cout<<"═";
	}
	cout<<"╝"<<endl;
	} 
	//选择模式 
	void drawchose(void)
{
	system("color 0E");
	system("mode con cols=55 lines=25");
	cout<<endl;
	cout<<"╔";
	for(int i=1;i<=25;i++)
	{
		cout<<"═";
	}
	cout<<"╗"<<endl;
	for(int a=1;a<=20;a++)
	{
	cout<<"║";
	if(a!=5&&a!=10&&a!=15)
	{
	  for(int b=1;b<=25*2;b++)
	   {
		   cout<<" ";
	   }
}
if(a==5)
{
	cout<<"                   1.双人游戏"<<"                     ";
}
if(a==10)
{
	cout<<"                   2.人机对战"<<"                     ";
}
if(a==15)
{
	cout<<"                   3.赏金模式"<<"                     ";
}
	cout<<"║"<<endl;
}
cout<<"╚";
	for(int i=1;i<=25;i++)
	{
		cout<<"═";
	}
	cout<<"╝"<<endl;
	} 
	//重复打印字符串 
void pen1(int a,string b)
{
  int i=0;
  while(true)
   {
   	cout<<b;
   	i++;
   	if(a<=i)
   	{
   		break;
	   }
	   }	
}
void a(HANDLE hout,int x,int y){
    //COORD是WindowsAPI中定义的一种结构,表示一个字符在控制台屏幕上的坐标
    COORD pos;
    pos.X=x;
    pos.Y=y;
    //SetConsoleCursorPosition是API中定位光标位置的函数。
    SetConsoleCursorPosition(hout,pos);
}
//文字颜色 
void color(WORD A)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), A);
} 
void zb(short x, short y) { //定位光标
    COORD coord = { x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
 

,main.cpp

#include<iostream>
#include<string>
#include<iomanip>
#include<ctime>
#include<cstdlib>
#include <windows.h>
#include <conio.h>
#include <vector>
#include <algorithm>
#include<fstream>
#define yellow FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define blue FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define orange FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define pulple FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define red  FOREGROUND_RED | FOREGROUND_INTENSITY
#define green FOREGROUND_GREEN | FOREGROUND_GREEN | FOREGROUND_INTENSITY
void ycgb(void);
void opengame(void);
void zb(short x, short y);
void color(WORD A);
void shop(void);
void xs(void);
void startGame(void);
void xue_dan(void);
void drawchose(void);
void renji(void);
int sui_ji(int a,int b);
	char shuru1=_getch(); 
int x=1,y=1,x2=43,y2=22;//设置玩家方位
int xue=10,fen,xue2=10,fen2;//血量;得分
int dan1=10,dan2=10,ju1=5,ju2=5;
int jinbi,jinbi2;
int shang1=1,shang2=1;
int i;//计数器 
int wa1,wa2;
using namespace std;
int main()
{
	startGame();
		return 0;
}
	void shop(void)
	{
	ifstream shop;
	string shop2;
	shop.open("shop.cheng");
	while(getline(shop,shop2))
	{
		i++; 
		if(i==2||i==3)
		{
			if(i==2)
			{
				cout<<shop2<<jinbi<<"                             *"<<endl;
			}
			if(i==3)
			{
				cout<<shop2<<jinbi2<<"                             *"<<endl;	
			}
		}
		else cout<<shop2<<endl;
	}
}
void xue_dan(void)
{
	ifstream nametxt; 
	string w1,w2;
	nametxt.open("name.txt");
	nametxt>>w1>>w2;
	int shu=0; 
	zb(60,3);
	printf("⊕射击游戏⊕");
//w1血量 
	zb(50,5);
	printf("┏━━━━━━━━━━━━━┓");
	zb(51,6);
	printf("                          ");
	zb(50,6);
	i=0;
	printf("┃");
	for(;;)
	{
		if(i>=xue)
		{
			break;
		}
		printf("■");
		i++;
 	} 
 	zb(78,6);
 	printf("┃");
 	zb(50,7);
 	printf("┗━━━━━━━━━━━━━┛");
 	zb(45,7);
 	printf("*");
//w2血量 
 	zb(50,9);
	printf("┏━━━━━━━━━━━━━┓");
	zb(51,10);
	printf("                          ");
	zb(50,10);
	i=0;
	printf("┃");
	for(;;)
	{
		if(i>=xue2)
		{
			break;
		}
		printf("■");
		i++;
 	} 
 	zb(78,10);
 	printf("┃");
 	zb(50,11);
 	printf("┗━━━━━━━━━━━━━┛");
 	zb(50,13);
 	cout<<"                             ";
 	zb(50,13);
	cout<<w1<<"子弹:"<<dan1;
	zb(50,15);
 	cout<<"                             ";
 	zb(50,15);
	cout<<w2<<"子弹:"<<dan2;
 	for(;;)
   {
   	 zb(0,shu);
   	 cout<<"*";
   	 zb(41*2,shu);
   	 cout<<"*";
   	 zb(45,shu);
   	 cout<<"*";
   	 shu++;
   	 if(shu==23)
   	 {
   	 	break;
		}
   }
} 
void heng(string a1,int a2) 
{
	for(int i;i<=a2;i++)
	{
		cout<<a1;
	}
	}
void xs(void)
{
   string fan[50][50];
   string w1,w2;
   int xding=0,yding=1;
   int shu=0;
   for(;;)
   {
   	 zb(0,shu);
   	 cout<<"*";
   	 zb(41*2,shu);
   	 cout<<"*";
   	 zb(45,shu);
   	 cout<<"*";
   	 shu++;
   	 if(shu==23)
   	 {
   	 	break;
		}
   }
   zb(0,0);
   printf("**********************************************************************************");
    color(blue);
   zb(8,4);
   printf("■");
   zb(35,4);
   printf("■");
   zb(8,19);
   printf("■");
   zb(35,19);
   printf("■");
   zb(22,9);
   printf("■");
   zb(22,13);
   printf("■");
   zb(18,11);
   printf("■");
   zb(26,11);
   printf("■"); 
   zb(22,11);
   color(yellow);
   printf("◎");
   int jishu10;
  if(x==x2||y==y2)
{
	if(x-x2<=ju2||x2-x<=ju2)
	{
	   color(pulple);
	}
   else
   {
   	color(green);
   }
}
 zb(x,y);
 if(wa1==1)
 {
 	color(red);
 	wa1=0;
 }
 printf("♂");
  if(x==x2||y==y2)
{
	if(x-x2<=ju1||x2-x<=ju1)
	{
	   color(pulple);
	}
   else
   {
   	color(green);
   }
}
 zb(x2,y2);
 if(wa2==1)
 {
 	color(red);
 	wa2=0;
 }
 printf("♀");
 zb(x,y-1);
 printf(" ");
 zb(x,y+1);
 printf(" ");
 zb(x2,y2-1);
 printf(" ");
 zb(x2,y2+1);
 printf(" ");
 color(green);
 zb(0,23);
printf("***********************************************************************************");
xue_dan();//插入目录 
if(xue<=0)
{
	zb(0,20);
	cout<<endl; 
	cout<<w1<<"挂了!O(∩_∩)O哈哈~";
	while(true)
	{
		x=1;
		y=1;
		x2=1;
		y2=1;
		Sleep(1000);
		break;
	}
	system("cls");
	cout<<"你是否要继续游戏?"<<endl;
	cout<<"(1)Yes   (2)No"<<endl;
	int shuru1;
	cin>>shuru1;
	if(shuru1==1)
	{
		 x=1;
		 y=1;
		 x2=43;
		 y2=22;
		 xue=10;
		 xue2=10;
		 dan1=10;
		 dan2=10;
		 shang1=1;
		 shang2=1;
		startGame();
	 } 
	 else
	 {
	 		cout<<"Bye!";
	 		break;
	 }
}
if(xue2<=0)
{
	cout<<endl;
	cout<<w2<<"挂了!( ⊙ o ⊙ )!";
		while(true)
	{
		x=1;
		y=1;
		x2=1;
		y2=1;
		dan1=10;
		dan2=10;
		shang1=1;
		shang2=1;
		Sleep(1000);
		break;
	}
	system("cls");
	cout<<"你是否要继续游戏?"<<endl;
	cout<<"(1)Yes   (2)No"<<endl;
	int shuru2;
	cin>>shuru2;
	if(shuru2==1)
	{
		x=1;
		 y=1;
		 x2=43;
		 y2=22;
		 xue=10;
		 xue2=10;
		startGame();
	 } 
	 else
	 {
	 	cout<<"Bye!";
	 	break;
	 }
}
	if(x==22&&y==11)
	{
		srand(time(0));
		int sui=rand()%5+1;
		if(sui<5)
		{
		dan1+=10;
	    }
	    if(sui==5)
	    {
	    	if(xue<10)
	    	{
	    	   xue=10;
	        }
		}
	}
	if(x2==22&&y2==11)
	{
	    srand(time(0));
		int sui2=rand()%5+1;
		if(sui2<5)
		{
		dan2+=10;
	    }
	    if(sui2==5)
	    {
	    	if(xue2<10)
	    	{
	    	   xue2=10;
	        }
		}
    }
    if(x==22&&y==13)
    {
    	x=1;
    	y=1;
	}
	if(x==18&&y==11)
    {
    	x=1;
    	y=1;
	}
	if(x==26&&y==11)
    {
    	x=1;
    	y=1;
	}
	if(x==22&&y==11)
    {
    	x=1;
    	y=1;
	}
	if(x2==22&&y2==13)
    {
    	x2=43;
    	y2=22;
	}
	if(x2==18&&y2==11)
    {
    	x2=43;
    	y2=22;
	}
	if(x2==26&&y2==11)
    {
    	x2=43;
    	y2=22;
	}
	if(x2==22&&y2==11)
    {
    	x2=43;
    	y2=22;
	}
}
  void startGame(void)
  {
  	opengame();
	char shuru1;
	cin>>shuru1;
	system("cls");
if(shuru1=='1')
{  
system("cls");
    char shuru2;
	drawchose();
	cin>>shuru2;
	if(shuru2=='1')
	{ 
    system("mode con cols=85 lines=25");  
	while(true)
	{
		ycgb();
		color(green);
		xs();
		char shuru=_getch();
		x2++;
		y2++;
			if(shuru=='s')
			{
				if(y<22)
				{
					y++;
				}
			}
			if(shuru=='w')
			{
				if(y>1)
				{
					y--;
				}
			}
			if(shuru=='a')
			{
				if(x>1)
				{
					x--;
				}
			}
			if(shuru=='d')
			{
				if(x<43)
				{
					x++;
				}
			}
			//
			if(shuru=='k'||shuru=='5')
			{
				if(y2<22)
				{
					y2++;
				}
			}
			if(shuru=='i'||shuru=='8')
			{
				if(y2>1)
				{
					y2--;
				}
			}
			if(shuru=='j'||shuru=='4')
			{
				if(x2>1)
				{
					x2--;
				}
			}
			if(shuru=='l'||shuru=='6')
			{
				if(x2<43)
				{
					x2++;
				}
			}
			if(x2==x&&y2==y)
			{
			  x2--;
			  y2--;
			  x--;
			  y--;	
			}
			if(shuru=='g')
			{
				if(x==x2||y==y2)
				{
					if(x-x2<=ju1||x2-x<=ju1)
					{
						if(dan1!=0)
						 {
					xue2=xue2-shang1;
					 fen++;
					dan1--;
					wa2=1;
				        }
				     }
				}
				if(dan1!=0)
				{
					dan1--;
				}
			}
				if(shuru==' '||shuru=='+')
			{
					if(x==x2||y==y2)
				{
					if(x-x2<=ju2||x2-x<=ju2)
					{
						if(dan2!=0)
						{
					xue=xue-shang2;
					fen2++;
					wa1=1;
				        }
				     }
				}
				if(dan2!=0)
				{
				dan2--;
			}
			}
		} 
	}
	if(shuru2=='2')
	{
	cout<<"请输入玩家 (1)";
	ifstream nametxt;
	string w1,w2;
	char shuru3;
	nametxt.open("name.txt");
	nametxt>>w1>>w2;
	cout<<w1<<"   (2)"<<w2<<endl;
	cin>>shuru3;
	if(shuru3=='2')
	{
	w2=w1;
    system("mode con cols=85 lines=25");  
	while(true)
	{
		ycgb();
		color(green);
		xs();
		int time=clock();
		int buxing;
		int zhuangtai;
		char shuru=_getch();
			if(shuru=='s')
			{
				if(y<22)
				{
					y++;
				}
			}
			if(shuru=='w')
			{
				if(y>1)
				{
					y--;
				}
			}
			if(shuru=='a')
			{
				if(x>1)
				{
					x--;
				}
			}
			if(shuru=='d')
			{
				if(x<43)
				{
					x++;
				}
			}
			//
		
			if(x2==x&&y2==y)
			{
			  x2--;
			  y2--;
			  x--;
			  y--;	
			}
			if(shuru=='k')
			{
				if(x==x2||y==y2)
				{
					if(x-x2<=ju1||x2-x<=ju1)
					{
						if(dan1!=0)
						 {
					xue2=xue2-shang1;
					 fen++;
					dan1--;
					wa2=1;
				        }
				     }
				}
				if(dan1!=0)
				{
					dan1--;
				}
			}
			while(true)
			{
			y2--;
			x2--;
		}
	int time,,cishu,panduan;
	int time2,cishu2,panduan2;
	int suiji,shoot,suiji2;

			if(x==x2||y==y2)
			{
				if(time2!=cishu)
        		{
        			cishu2=time2;
        			panduan2=0;
				}
				time2=clock();
				if(cishu2==time2)
				{
					panduan2=1;//rand()%5+1
				}
				shoot=rand()%500+700;
				if(panduan2==0)
				{
					xue=xue-3;
					dan2--;
				}
			}
			if(x!=x2&&y!=y2)
			{
				if(xue2>=3)
				{
					suiji=rand()%2+1;
					if(suiji==1)
					{
					    if(x>x2)
					    {
					    	x2++;
						}
						if(x<x2)
						{
							x2--;
						}
					}
					if(suiji==2)
					{
					    if(y>y2)
					    {
					    	y2++
						}
						if(y<y2)
						{
							y2--
						}
					}
				}
				if(xue<3)
				{
					suiji2=rand()%2+1;
					if(suiji2==1)
					{
					   y++;
					   x++;	
					}
					if(suiji2==2)
					{
						suiji=rand()%2+1;
					if(suiji==1)
					{
					    if(x>x2)
					    {
					    	x2++;
						}
						if(x<x2)
						{
							x2--;
						}
					}
					if(suiji==2)
					{
					    if(y>y2)
					    {
					    	y2++
						}
						if(y<y2)
						{
							y2--
						}
					}
					}
				}
			}
		}
		}
	if(shuru2=='3')
	{
		cout<<"本模式暂无开发,敬请期待。";
		Sleep(1000);
		system("cls");
		startGame(); 
	}
}
}
	if(shuru1=='2')
	{
		char shuru2;
        cout<<"           版本:Cazy Game 2.0普通版"<<endl;	
        cout<<endl;
	    cout<<"           作者:CYF_001   ( )"<<endl;
	    cout<<endl;
		cout<<"           游戏规则:此游戏为双人游戏,玩家1控制键"<<endl;
		cout<<"           为(W)上(S)下(A)左(D)右,玩家2控制键 为(I)"<<endl;
        cout<<"           上(K)下(J)左(L)右"<<endl;
        cout<<"-----------------返回按(t)键------------------------"<<endl;
        cin>>shuru2;
		if(shuru2=='t')
	{
		system("cls");
		startGame();
	}
}
if(shuru1=='3')
{
	ifstream txt1,txt2;
	system("cls");
	txt1.open("coin1.cheng");
	txt1>>jinbi;
	txt2.open("coin2.cheng");
	txt2>>jinbi2;
	char shopshu1,shopshu2;
while(true)
{
shop();
cin>>shopshu1>>shopshu2;
if(shopshu1=='d')
{
  if(shopshu2=='1')
  {
  	if(jinbi>=5)
  	{
  	ju1=5;
  	dan1=50;
  	jinbi=jinbi-5;
  }
	   } 
  if(shopshu2=='2')
  {
  	if(jinbi>=7)
  	{
  	ju1=10;
  	shang1=2;
  	dan1=12;
  	jinbi=jinbi-7;
  }
	  }
  if(shopshu2=='3')
  {
  	if(jinbi>=10)
  	{
  	ju1=20;
  	shang1=5;
  	dan1=3;
  	jinbi=jinbi-10;
  }
	  }	
	  if(shopshu2=='4')
	  {
	  	if(jinbi>=2)
	  	{
	  	xue=13;
	  	jinbi=jinbi-2;
	  }
	  }
}
if(shopshu1=='h')
{
  if(shopshu2=='1')
  {
  	if(jinbi2>=5)
  	{
  	ju2=5;
  	dan2=50;
  		jinbi2=jinbi2-5;
  }
	   } 
  if(shopshu2=='2')
  {
  	if(jinbi2>=7)
  	{
  	ju2=10;
  	shang2=2;
  	dan2=12;
  	jinbi2=jinbi2-7;
  }
	  }
  if(shopshu2=='3')
  {
  	if(jinbi2>=10)
  	{
  	ju2=20;
  	shang2=5;
  	dan2=3;
  	jinbi2=jinbi2-10;
  }
	  }	
	  if(shopshu2=='4')
	  {
	  	if(jinbi2>=2)
	  	{
	  	xue2=13;
	  	jinbi2=jinbi2-2;
	  }
	  }
}
if(shopshu1=='t')
{
	system("cls");
	startGame();
	break;
}
system("cls");
}
} 
else{
	system("cls");
	startGame();
}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C++控制台射击游戏: ```cpp #include <iostream> #include <conio.h> #include <Windows.h> using namespace std; const int WIDTH = 50; // 游戏窗口宽度 const int HEIGHT = 20; // 游戏窗口高度 int playerX = WIDTH / 2; // 玩家初始位置 int playerY = HEIGHT - 2; int bulletX = -1; // 子弹位置 int bulletY = -1; int enemyX = rand() % (WIDTH - 2) + 1; // 敌人初始位置 int enemyY = 1; int 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 || j == WIDTH - 1) cout << "#"; else if (i == playerY && j == playerX) cout << "A"; // 绘制玩家 else if (i == enemyY && j == enemyX) cout << "E"; // 绘制敌人 else if (i == bulletY && j == bulletX) cout << "*"; // 绘制子弹 else cout << " "; } cout << endl; } // 绘制分数 cout << "Score: " << score << endl; // 绘制游戏窗口边框 for (int i = 0; i < WIDTH + 2; i++) cout << "#"; cout << endl; } void update() // 更新游戏状态 { // 更新子弹位置 if (bulletY >= 0) { bulletY--; if (bulletY == enemyY && bulletX == enemyX) { // 判断是否命中敌人 score++; enemyX = rand() % (WIDTH - 2) + 1; enemyY = 1; bulletX = -1; bulletY = -1; } else if (bulletY == 0) { // 子弹飞出屏幕 bulletX = -1; bulletY = -1; } } // 更新敌人位置 enemyY++; if (enemyY >= HEIGHT - 1) { // 敌人到达底部 enemyX = rand() % (WIDTH - 2) + 1; enemyY = 1; score--; } // 检测玩家输入 if (_kbhit()) { char c = _getch(); if (c == 'a' && playerX > 1) playerX--; if (c == 'd' && playerX < WIDTH - 2) playerX++; if (c == ' ') bulletX = playerX; bulletY = playerY - 1; } } int main() { while (true) { draw(); update(); Sleep(50); // 休眠一段时间,控制游戏速度 } return 0; } ``` 这是一个非常简单的示例,只有一个敌人和一个子弹,但你可以根据需要添加更多的元素来增强游戏的复杂性和乐趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值