跳到正文

剪贴板

剪贴板 z4kxdtg3

原文
剪贴板信息

剪贴板信息

收录
更新

世纪战争

0.4版本更新

增加灯塔,整理代码增强可读性。

游戏

C++
#include<bits/stdc++.h>
#include<graphics.h>//可视化头文件
#include<windows.h>//系统
#include<conio.h>//键盘
using namespace std;
const int MAXX=1500,MAXY=800,ALLX=1700,ALLY=800;//游戏框(不包含消息框)的xy轴长度
bool together=false;//在一起
int startx=100,starty=100;//玩家的初始位置
int player1huge=13,player1hp=2000;//玩家的大小,血量
int morehp_add=1;//增加的血量
int last_move_time;
int last_hit_time;//上一次攻击的时间
int last_more_army_time;//上一次增兵时间
double hpsub=0.1;//扣除血量占的百分比
int mscnt=0;//消息的个数
int show_time=500;//血量扣除标记的时间留置(单位:毫秒)
string ms[50005];//消息框
struct hp_change{
	int x,y,w,t;
};
queue<hp_change> hp_c;
struct node{//一个角色
	string from;//阵营
	int x,y,allhp,nowhp,huge,speed,attack,defense;//坐标、总血量、现在的血量、大小、速度、攻击力、防御力
	bool kq;int last_kq_time;//开枪技能和上一次开枪的时间
}player1,player2;
vector<node> army;
struct bullet_{
	string from;//阵营
	int x,y,huge,w;//坐标、大小、攻击力
	double addx,addy;//方向
};
queue<bullet_> bullet;
string prin[105];
double get_Dis(double xa,double ya,double xb,double yb){
	return sqrt(1.0*(pow(xa-xb,2))+1.0*(pow(ya-yb,2)));
}
struct tower{
	string from;//阵营
	int x,y,bar1_k,bar1_c,bar2_k,bar2_c,cir1_r,allhp,nowhp;
};
tower red_tower,blue_tower;
void print_tower(tower pr_l){
	if(pr_l.from=="red")setcolor(RED);
	else setcolor(BLUE);
	if(pr_l.from=="red")setfillcolor(RED);
	else setfillcolor(BLUE);
	bar(pr_l.x-0.5*pr_l.bar1_c,pr_l.y-0.5*pr_l.bar1_k,pr_l.x+0.5*pr_l.bar1_c,pr_l.y+0.5*pr_l.bar1_k);
	bar(pr_l.x-0.5*pr_l.bar2_k,pr_l.y+0.5*pr_l.bar1_k,pr_l.x+0.5*pr_l.bar2_k,pr_l.y+0.5*pr_l.bar1_k+pr_l.bar2_c);
	circle(pr_l.x,pr_l.y-pr_l.cir1_r,pr_l.cir1_r);
	setcolor(WHITE);
	setfillcolor(WHITE);//将填充颜色设置成为白色
	bar(pr_l.x-pr_l.cir1_r,pr_l.y-0.5*pr_l.bar1_k-2.0*pr_l.cir1_r-5,pr_l.x+pr_l.cir1_r,pr_l.y-0.5*pr_l.bar1_k-2.0*pr_l.cir1_r-10);
	if(pr_l.from=="red")setcolor(RED);
	else setcolor(BLUE);
	if(pr_l.from=="red")setfillcolor(RED);
	else setfillcolor(BLUE);
	bar(pr_l.x-pr_l.cir1_r,pr_l.y-0.5*pr_l.bar1_k-2.0*pr_l.cir1_r-6,pr_l.x+(pr_l.nowhp*1.0/pr_l.allhp)*pr_l.cir1_r,pr_l.y-0.5*pr_l.bar1_k-2.0*pr_l.cir1_r-9);
}
int get_time(){
	return GetTickCount();
}
double get_area(int xa,int ya,int xb,int yb,int xc,int yc){
	double dis1=get_Dis(xa,ya,xb,yb);
	double dis2=get_Dis(xa,ya,xc,yc);
	double dis3=get_Dis(xb,yb,xc,yc);
	double p=0.5*(dis1+dis2+dis3);
	return sqrt(p*(p-dis1)*(p-dis2)*(p-dis3));
}
bool circle_check(node circle1,node circle2){//判断两个圆是否有重合
	double rdis=sqrt(pow(1.0*(circle1.x-circle2.x),2)+pow(1.0*(circle1.y-circle2.y),2));
	double rsum=circle1.huge+circle2.huge;
	return rdis<=rsum;
}
bool _circle_check(bullet_ circle1,node circle2){
	double rdis=sqrt(pow(1.0*(circle1.x-circle2.x),2)+pow(1.0*(circle1.y-circle2.y),2));
	double rsum=circle1.huge+circle2.huge;
	return rdis<=rsum;
}
bool str_check(int xa,int ya,int xb,int yb,node tow){
	double area=get_area(xa,ya,xb,yb,tow.x,tow.y);
	double h=area*1.0/get_Dis(xa,ya,xb,yb);
	return h<tow.huge;
}
void print(string title,int art_size,string article[105]){
	if(art_size==0){
		int output_x=ALLX*0.5-15*title.size(),output_y=0.4*ALLY;
		setcolor(RED);
		setfont(30,0,"宋体");
		outtextxy(output_x,output_y,title.c_str());
	}
	else{
		int output_x=ALLX*0.5-15*title.size(),output_y=0.2*ALLY;
		setfont(30,0,"宋体");
		outtextxy(output_x,output_y,title.c_str());
		setfont(15,0,"宋体");
		output_y+=30;
		output_x=ALLX*0.3;
		for(int i=1;i<=art_size;i++){
			cout<<article[i].size()<<'\n';
			output_y+=20;
			outtextxy(output_x,output_y,article[i].c_str());
		}
	}
}
void print_a_bullet(bullet_ bull){
	setcolor(BLACK);
	setfillcolor(BROWN);
	fillcircle(bull.x,bull.y,bull.huge);
}
void print_bullet(){
	int run=bullet.size();
	while(run--){
		bullet_ top=bullet.front();
		bullet.pop();
		print_a_bullet(top);
		bullet.push(top);
	}
}
void print_blue(node Print_people){//打印一个蓝队成员
	setcolor(WHITE);
	setfillcolor(BLUE);//将填充颜色设置成为蓝色
	fillcircle(Print_people.x,Print_people.y,Print_people.huge);//画出成员本体
	setfillcolor(WHITE);//将填充颜色设置成为白色
	bar(Print_people.x-Print_people.huge,Print_people.y-Print_people.huge-5,Print_people.x+Print_people.huge,Print_people.y-Print_people.huge-10);//画出总血量
	setfillcolor(BLUE);//将填充颜色设置成为蓝色
	bar(Print_people.x-Print_people.huge,Print_people.y-Print_people.huge-6,Print_people.x-Print_people.huge+(1.0*Print_people.nowhp/Print_people.allhp)*Print_people.huge*2,Print_people.y-Print_people.huge-9);//画出血量
}
void print_red(node Print_people){//打印一个红队成员
	setcolor(WHITE);
	setfillcolor(RED);//将填充颜色设置成为红色
	fillcircle(Print_people.x,Print_people.y,Print_people.huge);//画出成员本体
	setfillcolor(WHITE);//将填充颜色设置成为白色
	bar(Print_people.x-Print_people.huge,Print_people.y-Print_people.huge-5,Print_people.x+Print_people.huge,Print_people.y-Print_people.huge-10);//画出总血量
	setfillcolor(RED);//将填充颜色设置成为红色
	bar(Print_people.x-Print_people.huge,Print_people.y-Print_people.huge-6,Print_people.x-Print_people.huge+(1.0*Print_people.nowhp/Print_people.allhp)*Print_people.huge*2,Print_people.y-Print_people.huge-9);//画出血量
}
string change_int(int in){
	string ret="";
	while(in){
		ret+=(in%10)+'0';
		in/=10;
	}
	for(int i=0;i<ret.size()/2;i++)swap(ret[i],ret[ret.size()-i-1]);
	return ret;
}
void print_hp_c(hp_change pr){
	setcolor(RED);
	setfont(10,0,"宋体");//设置字体
	string output;
	if(pr.w<0){
		output="-";
		pr.w=-pr.w;
	}
	else output="+";
	output+=change_int(pr.w);
	outtextxy(pr.x,pr.y,output.c_str());
}
void print_all_hp_c(){//打印所有的血量扣除标记
	int run=hp_c.size();
	while(run--){
		hp_change top=hp_c.front();
		hp_c.pop();
		if(get_time()-top.t>show_time)continue;
		print_hp_c(top);
		hp_c.push(top);
	}
}
void print_army(){//打印军队
	for(int i=0;i<army.size();i++){
		if(army[i].from=="red")print_red(army[i]);
		if(army[i].from=="blue")print_blue(army[i]);
	}
}
void check_move(node &circle1){//修正角色位置防止出界
	if(circle1.x>MAXX)circle1.x=MAXX;
	if(circle1.y>MAXY)circle1.y=MAXY;
	if(circle1.x<0)circle1.x=0;
	if(circle1.y<0)circle1.y=0;
}
void print_txt(){//打印消息框
	setcolor(WHITE);
	setfont(15,0,"宋体");//设置字体
	outtextxy(MAXX,0,"消息框:");
	int nowx=MAXX,nowy=20;//起始文本的位置
	for(int i=max(1,mscnt-37);i<=mscnt;i++){//打印最后25个消息
		outtextxy(nowx,nowy,ms[i].c_str());
		nowy+=20;//修改下一个文本的位置
	}
}
void print_all_tower(){//打印灯塔
	print_tower(red_tower);
	print_tower(blue_tower);
}
void print(){//打印场景
	cleardevice();//清空画面
	print_all_tower();//打印灯塔
	print_all_hp_c();//打印血量标记
	print_army();//打印军队
	print_blue(player1);//打印玩家
	print_red(player2);//打印玩家
	print_bullet();//打印子弹
	print_txt();//打印消息框
}
void Move(node &circle1,node &circle2){
	int subx=circle1.x-circle2.x,suby=circle1.y-circle2.y,mb=circle1.huge+circle2.huge;
	if(abs(subx)<abs(suby)){
		if(subx>0){
			double movej=1.0*(mb-subx)*(circle1.huge/mb);
			double movei=1.0*mb-movej;
			circle1.x+=movei;
			circle2.x-=movej;
		}
		else{
			double movej=1.0*(mb-(-subx))*(circle1.huge/mb);
			double movei=1.0*mb-movej;
			circle1.x-=movei;
			circle2.x+=movej;
		}
	}
	else{
		if(suby>0){
			double movej=1.0*(mb-suby)*(circle1.huge/mb);
			double movei=1.0*mb-movej;
			circle1.y+=movei;
			circle2.y-=movej;
		}
		else{
			double movej=1.0*(mb-(-suby))*(circle1.huge/mb);
			double movei=1.0*mb-movej;
			circle1.y-=movei;
			circle2.y+=movej;
		}
	}
}
void f(node &circle1,node &circle2){
	if(circle_check(circle1,circle2)){
		Move(circle1,circle2);
		if(circle1.from!=circle2.from){
			int who=rand()%2;
			if(who==0){
				int sub=max(0,circle2.attack-circle1.defense);
				hp_c.push({circle1.x-circle1.huge,circle1.y-circle1.huge,-sub,get_time()});
				circle1.nowhp-=sub;
			}
			else{
				int sub=max(0,circle1.attack-circle2.defense);
				hp_c.push({circle2.x-circle2.huge,circle2.y-circle2.huge,-sub,get_time()});
				circle2.nowhp-=sub;
			}
		}
	}
}
void hit(){//攻击和排挤
	int run=bullet.size();
	while(run--){
		bullet_ top=bullet.front();
		bullet.pop();
		bool clean=false;
		for(int i=0;i<army.size();i++){
			if(top.from!=army[i].from and _circle_check(top,army[i])){
				army[i].nowhp-=top.w;
				hp_c.push({army[i].x-army[i].huge,army[i].y-army[i].huge,-top.w,get_time()});
				clean=true;
				break;
			}
		}
		if(top.from!=player1.from and _circle_check(top,player1)){
			player1.nowhp-=top.w;
			hp_c.push({player1.x-player1.huge,player1.y-player1.huge,-top.w,get_time()});
			clean=true;
		}
		if(top.from!=player2.from and _circle_check(top,player2)){
			player2.nowhp-=top.w;
			hp_c.push({player2.x-player2.huge,player2.y-player2.huge,-top.w,get_time()});
			clean=true;
		}
		if(!clean)bullet.push(top);
	}
	for(int i=0;i<army.size();i++){
		for(int j=0;j<army.size();j++)if(i!=j)f(army[i],army[j]);
		f(army[i],player1);
		f(army[i],player2);
	}
	f(player1,player2);
	for(int i=0;i<army.size();i++){//检查队员
		if(army[i].nowhp<=0){//检查生死
			if(army[i].from=="blue")ms[++mscnt]="蓝队成员被击杀";
			if(army[i].from=="red")ms[++mscnt]="红队成员被击杀";
			army.erase(army.begin()+i);
		}
		check_move(army[i]);//检查出界
	}
}
double get_dis(node circle1,node circle2){
	return get_Dis(circle1.x,circle1.y,circle2.x,circle2.y);
}
void get_close(node &circle1,node circle2,int speed){
	int subx=circle1.x-circle2.x,suby=circle1.y-circle2.y;
	if(abs(subx)>abs(suby)){
		if(subx<0)circle1.x+=speed;
		else circle1.x-=speed;
	}
	else{
		if(suby<0)circle1.y+=speed;
		else circle1.y-=speed;
	}
}
void army_move(){
	for(int i=0;i<army.size();i++){
		node chose={"NULL"};
		int mindis=1e9;
		for(int j=0;j<army.size();j++){
			if(army[i].from!=army[j].from){
				int this_dis=get_dis(army[i],army[j]);
				if(this_dis<mindis){
					mindis=this_dis;
					chose=army[j];
				}
			}
		}
		if(army[i].from==player1.from and player1.nowhp*1.0/player1.allhp<0.3)chose=player1;
		if(army[i].from==player2.from and player2.nowhp*1.0/player2.allhp<0.3)chose=player2;
		if(army[i].from!=player1.from){
			int this_dis=get_dis(army[i],player1);
			if(this_dis<mindis){
				mindis=this_dis;
				chose=player1;
			}
		}
		if(army[i].from!=player2.from){
			int this_dis=get_dis(army[i],player2);
			if(this_dis<mindis){
				mindis=this_dis;
				chose=player2;
			}
		}
		get_close(army[i],chose,army[i].speed);
	}
}
void bullet_move(){
	int run=bullet.size();
	while(run--){
		bullet_ top=bullet.front();
		bullet.pop();
		top.x+=top.addx;
		top.y+=top.addy;
		if(top.x<0 or top.y<0 or top.x>MAXX or top.y>MAXY)continue;
		bullet.push(top);
	}
}
void morehp(){//血量的自然增加
	for(int i=0;i<army.size();i++){
		army[i].nowhp+=morehp_add;
		if(army[i].nowhp>army[i].allhp)army[i].nowhp=army[i].allhp;
	}
}
void end(int winner){//结束
	if(winner==1){
		cleardevice();//清空画面
		setcolor(BLUE);
		setfont(50,0,"宋体");//设置字体
		outtextxy(350,200,"红队队长已被击杀");
		outtextxy(450,300,"游戏结束");
		Sleep(3000);
	}
	else{
		cleardevice();//清空画面
		setcolor(RED);
		setfont(50,0,"宋体");//设置字体
		outtextxy(350,200,"蓝队队长已被击杀");
		outtextxy(450,300,"游戏结束");
		Sleep(3000);
	}
	closegraph();//关闭游戏
}
void kq(node player){
	double mindis=1e9;
	node chose={"NULL"};
	for(int i=0;i<army.size();i++){
		if(player.from!=army[i].from){
			int this_dis=get_dis(army[i],player);
			if(this_dis<mindis){
				mindis=this_dis;
				chose=army[i];
			}
		}
	}
	if(player1.from!=player.from){
		int this_dis=get_dis(player1,player);
		if(this_dis<mindis){
			mindis=this_dis;
			chose=player1;
		}
	}
	if(player2.from!=player.from){
		int this_dis=get_dis(player2,player);
		if(this_dis<mindis){
			mindis=this_dis;
			chose=player2;
		}
	}
	int subx=player.x-chose.x,suby=player.y-chose.y;
	double movex=10.0/(abs(subx)+abs(suby))*abs(subx),movey=10.0/(abs(subx)+abs(suby))*abs(suby);
	if(subx>0)movex=-movex;
	if(suby>0)movey=-movey;
	bullet.push({player.from,player.x,player.y,5,player.attack,movex,movey});
}
int main(){
	initgraph(MAXX+200,MAXY);//开启游戏

	struct _timeb T;
	_ftime(&T);
	srand(T.millitm);//获得毫秒级随机种子
	player1={"blue",startx,starty,2000,2000,13,5,200,20};//初始化玩家
	player2={"red",startx+100,starty+100,2000,2000,13,5,200,20};//初始化玩家
	setbkcolor(GREEN);//设置背景
	last_hit_time=get_time();
	last_move_time=0;

	//测试
	for(int i=1;i<=15;i++){
		army.push_back({"blue",rand()%MAXX,rand()%MAXY,500,500,8,1,200,100,0,0});
		army.push_back({"red",rand()%MAXX,rand()%MAXY,500,500,8,1,200,100,0,0});
	}

	red_tower={"red",100,500,10,20,10,30,15,100,100};
	blue_tower={"blue",300,100,10,20,10,30,15,100,100};
	print();
	while(1){
		if(player1.nowhp<=0)end(2);
		if(player2.nowhp<=0)end(1);
		morehp();
		player1.nowhp+=morehp_add;
		if(player1.nowhp>player1.allhp)player1.nowhp=player1.allhp;
		player2.nowhp+=morehp_add;
		if(player2.nowhp>player2.allhp)player2.nowhp=player2.allhp;

		if(kbhit()){
			char c=getch();
			if(c=='A' or c=='a')player1.x-=player1.speed;
			if(c=='D' or c=='d')player1.x+=player1.speed;
			if(c=='W' or c=='w')player1.y-=player1.speed;
			if(c=='S' or c=='s')player1.y+=player1.speed;
			if(c=='J' or c=='j')player2.x-=player2.speed;
			if(c=='L' or c=='l')player2.x+=player2.speed;
			if(c=='I' or c=='i')player2.y-=player2.speed;
			if(c=='K' or c=='k')player2.y+=player2.speed;
			if(c=='1'){
				if(get_time()-player1.last_kq_time>10000)player1.last_kq_time=get_time();
			}
			if(c=='0'){
				if(get_time()-player2.last_kq_time>10000)player2.last_kq_time=get_time();
			}
		}
		check_move(player1);
		check_move(player2);
		if(get_time()-last_move_time>0){
			army_move();
			last_move_time=get_time();
		}
		bullet_move();
		if(get_time()-last_hit_time>100){
			hit();
			last_hit_time=get_time();
		}
		if(get_time()-player1.last_kq_time<=3000 and get_time()%10==0){
			kq(player1);
		}
		if(get_time()-player2.last_kq_time<=3000 and get_time()%10==0){
			kq(player2);
		}
		print();
		Sleep(10);
	}
	return 0;
}