C++实现Kingdomrush小游戏

👩‍💻博客主页:沾花把玖的博客主页

✨欢迎关注🖱点赞🎀收藏⭐留言✒

🔮本文由沾花把玖原创,csdn首发!

😘系列专栏:C++游戏制作_沾花把玖的博客

🎨你的收入跟你的不可替代成正比

🀄如果觉得博主的文章还不错的话,请三连支持一下博主哦

Kingdomrush为本人独立编写的一款C++小游戏,以战斗为主题,效果尚未成熟,小编还会继续改进。

Kingdomrush:本为乌拉圭Ironhide Game Studio游戏公司出品的游戏《王国保卫战》的英文名,这里小编作为该小游戏的化用,如有问题,请联系小编)

注意:游戏内为全英文!!!

先上效果:

【图1】

上图的字体来自某个字体生成网站:

patorjk.com

【图2】

【图3】

【图4】

接下来是游戏代码(以下不是最优成果,小编会继续修改):

#include<bits/stdc++.h>
#include<windows.h>
#include<ctime>
#include<stdlib.h>
using namespace std;

int location;     //location 敌军出现地点
int re;     //re 玩家攻击
int point=50;     //point 交易点数
int buy;     //buy 购买武器代号
int wave;     //wave 敌人波数
int hp=30,enemy_hp=20,boss_hp=50;     //hp 玩家血量   enemy_hp 普通敌人血量   boss_hp BOSS血量
bool p1=false,p2=false,p3=false;     //p1 判断第一个武器是否购买   p2 判断第二个武器是否购买   p3 判断第三个武器是否购买
int rnd=0;     //rnd 目前波数

int RandomNumber(){     //随机生成敌人
	srand((unsigned)time(NULL));
	location=rand()%8+1;
    printf("The enemy appeared in area %d .\n",location);
    return 0;
}

int isDead(){     //死亡界面
	system("cls");
	system("color 75");
	printf("\n");
	printf("      ▓██   ██▓ ▒█████   █    ██     ▄▄▄       ██▀███  ▓█████    ▓█████▄ ▓█████ ▄▄▄      ▓█████▄  ▐██▌ \n");
	printf("       ▒██  ██▒▒██▒  ██▒ ██  ▓██▒   ▒████▄    ▓██ ▒ ██▒▓█   ▀    ▒██▀ ██▌▓█   ▀▒████▄    ▒██▀ ██▌ ▐██▌ \n");
	printf("        ▒██ ██░▒██░  ██▒▓██  ▒██░   ▒██  ▀█▄  ▓██ ░▄█ ▒▒███      ░██   █▌▒███  ▒██  ▀█▄  ░██   █▌ ▐██▌ \n");
	printf("        ░ ▐██▓░▒██   ██░▓▓█  ░██░   ░██▄▄▄▄██ ▒██▀▀█▄  ▒▓█  ▄    ░▓█▄   ▌▒▓█  ▄░██▄▄▄▄██ ░▓█▄   ▌ ▓██▒ \n");
	printf("        ░ ██▒▓░░ ████▓▒░▒▒█████▓     ▓█   ▓██▒░██▓ ▒██▒░▒████▒   ░▒████▓ ░▒████▒▓█   ▓██▒░▒████▓  ▒▄▄  \n");
	printf("         ██▒▒▒ ░ ▒░▒░▒░ ░▒▓▒ ▒ ▒     ▒▒   ▓▒█░░ ▒▓ ░▒▓░░░ ▒░ ░    ▒▒▓  ▒ ░░ ▒░ ░▒▒   ▓▒█░ ▒▒▓  ▒  ░▀▀▒ \n");
	printf("       ▓██ ░▒░   ░ ▒ ▒░ ░░▒░ ░ ░      ▒   ▒▒ ░  ░▒ ░ ▒░ ░ ░  ░    ░ ▒  ▒  ░ ░  ░ ▒   ▒▒ ░ ░ ▒  ▒  ░  ░ \n");
	printf("       ▒ ▒ ░░  ░ ░ ░ ▒   ░░░ ░ ░      ░   ▒     ░░   ░    ░       ░ ░  ░    ░    ░   ▒    ░ ░  ░     ░ \n");
	printf("       ░ ░         ░ ░     ░              ░  ░   ░        ░  ░      ░       ░  ░     ░  ░   ░     ░    \n");
	printf("       ░ ░                                                        ░                       ░            \n");
	exit(0);
}

int victory(){     //胜利界面
	printf("\n");
	printf("You beat the Vikings away!\n");
	Sleep(2000);
	system("cls");
	system("color 75");
	printf(" ▄         ▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄         ▄       ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄       ▄         ▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄        ▄  ▄ \n");
	printf("▐░▌       ▐░▌▐░░░░░░░░░░░▌▐░▌       ▐░▌     ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌     ▐░▌       ▐░▌▐░░░░░░░░░░░▌▐░░▌      ▐░▌▐░▌\n");
	printf("▐░▌       ▐░▌▐░█▀▀▀▀▀▀▀█░▌▐░▌       ▐░▌     ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀      ▐░▌       ▐░▌ ▀▀▀▀█░█▀▀▀▀ ▐░▌░▌     ▐░▌▐░▌\n");
	printf("▐░▌       ▐░▌▐░▌       ▐░▌▐░▌       ▐░▌     ▐░▌       ▐░▌▐░▌       ▐░▌▐░▌               ▐░▌       ▐░▌     ▐░▌     ▐░▌▐░▌    ▐░▌▐░▌\n");
	printf("▐░█▄▄▄▄▄▄▄█░▌▐░▌       ▐░▌▐░▌       ▐░▌     ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄      ▐░▌   ▄   ▐░▌     ▐░▌     ▐░▌ ▐░▌   ▐░▌▐░▌\n");
	printf("▐░░░░░░░░░░░▌▐░▌       ▐░▌▐░▌       ▐░▌     ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌     ▐░▌  ▐░▌  ▐░▌     ▐░▌     ▐░▌  ▐░▌  ▐░▌▐░▌\n");
	printf(" ▀▀▀▀█░█▀▀▀▀ ▐░▌       ▐░▌▐░▌       ▐░▌     ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀█░█▀▀ ▐░█▀▀▀▀▀▀▀▀▀      ▐░▌ ▐░▌░▌ ▐░▌     ▐░▌     ▐░▌   ▐░▌ ▐░▌▐░▌\n");
	printf("     ▐░▌     ▐░▌       ▐░▌▐░▌       ▐░▌     ▐░▌       ▐░▌▐░▌     ▐░▌  ▐░▌               ▐░▌▐░▌ ▐░▌▐░▌     ▐░▌     ▐░▌    ▐░▌▐░▌ ▀ \n");
	printf("     ▐░▌     ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄█░▌     ▐░▌       ▐░▌▐░▌      ▐░▌ ▐░█▄▄▄▄▄▄▄▄▄      ▐░▌░▌   ▐░▐░▌ ▄▄▄▄█░█▄▄▄▄ ▐░▌     ▐░▐░▌ ▄ \n");
	printf("     ▐░▌     ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌     ▐░▌       ▐░▌▐░▌       ▐░▌▐░░░░░░░░░░░▌     ▐░░▌     ▐░░▌▐░░░░░░░░░░░▌▐░▌      ▐░░▌▐░▌\n");
	printf("      ▀       ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀       ▀         ▀  ▀         ▀  ▀▀▀▀▀▀▀▀▀▀▀       ▀▀       ▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀        ▀▀  ▀ \n");
	exit(0);
}

void printMap(){     //打印地图
	printf("             ┌──────┐\n");
	printf("╔══════════╦═╡ ⊙");
	cout<<setw(3)<<point%1000;
	printf("╞═╦══════════╗\n");
	printf("║          ║ └──────┘ ║          ║\n");
	printf("║          ║          ║          ║\n");
	printf("║          ║          ║          ║\n");
	printf("║          ║          ║          ║\n");
	printf("║          ║          ║          ║\n");
	printf("╠══════════╬══════════╬══════════╣\n");
	printf("║          ║          ║          ║\n");
	printf("║  ┏━━━┓   ║          ║        ┌─╨─┐\n");
	printf("║  ┃ ╳ ┃   ║          ║        │");
	cout<<' '<<(rnd/10?"":"0")<<rnd;
	printf("│\n");
	printf("║  ┗━━━┛   ║          ║        └─╥─┘\n");
	printf("║          ║          ║          ║\n");
	printf("╠══════════╬══════════╬══════════╣\n");
	printf("║          ║          ║          ║\n");
	printf("║          ║          ║          ║\n");
	printf("║          ║          ║          ║\n");
	printf("║          ║          ║          ║\n");
	printf("║ ┌────────────────────────────┐ ║\n");
	printf("╚═╡ ★ ");
	for(int i=1; i<=24; i++)
		if(hp*5/6>=i) printf("|");
		else printf(" ");
	printf("╞═╝\n");
	printf("  └────────────────────────────┘\n");
}

int main(){
	system("chcp 65001");     //将编码形式改为UTF-8
	system("color 74");
	printf("\n");
	printf("     ██╗  ██╗ ██╗ ███╗   ██╗  ██████╗  ██████╗   ██████╗  ███╗   ███╗ ██████╗  ██╗   ██╗ ███████╗ ██╗  ██╗\n");
    printf("     ██║ ██╔╝ ██║ ████╗  ██║ ██╔════╝  ██╔══██╗ ██╔═══██╗ ████╗ ████║ ██╔══██╗ ██║   ██║ ██╔════╝ ██║  ██║\n");
	printf("     █████╔╝  ██║ ██╔██╗ ██║ ██║  ███╗ ██║  ██║ ██║   ██║ ██╔████╔██║ ██████╔╝ ██║   ██║ ███████╗ ███████║\n");
	printf("     ██╔═██╗  ██║ ██║╚██╗██║ ██║   ██║ ██║  ██║ ██║   ██║ ██║╚██╔╝██║ ██╔══██╗ ██║   ██║ ╚════██║ ██╔══██║\n");
	printf("     ██║  ██╗ ██║ ██║ ╚████║ ╚██████╔╝ ██████╔╝ ╚██████╔╝ ██║ ╚═╝ ██║ ██║  ██║ ╚██████╔╝ ███████║ ██║  ██║\n");
	printf("     ╚═╝  ╚═╝ ╚═╝ ╚═╝  ╚═══╝  ╚═════╝  ╚═════╝   ╚═════╝  ╚═╝     ╚═╝ ╚═╝  ╚═╝  ╚═════╝  ╚══════╝ ╚═╝  ╚═╝\n");
	printf("\n");
	printf("Press any key to enter the game.\n");
	system("pause >nul");
	system("cls");
	
	system("color 79");
	printf("Look at the map below.\n");
	printf("\n");
	printf("You have a tower on the left of the map. Make sure your tower is not destroyed when attacked.\n");
	printf("Your tower has 30 HP. If HP become 0, GAME OVER!!!\n");
	printf("So, if the enemy fire to your tower, you'd better protect it with your weapon.\n");
	printf("Are you ready? Come on, protect your kingdom!\n");
	printf("\n");
	printMap();
	printf("MAP NOTE:\n");
	printf("area 1   area 2   area 3\n");
	printf("tower    area 4   area 5\n");
	printf("area 6   area 7   area 8\n");
	printf("\n");
	printf("Press any key if you are ready.\n");
	system("pause >nul");
	system("cls");
	
	system("color 79");
	printf("Martin: Hey, gay! The Viking is appoarching!\n");
	system("pause >nul");
	system("cls");
	printf("Mary: Go on the tower and get ready for fight!\n");
	system("pause >nul");
	system("cls");
	printf("Martin: It's your turn.There are several types of weapon for you:\n");
	printf("         1.cannon(1)    2.incendiary(2)    3.bombardment aircraft(3)\n");
    printf("           50 points      75 points          100 points\n");
    system("pause >nul");
	system("cls");
	printf("Martin: You can get points by killing the enemy.\n");
	printf("        The waves of enemies you will stick to: \n");
	scanf("%d",&wave);
	while(wave<10){
		printf("Warning: This is too little, please increase it. (The enemy's wave number is at least 10.)\n");
		scanf("%d",&wave);
	}
	printf("Mary: OK. Press any key and start game.\n");
	system("pause >nul");
	system("cls");
	
	system("color 70");
	printf("Item Column:\n");
	printf("1.cannon(1)    2.incendiary(2)    3.bombardment aircraft(3)\n");
	printf("  50 points      75 points          100 points\n");
	printf("\n");
	for(rnd=1;rnd<wave;rnd++){
		printMap();
		//随机生成敌人
	    RandomNumber();
	    printf("Fire to area:\n");
	    scanf("%d",&re);
	    enemy_hp=20;
	    //判断是否击杀敌人
	    if(re==location){
	    	while(enemy_hp>0){
	    		if(p1==true)enemy_hp-=5,point-=1,hp+=1;
	    		if(p2==true)enemy_hp-=10,point-=2,hp+=5;
	    		if(p3==true)enemy_hp-=20,point-=5,hp+=10;
	    		if(p1==false&&p2==false&&p3==false)enemy_hp-=4,point-=1;
	    		printf("\n");
	    		printf("HP of enemy: %d \n",enemy_hp);
	    		printf("HP of tower: %d \n",hp);
	    		printf("point: %d \n",point);
	    		Sleep(1000);
	    		//判断是否死亡
	    		if(hp==0){
					printf("Warning: Your tower's HP is equal to zero.\n");
					Sleep(2000);
					isDead();
				}
				if(point<0){
					printf("Warning: Your point is negative.\n");
					Sleep(2000);
					isDead();
				}
				if(hp<0){
					printf("Warning: Your tower's HP is negative.\n");
					Sleep(2000);
					isDead();
				}
			}
	    	if(enemy_hp==0){
	    		p1=false,p2=false,p3=false;
	    		printf("\n");
	    		printf("You destroyed the %d  wave of enemies!\n",rnd);
	    		point+=15;
	            printf("point: %d \n",point);
	            Sleep(1000);
	            printf("\n");
	            printf("You can buy some weapons with trading points. Press '999' to skip.\n");
				scanf("%d",&buy);
				//购买武器
				if(buy==1){
					point-=50;
					printf("You trade a cannon. It costs 50 points.\n");
					p1=true;
				}
				if(buy==2){
					point-=75;
					printf("You trade an incendiary. It costs 75 points.\n");
					p2=true;
				}
				if(buy==3){
					point-=100;
		    		printf("You trade a bombardment aircraft. It costs 100 points.\n");
		    		p3=true;
				}
				if(buy==999)continue;
	        	Sleep(2000);
	    	}
	    }
	    else{
	    	p1=false,p2=false,p3=false;
	    	printf("\n");
		    printf("This wave of enemies has never been hit. Come on next time!\n");
		    hp-=5;
		    point-=5;
		    printf("HP of tower: %d \n",hp);
		    printf("point: %d \n",point);
		    printf("\n");
			Sleep(2000);
		}
	}
	
	printMap();
	printf("The last wave. THE BOSS IS COMING!!!\n");
	//随机生成boss
	RandomNumber();
	printf("Fire to area:\n");
	scanf("%d",&re);
	//判断是否击杀boss
	if(re==location){
	    while(boss_hp>0){
	    	if(p1==true)boss_hp-=5,point-=1,hp+=1;
	    	if(p2==true)boss_hp-=10,point-=2,hp+=5;
	    	if(p3==true)boss_hp-=20,point-=5,hp+=10;
	    	if(p1==false&&p2==false&&p3==false)boss_hp-=4,point-=1;
	    	printf("\n");
	    	printf("HP of boss: %d \n",boss_hp);
	    	printf("HP of tower: %d \n",hp);
	    	printf("point: %d \n",point);
	    	Sleep(1000);
	    	//判断是否死亡
	    		if(hp==0){
					printf("Warning: Your tower's HP is equal to zero.\n");
					Sleep(2000);
					
				}
				if(point<0){
					printf("Warning: Your point is negative.\n");
					Sleep(2000);
					isDead();
				}
				if(hp<0){
					printf("Warning: Your tower's HP is negative.\n");
					Sleep(2000);
					isDead();
				}
		}
	    if(boss_hp==0){
	    	p1=false,p2=false,p3=false;
	    	printf("\n");
	    	printf("You beat the boss! The Vikings are running away!\n");
	    	point+=15;
	        printf("point: %d \n",point);
	        Sleep(2000);
	    }
	}
	else{
	    p1=false,p2=false,p3=false;
	    printf("\n");
		printf("The boss has never been hit. YOU ARE DIE!\n");
		hp-=5;
		point-=5;
		printf("HP of tower: %d \n",hp);
		printf("point: %d \n",point);
		printf("\n");
		Sleep(2000);
	}
	victory();
	return 0;
}

/*color:
0 = 黑色   8 = 灰色 
1 = 蓝色   9 = 淡蓝色 
2 = 绿色   A = 淡绿色 
3 = 浅绿色 B = 淡浅绿色 
4 = 红色   C = 淡红色 
5 = 紫色   D = 淡紫色 
6 = 黄色   E = 淡黄色 
7 = 白色   F = 亮白色  
*/

//-----------------------------------------------------------------------------------------
//版权声明:本代码由沾花把玖独立编写,未经允许,请勿抄袭!(感谢SuperAlex4等人对沾花把玖的帮助)

注意:以上代码最好使用UTF-8编码形式,否则可能会产生意想不到的结果! 

详细附录(包括patorjk的操作方法、UTF-8的转换方法)请见:C++ Kingdomrush小游戏制作(附录)_沾花把玖的博客


本文内容为小编自己汇总,内容可能会有错误或疏漏,感谢大家的提议!

记得点赞和关注哦~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值