一个简单的游戏——c++

#include<ctime>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
int map[12][12]; 
int derection[3]={0,1,-1};
int calculate(int x,int y)
{
	int counter=0;
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			if (map[x+derection[i]][y+derection[j]]==9 )
				counter++;
			return counter;
}
void game(int x,int y)
{
    if(calculate(x,y)==0)
	{
		map[x][y]=0;
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
				if(x+derection[i]<=9&&y+derection[j]<=9&&x+derection[i]>=1&&y+derection[j]>=1&&!(derection[i]==0&&derection[j]==0)&&map[x+derection[i]][y+derection[j]]==-1) 
					game( x+derection[i], y+derection[j] ); 
		}                 
	}
	else
		map[x][y] = calculate(x,y);
}
void print()
{
	for(int i=1;i<10;i++)
	{
		for(int j=1;j<10;j++)
		{
			if(map[i][j]==-1||map[i][j]==9)
				cout<<"#"<<" ";
			else
			{
				if(map[i][j]==0) cout<<"o ";
				else
				cout<<map[i][j]<<" ";
			}
		}
		cout<<endl;
	}
}
bool check ()
{
	int counter=0;
	for (int i=1;i<10;i++)
		for (int j=1;j<10;j++)
			if(map[i][j]!=-1)
				counter++;
			if(counter==10)
				return true;
			else
				return false;
}
int main ()
{
	int i,j,x,y;
	char ch;
	srand(time(0));
	do
	{
		memset(map,-1,sizeof(map));
		for(i=0;i<10;)
		{
			x=rand()%9+1;
			y=rand()%9+1;
			if ( map[x][y]!=9 )
			{
				map[x][y]=9;
				i++;
			}
		}
		for(i=1;i<10;i++ )
		{
			for(j=1;j<10;j++ )
				cout<<"#"<<" ";
			cout<<"\n";
		}
		cout<<"\n";
		cout<<"Please enter a coordinate:(x,y)\n";
		while(cin>>x>>y)
		{
			if(map[x][y]==9)
			{
				cout<<"GAME OVER"<<endl;   
				for(i=1;i<10;i++)
				{
					for(j=1;j<10;j++ )
					{
						if(map[i][j]==9 )
							cout<<"@ ";
						else
							cout<<"# ";
					}
					cout<<endl;
				}
				break;
			}
			game(x,y);
			print();
			cout<<"Please enter a coordinate:(x,y)\n";
			if(check())
			{
				cout<<"YOU WIN"<<endl;
				break;
			}
			cout<<"\n";
		}	
		cout<<"Do you want to play again?\nenter Y or N"<<endl;
		cin>>ch;
		cout<<"\n";
	}while(ch=='Y');	
	return 0;
}

 

  • 9
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
游戏分里外两个部分组成,里部分(用户不可见) 通过里部分执行判断,地图数组更改,和各种值的改变。更改完里部分再根据相应变化更改表部分。(用户可视部分)表部分的打印通过gotoxy去到相应坐标再printf打印出字符,通过文本函数改变文字字体颜色与文字背景颜色与字符组合实现图形界面。 程序通过 计数器+循环判断 的思想,类似单核cpu的多线程实现(单线程在不同程序/函数间来回执行)省去了多线程。(具体过程在功能设计与描述有详细描述) 另AI实现与加强依赖于rand随机函数的运用,进一步强化AI,增加游戏乐趣 功能方面,游戏参考于80年代任天堂红白机(FC/FamilyComputer)上的游戏坦克大战(Battle City),包括地图,游戏模式等等(当时的游戏直接烧在电路板上)。所以游戏平衡方面已经有了很好的参考,无需再花大量时间测试平衡性。 但诸如地图中的树林元素,随机道具等没有实现。但较之原版,该游戏由C/C++编写PC运行,由字符界面实现游戏画面。原版一辆坦克的子弹未消失之前不能发射第二颗。导致子弹打击远处CD长,近处CD短。该游戏每个子弹都有相同CD,子弹未消失只要CD达到即可发射第二颗,第三颗…增加了真实性,相较于原版是个改进。且考虑到PC性能不一内置了游戏速度调整。玩家可根据PC性能调整至合适的速度。
以下是一个简单C++文字RPG游戏的示例代码,其中包括了基本的角色、战斗和物品系统: ```c++ #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; // 角色类 class Character { public: string name; int hp; int atk; int def; int gold; Character(string n, int h, int a, int d, int g) { name = n; hp = h; atk = a; def = d; gold = g; } // 攻击函数 void attack(Character& other) { int damage = atk - other.def; if (damage < 0) { damage = 0; } other.hp -= damage; cout << name << "攻击了" << other.name << ",造成了" << damage << "点伤害。" << endl; } // 是否死亡 bool isDead() { return hp <= 0; } }; // 物品类 class Item { public: string name; int price; int hp; int atk; int def; Item(string n, int p, int h, int a, int d) { name = n; price = p; hp = h; atk = a; def = d; } }; // 商店类 class Shop { public: Item items[3]; Shop() { items[0] = Item("草药", 10, 20, 0, 0); items[1] = Item("铁剑", 50, 0, 10, 0); items[2] = Item("铁甲", 100, 0, 0, 10); } // 显示商店物品 void showItems() { cout << "欢迎光临!以下是本店的物品:" << endl; for (int i = 0; i < 3; i++) { cout << i + 1 << ". " << items[i].name << " - " << items[i].price << "金币" << endl; } } // 购买物品 bool buy(Character& c, int choice) { if (c.gold < items[choice - 1].price) { cout << "金币不足,法购买!" << endl; return false; } c.gold -= items[choice - 1].price; c.hp += items[choice - 1].hp; c.atk += items[choice - 1].atk; c.def += items[choice - 1].def; cout << "购买成功!" << endl; return true; } }; // 战斗函数 void battle(Character& player, Character& enemy) { cout << "你遇到了一只" << enemy.name << ",准备战斗!" << endl; while (!player.isDead() && !enemy.isDead()) { player.attack(enemy); if (enemy.isDead()) { cout << enemy.name << "被你打败了!" << endl; player.gold += enemy.gold; return; } enemy.attack(player); if (player.isDead()) { cout << "你被" << enemy.name << "打败了!" << endl; return; } } } int main() { srand(time(NULL)); // 初始化随机数种子 // 初始化角色和商店 Character player("勇者", 100, 10, 5, 50); Character enemies[3] = { Character("史莱姆", 30, 5, 2, 10), Character("骷髅兵", 50, 10, 5, 20), Character("巨龙", 100, 20, 10, 50) }; Shop shop; // 游戏循环 while (true) { cout << "你的状态 - HP:" << player.hp << " ATK:" << player.atk << " DEF:" << player.def << " 金币:" << player.gold << endl; cout << "请选择操作:" << endl; cout << "1. 进入商店" << endl; cout << "2. 进行战斗" << endl; cout << "3. 离开游戏" << endl; int choice; cin >> choice; switch (choice) { case 1: shop.showItems(); cout << "请选择要购买的物品(输入编号):" << endl; cin >> choice; shop.buy(player, choice); break; case 2: battle(player, enemies[rand() % 3]); break; case 3: cout << "游戏结束,欢迎再次光临!" << endl; return 0; default: cout << "无效的操作!" << endl; break; } } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值