世纪战争
初始0.1版本
最老版本,无更新。
游戏
#include<bits/stdc++.h>
#include<graphics.h>//可视化头文件
#include<windows.h>//系统
#include<conio.h>//键盘
using namespace std;
const int MAXX=1000,MAXY=600,ALLX=1200,ALLY=600;//游戏框(不包含消息框)的xy轴长度
bool together=false;//在一起
int startx=100,starty=100;//玩家的初始位置
int player1huge=20,player1hp=2000;//玩家的大小,血量
int morehp_add=1;//增加的血量
int player1_move=10;//玩家的移动速度
int player2_move=10;//玩家的移动速度
int last_together_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{//一个角色
int x,y,allhp,nowhp,huge;//坐标、总血量、现在的血量、大小
}player1,player2;
vector<node> blue_army,red_army;//红军和蓝军
int get_time(){
return GetTickCount();
}
struct bullet_{
int x,y,w;
double addx,addy;
};
queue<bullet_> bullet;
string prin[105];
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_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_blue_army(){//打印蓝军
for(int i=0;i<blue_army.size();i++)print_blue(blue_army[i]);
}
void print_red_army(){//打印红军
for(int i=0;i<red_army.size();i++)print_red(red_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(1000,0,"消息框:");
int nowx=1000,nowy=20;//起始文本的位置
for(int i=max(1,mscnt-25);i<=mscnt;i++){//打印最后25个消息
outtextxy(nowx,nowy,ms[i].c_str());
nowy+=20;//修改下一个文本的位置
}
}
void print(){//打印场景
cleardevice();//清空画面
print_all_hp_c();//打印血量标记
print_red_army();//打印红军
print_blue_army();//打印蓝军
print_blue(player1);//打印玩家
print_red(player2);//打印玩家
print_txt();//打印消息框
}
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;
}
void hit(){//攻击和排挤
for(int i=0;i<blue_army.size();i++){//玩家1与蓝队队员的排挤
node circle1=player1,circle2=blue_army[i];
if(circle_check(circle1,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;
}
}
}
player1=circle1,blue_army[i]=circle2;
}
for(int i=0;i<red_army.size();i++){//玩家1与红队队员的攻击与排挤
node circle1=player1,circle2=red_army[i];
if(circle_check(circle1,circle2)){
int who=rand()%2;
if(who==0){
circle1.nowhp-=circle2.allhp*hpsub;
ms[++mscnt]="蓝队玩家受伤了";
hp_c.push({circle1.x-circle1.huge,circle1.y-circle1.huge,-circle2.allhp*hpsub,get_time()});
}
else{
circle2.nowhp-=circle1.allhp*hpsub;
ms[++mscnt]="红队玩家受伤了";
hp_c.push({circle2.x-circle2.huge,circle2.y-circle2.huge,-circle1.allhp*hpsub,get_time()});
}
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;
}
}
}
player1=circle1,red_army[i]=circle2;
}
for(int i=0;i<blue_army.size();i++){
for(int j=0;j<red_army.size();j++){//蓝队成员和红队成员的攻击与排挤
node circle1=blue_army[i],circle2=red_army[j];
if(circle_check(circle1,circle2)){
int who=rand()%2;
if(who==0){
circle1.nowhp-=circle2.allhp*hpsub;
ms[++mscnt]="蓝队玩家受伤了";
hp_c.push({circle1.x-circle1.huge,circle1.y-circle1.huge,-circle2.allhp*hpsub,get_time()});
}
else{
circle2.nowhp-=circle1.allhp*hpsub;
ms[++mscnt]="红队玩家受伤了";
hp_c.push({circle2.x-circle2.huge,circle2.y-circle2.huge,-circle1.allhp*hpsub,get_time()});
}
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;
}
}
}
blue_army[i]=circle1,red_army[j]=circle2;
}
}
for(int i=0;i<blue_army.size();i++){
for(int j=0;j<blue_army.size();j++){//蓝队成员和蓝队成员的排挤
node circle1=blue_army[i],circle2=blue_army[j];
if(circle_check(circle1,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;
}
}
}
blue_army[i]=circle1,blue_army[j]=circle2;
}
}
for(int i=0;i<red_army.size();i++){
for(int j=0;j<red_army.size();j++){//红队成员和红队成员的排挤
node circle1=red_army[i],circle2=red_army[j];
if(circle_check(circle1,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;
}
}
}
red_army[i]=circle1,red_army[j]=circle2;
}
}
for(int i=0;i<blue_army.size();i++){//玩家2与蓝队成员的攻击与排挤
node circle1=player2,circle2=blue_army[i];
if(circle_check(circle1,circle2)){
int who=rand()%2;
if(who==0){
circle1.nowhp-=circle2.allhp*hpsub;
ms[++mscnt]="玩家2受伤了";
hp_c.push({circle1.x-circle1.huge,circle1.y-circle1.huge,-circle2.allhp*hpsub,get_time()});
}
else{
circle2.nowhp-=circle1.allhp*hpsub;
ms[++mscnt]="蓝队玩家受伤了";
hp_c.push({circle2.x-circle2.huge,circle2.y-circle2.huge,-circle1.allhp*hpsub,get_time()});
}
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;
}
}
}
player2=circle1,blue_army[i]=circle2;
}
for(int i=0;i<red_army.size();i++){//玩家2与红队成员的排挤
node circle1=player2,circle2=red_army[i];
if(circle_check(circle1,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;
}
}
}
player2=circle1,red_army[i]=circle2;
}
if(true){
node circle1=player1,circle2=player2;
if(circle_check(circle1,circle2)){
int who=rand()%2;
if(who==0){
circle1.nowhp-=circle2.allhp*hpsub;
ms[++mscnt]="玩家1受伤了";
hp_c.push({circle1.x-circle1.huge,circle1.y-circle1.huge,-circle2.allhp*hpsub,get_time()});
}
else{
circle2.nowhp-=circle1.allhp*hpsub;
ms[++mscnt]="玩家2受伤了";
hp_c.push({circle2.x-circle2.huge,circle2.y-circle2.huge,-circle1.allhp*hpsub,get_time()});
}
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;
}
}
}
player1=circle1,player2=circle2;
}
for(int i=0;i<blue_army.size();i++){//检查蓝队成员
if(blue_army[i].nowhp<=0){//检查生死
ms[++mscnt]="蓝队成员被击杀";
blue_army.erase(blue_army.begin()+i);
}
check_move(blue_army[i]);//检查出界
}
for(int i=0;i<red_army.size();i++){//检查红队成员
if(red_army[i].nowhp<=0){//检查生死
ms[++mscnt]="红队成员被击杀";
red_army.erase(red_army.begin()+i);
}
check_move(red_army[i]);//检查出界
}
}
double get_dis(node circle1,node circle2){
return sqrt(pow(1.0*(circle1.x-circle2.x),2)+pow(1.0*(circle1.y-circle2.y),2));
}
void get_close(node &circle1,node circle2,bool fast){
int subx=circle1.x-circle2.x,suby=circle1.y-circle2.y;
if(abs(subx)>abs(suby)){
if(subx<0){
circle1.x++;
if(fast)circle1.x++;
}
else{
circle1.x--;
if(fast)circle1.x--;
}
}
else{
if(suby<0){
circle1.y++;
if(fast)circle1.y++;
}
else{
circle1.y--;
if(fast)circle1.y--;
}
}
}
void army_move(){
for(int i=0;i<blue_army.size();i++){//蓝队成员的移动
node chose={-1,-1,-1,-1,-1};
if(player1.nowhp*1.0/player1.allhp<0.3){
chose=player1;//玩家的紧急救援
get_close(blue_army[i],player1,true);
continue;
}
else{
double mindis=1e9;
for(int j=0;j<red_army.size();j++){
double thisdis=1.0*get_dis(blue_army[i],red_army[j]);
if(thisdis<mindis){
chose=red_army[j];
mindis=thisdis;
}
}
double player2dis=1.0*get_dis(blue_army[i],player2);
if(player2dis<mindis){
chose=player2;
mindis=player2dis;
}
}
if(chose.huge==-1){//未找到目标
blue_army[i].x+=rand()%2;
blue_army[i].y+=rand()%2;
}
else get_close(blue_army[i],chose,false);
}
for(int i=0;i<red_army.size();i++){//红队成员的移动
node chose={-1,-1,-1,-1,-1};
if(player2.nowhp*1.0/player2.allhp<0.3){
chose=player2;//玩家的紧急救援
get_close(red_army[i],player2,true);
}
else{
double mindis=1e9;
for(int j=0;j<blue_army.size();j++){
double thisdis=1.0*get_dis(blue_army[j],red_army[i]);
if(thisdis<mindis){
chose=blue_army[j];
mindis=thisdis;
}
}
double player1dis=1.0*get_dis(red_army[i],player1);
if(player1dis<mindis){
chose=player1;
mindis=player1dis;
}
}
if(chose.huge==-1){//未找到目标
red_army[i].x+=rand()%2;
red_army[i].y+=rand()%2;
}
else get_close(red_army[i],chose,false);
}
}
void morehp(){//血量的自然增加
for(int i=0;i<blue_army.size();i++){//蓝队血量自然增加
int lasthp=blue_army[i].nowhp;
blue_army[i].nowhp+=morehp_add;
if(blue_army[i].nowhp>blue_army[i].allhp)blue_army[i].nowhp=blue_army[i].allhp;
// hp_c.push({blue_army[i].x-blue_army[i].huge,blue_army[i].y-blue_army[i].huge,blue_army[i].nowhp-lasthp,get_time()});
}
for(int i=0;i<red_army.size();i++){//红队血量自然增加
int lasthp=red_army[i].nowhp;
red_army[i].nowhp+=morehp_add;
if(red_army[i].nowhp>red_army[i].allhp)red_army[i].nowhp=red_army[i].allhp;
// hp_c.push({red_army[i].x-red_army[i].huge,red_army[i].y-red_army[i].huge,red_army[i].nowhp-lasthp,get_time()});
}
}
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 more_army(){
blue_army.push_back({rand()%MAXX,rand()%MAXY,500,500,10});
red_army.push_back({rand()%MAXX,rand()%MAXY,500,500,10});
}
int main(){
initgraph(MAXX+200,MAXY);//开启游戏
struct _timeb T;
_ftime(&T);
srand(T.millitm);//获得毫秒级随机种子
player1={startx,starty,player1hp,player1hp,player1huge};//初始化玩家
player2={startx+100,starty+100,player1hp,player1hp,player1huge};//初始化玩家
setbkcolor(GREEN);//设置背景
last_hit_time=get_time();
last_together_time=get_time();
last_more_army_time=get_time();
//测试
for(int i=1;i<=15;i++){
blue_army.push_back({rand()%MAXX,rand()%MAXY,500,500,10});
}
for(int i=1;i<=15;i++){
red_army.push_back({rand()%MAXX,rand()%MAXY,500,500,10});
}
print();
while(1){
if(player1.nowhp<=0)end(2);
if(player2.nowhp<=0)end(1);
morehp();
int lasthp=player1.nowhp;
player1.nowhp+=morehp_add;
if(player1.nowhp>player1.allhp)player1.nowhp=player1.allhp;
// hp_c.push({player1.x-player1.huge,player1.y-player1.huge,player1.nowhp-lasthp,get_time()});
lasthp=player2.nowhp;
player2.nowhp+=morehp_add;
if(player2.nowhp>player2.allhp)player2.nowhp=player2.allhp;
// hp_c.push({player2.x-player2.huge,player2.y-player2.huge,player2.nowhp-lasthp,get_time()});
if(kbhit()){
char c=getch();
if(c=='A' or c=='a')player1.x-=player1_move;
if(c=='D' or c=='d')player1.x+=player1_move;
if(c=='W' or c=='w')player1.y-=player1_move;
if(c=='S' or c=='s')player1.y+=player1_move;
if(c=='J' or c=='j')player2.x-=player2_move;
if(c=='L' or c=='l')player2.x+=player2_move;
if(c=='I' or c=='i')player2.y-=player2_move;
if(c=='K' or c=='k')player2.y+=player2_move;
}
check_move(player1);
check_move(player2);
army_move();
if(get_time()-last_hit_time>100){
hit();
last_hit_time=get_time();
}
if(get_time()-last_more_army_time>3000){
more_army();
last_more_army_time=get_time();
}
print();
Sleep(10);
}
return 0;
}