文章目录
一、题目描述
(1)程序运行后,首先需要输入用户登录名和登录密码,输入正确以后进入游戏菜单,输入错误重新输入(上限3次),也会判断用户是否存在;
(2)进入游戏菜单后,会选择随机匹配人物或者自己选择人物,人物选择完成后进入下一步;
(3)根据出现的菜单选择相应的功能(1.显示选择的人物信息;2.商城剁手;3.养家糊口)。
二、部分代码展示
1.用户信息数组及初始化
public struct UserInfo
{
public string UserName;
public string UserPwd;
}
static UserInfo[] UserItem = new UserInfo[5];
public static void AddUserInfo()
{
UserItem[0].UserName = "张三";
UserItem[0].UserPwd = "123456";
UserItem[1].UserName = "李四";
UserItem[1].UserPwd = "123456";
UserItem[2].UserName = "王五";
UserItem[2].UserPwd = "123456";
UserItem[3].UserName = "赵六";
UserItem[3].UserPwd = "123456";
UserItem[4].UserName = "孙七";
UserItem[4].UserPwd = "123456";
}
2.main函数
static void Main(string[] args)
{
AddUserInfo();//初始化用户信息
//1.登录
if (login())
{
Console.WriteLine("----------------------------------");
Console.WriteLine("-------------女神联盟-------------");
Console.WriteLine("----------------------------------");
//2.展示所有英雄
AddHero();//初始化女神信息
ShowHero();//显示所有女神信息
//3.选择英雄,获得选择女神的数组下标
int num = NumHero() - 1;
Console.WriteLine("您选择的女神是{0},她的基本信息如下:", HeroItem[num].HeroName);
//4.展示指定编号的女神信息
ShowHero(num);
//5.进入游戏主要流程
MainProcess(num);
}
}
3.商城函数
public static void Shopping(int num)//num接收选中的英雄编号
{
do
{
//1.显示已有商品信息
ShowGoods();
//2.接收购买编号
Console.WriteLine("请输入购买编号:");
int x = int.Parse(Console.ReadLine()) - 1;
//3.判断是否金钱足够
if (goods[x].CommodityMoney <= HeroItem[num].Money)
{
//4.购买成功
Console.WriteLine("购买成功!");
// 4.1.选定的女神属性增加
HeroItem[num].HeroAttack += goods[x].CommodityAttack;
HeroItem[num].HeroDefense += goods[x].CommodityDefense;
HeroItem[num].HeroHealth += goods[x].CommodityHealth;
// 4.2.选定的女神金钱减少
HeroItem[num].Money -= goods[x].CommodityMoney;
}
else
{
Console.WriteLine("您的金币不足!");
}
Console.WriteLine("是否继续购物?y/n?");
if (Console.ReadLine() == "n")
{
break;
}
} while (true);
}
4.养家糊口函数
public static void Existence(int num)
{
AddMonsterItem();//初始化怪物信息
int MonsterNum = new Random().Next(5);//随机怪物
int InjuryValue = HeroItem[num].HeroAttack - MonsterItem[MonsterNum].MonsterDefense;//女神伤害值=女神的攻击-怪物的防御力
Console.WriteLine("开始战斗:");
while (true)
{
MonsterItem[MonsterNum].MonsterHealth -= InjuryValue;//怪物被女神攻击,重新计算血量
if (MonsterItem[MonsterNum].MonsterHealth <= 0)
{
Console.WriteLine("女神{0}向怪物{1}发出攻击,造成{2}点伤害,{3}剩余血量{4}"
, HeroItem[num].HeroName, MonsterItem[MonsterNum].MonsterName, InjuryValue,
MonsterItem[MonsterNum].MonsterName, 0);
Console.WriteLine("女神获得{0}点金币", MonsterItem[MonsterNum].MonsterMoney);
break;
}
else
{
Console.WriteLine("女神{0}向怪物{1}发出攻击,造成{2}点伤害,{3}剩余血量{4}"
, HeroItem[num].HeroName, MonsterItem[MonsterNum].MonsterName, InjuryValue,
MonsterItem[MonsterNum].MonsterName, MonsterItem[MonsterNum].MonsterHealth);
}
}
}
三、运行界面
1.登录
2.随机人物选择
3.自由选择人物
4.显示信息
5.养家糊口
6.商城剁手
四、程序源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
//用户信息数组
public struct UserInfo
{
public string UserName;
public string UserPwd;
}
static UserInfo[] UserItem = new UserInfo[5];
public static void AddUserInfo()
{
UserItem[0].UserName = "张三";
UserItem[0].UserPwd = "123456";
UserItem[1].UserName = "李四";
UserItem[1].UserPwd = "123456";
UserItem[2].UserName = "王五";
UserItem[2].UserPwd = "123456";
UserItem[3].UserName = "赵六";
UserItem[3].UserPwd = "123456";
UserItem[4].UserName = "孙七";
UserItem[4].UserPwd = "123456";
}
//女神信息
public struct HeroState
{
public string HeroName;//女神姓名
public int HeroAttack;//女神攻击力
public int HeroDefense;//女神防御力
public int HeroHealth;//女神生命值
public int Money;//金钱
}
static HeroState[] HeroItem = new HeroState[5];
public static void AddHero()
{
HeroItem[0].HeroName = "不知火舞";
HeroItem[0].HeroAttack = 60;
HeroItem[0].HeroDefense = 30;
HeroItem[0].HeroHealth = 1200;
HeroItem[0].Money = 1000;
HeroItem[1].HeroName = "芈月";
HeroItem[1].HeroAttack = 55;
HeroItem[1].HeroDefense = 35;
HeroItem[1].HeroHealth = 1100;
HeroItem[1].Money = 1000;
HeroItem[2].HeroName = "武则天";
HeroItem[2].HeroAttack = 58;
HeroItem[2].HeroDefense = 30;
HeroItem[2].HeroHealth = 1000;
HeroItem[2].Money = 1000;
HeroItem[3].HeroName = "花木兰";
HeroItem[3].HeroAttack = 55;
HeroItem[3].HeroDefense = 35;
HeroItem[3].HeroHealth = 1100;
HeroItem[3].Money = 1000;
HeroItem[4].HeroName = "貂蝉";
HeroItem[4].HeroAttack = 58;
HeroItem[4].HeroDefense = 30;
HeroItem[4].HeroHealth = 1000;
HeroItem[4].Money = 1000;
}
//商品信息
public struct Commodity
{
public string CommodityName;//商品名称
public int CommodityAttack;//商品攻击力
public int CommodityDefense;//商品防御力
public int CommodityHealth;//商品生命值
public int CommodityMoney;//商品价格
}
static Commodity[] goods = new Commodity[99];
public static void AddCommodity()
{
goods[0].CommodityName = "风暴巨剑";
goods[0].CommodityAttack = 80;
goods[0].CommodityDefense = 0;
goods[0].CommodityHealth = 0;
goods[0].CommodityMoney = 910;
goods[1].CommodityName = "力量腰带";
goods[1].CommodityAttack = 80;
goods[1].CommodityDefense = 0;
goods[1].CommodityHealth = 1000;
goods[1].CommodityMoney = 900;
goods[2].CommodityName = "破烂铁剑";
goods[2].CommodityAttack = 20;
goods[2].CommodityDefense = 0;
goods[2].CommodityHealth = 0;
goods[2].CommodityMoney = 250;
goods[3].CommodityName = "冲击铠甲";
goods[3].CommodityAttack = 80;
goods[3].CommodityDefense = 240;
goods[3].CommodityHealth = 800;
goods[3].CommodityMoney = 2100;
goods[4].CommodityName = "血魔之怒";
goods[4].CommodityAttack = 20;
goods[4].CommodityDefense = 0;
goods[4].CommodityHealth = 1000;
goods[4].CommodityMoney = 2120;
}
//怪物信息数组
public struct Monster
{
public string MonsterName;//怪物名称
public int MonsterDefense;//怪物防御力
public int MonsterHealth;//怪物生命值
public int MonsterMoney;//怪物价值金币
}
static Monster[] MonsterItem = new Monster[5];
public static void AddMonsterItem()
{
MonsterItem[0].MonsterName = "森林野狼";
MonsterItem[0].MonsterDefense = 15;
MonsterItem[0].MonsterHealth = 500;
MonsterItem[0].MonsterMoney = new Random().Next(30, 50);//掉落金钱随机
MonsterItem[1].MonsterName = "暗夜幽灵";
MonsterItem[1].MonsterDefense = 10;
MonsterItem[1].MonsterHealth = 400;
MonsterItem[1].MonsterMoney = new Random().Next(20, 40);//掉落金钱随机
MonsterItem[2].MonsterName = "变异蛤蟆";
MonsterItem[2].MonsterDefense = 25;
MonsterItem[2].MonsterHealth = 700;
MonsterItem[2].MonsterMoney = new Random().Next(40, 60);//掉落金钱随机
MonsterItem[3].MonsterName = "泰坦后裔";
MonsterItem[3].MonsterDefense = 45;
MonsterItem[3].MonsterHealth = 900;
MonsterItem[3].MonsterMoney = new Random().Next(50, 70);//掉落金钱随机
MonsterItem[4].MonsterName = "邪恶巨龙";
MonsterItem[4].MonsterDefense = 35;
MonsterItem[4].MonsterHealth = 900;
MonsterItem[4].MonsterMoney = new Random().Next(60, 85);//掉落金钱随机
}
//进入游戏前的准备工作
static void Main(string[] args)
{
AddUserInfo();//初始化用户信息
//1.登录
if (login())
{
Console.WriteLine("----------------------------------");
Console.WriteLine("-------------女神联盟-------------");
Console.WriteLine("----------------------------------");
//2.展示所有英雄
AddHero();//初始化女神信息
ShowHero();//显示所有女神信息
//3.选择英雄,获得选择女神的数组下标
int num = NumHero() - 1;
Console.WriteLine("您选择的女神是{0},她的基本信息如下:", HeroItem[num].HeroName);
//4.展示指定编号的女神信息
ShowHero(num);
//5.进入游戏主要流程
MainProcess(num);
}
}
//登录
public static bool login()
{
int num = 0;
bool iRet = false;
do
{
if (num == 3)
{
Console.WriteLine("错误超过3次,请重新开始!");
break;
}
Console.WriteLine("请输入登录账号名:");
string UserName = Console.ReadLine();
Console.WriteLine("请输入登录密码:");
string UserPwd = Console.ReadLine();
num++;
if (MatchingUser(UserName, UserPwd))//判断用户是否匹配成功
{
iRet = true;
break;
}
} while (true);
return iRet;
}
//匹配用户是否存在
public static bool MatchingUser(string name, string pwd)
{
bool iRet = false;
for (int i = 0; i < UserItem.Length; i++)
{
if (name == UserItem[i].UserName && pwd == UserItem[i].UserPwd)
{
iRet = true;
break;
}
}
return iRet;
}
//展示所有女神
public static void ShowHero()
{
Console.WriteLine("女神候选人如下:");
for (int i = 0; i < HeroItem.Length; i++)
{
Console.WriteLine("{0}.{1}", i + 1, HeroItem[i].HeroName);
}
}
//选择一个女神,返回选择得女神编号
public static int NumHero()
{
int HeroNum = -1;//记录选择的英雄编号
do
{
Console.WriteLine("请输入选择模式:");
Console.WriteLine("1.自由选择");
Console.WriteLine("2.随机选择");
string num = Console.ReadLine();
if (num == "1")
{
Console.WriteLine("请输入选择的编号:");
HeroNum = int.Parse(Console.ReadLine());
break;
}
else if (num == "2")
{
HeroNum = new Random().Next(HeroItem.Length);//获得一个0-4的随机数
break;
}
else
{
Console.WriteLine("选择错误,请重新选择");
}
} while (true);
return HeroNum;
}
//展示指定编号的女神信息
public static void ShowHero(int Num)
{
Console.WriteLine("女神候选人如下:");
Console.WriteLine("姓名:{0}", HeroItem[Num].HeroName);
Console.WriteLine("攻击力:{0}", HeroItem[Num].HeroAttack);
Console.WriteLine("防御力:{0}", HeroItem[Num].HeroDefense);
Console.WriteLine("生命值:{0}", HeroItem[Num].HeroHealth);
Console.WriteLine("剩余金钱:{0}", HeroItem[Num].Money);
}
//主要流程
public static void MainProcess(int num)
{
do
{
Console.WriteLine("----------------------------------");
Console.WriteLine("-------------女神联盟-------------");
Console.WriteLine("----------------------------------");
Console.WriteLine("游戏测试功能如下:");
Console.WriteLine("1.商城剁手");
Console.WriteLine("2.养家糊口");
Console.WriteLine("3.显示女神当前信息");
int x = int.Parse(Console.ReadLine());
switch (x)
{
case 1:
Shopping(num);
break;
case 2:
Existence(num);
break;
case 3:
ShowHero(num);
break;
}
//底部
Console.WriteLine("是否继续游戏?y/n?");
if (Console.ReadLine() == "n")
{
Console.WriteLine("游戏结束!");
break;
}
} while (true);
}
//商城主函数
public static void Shopping(int num)//num接收选中的英雄编号
{
do
{
//1.显示已有商品信息
ShowGoods();
//2.接收购买编号
Console.WriteLine("请输入购买编号:");
int x = int.Parse(Console.ReadLine()) - 1;
//3.判断是否金钱足够
if (goods[x].CommodityMoney <= HeroItem[num].Money)
{
//4.购买成功
Console.WriteLine("购买成功!");
// 4.1.选定的女神属性增加
HeroItem[num].HeroAttack += goods[x].CommodityAttack;
HeroItem[num].HeroDefense += goods[x].CommodityDefense;
HeroItem[num].HeroHealth += goods[x].CommodityHealth;
// 4.2.选定的女神金钱减少
HeroItem[num].Money -= goods[x].CommodityMoney;
}
else
{
Console.WriteLine("您的金币不足!");
}
Console.WriteLine("是否继续购物?y/n?");
if (Console.ReadLine() == "n")
{
break;
}
} while (true);
}
//显示所有商品
public static void ShowGoods()
{
Console.WriteLine("商品如下:");
Console.WriteLine("序号\t商品名称\t攻击力\t防御力\t生命值\t价格");
AddCommodity();//初始化商品信息
int x = 1;
foreach (Commodity item in goods)
{
if (item.CommodityName != null)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", x
, item.CommodityName, item.CommodityAttack, item.CommodityDefense
, item.CommodityHealth, item.CommodityMoney);
x++;
}
}
}
//养家糊口主函数
public static void Existence(int num)
{
AddMonsterItem();//初始化怪物信息
int MonsterNum = new Random().Next(5);//随机怪物
int InjuryValue = HeroItem[num].HeroAttack - MonsterItem[MonsterNum].MonsterDefense;//女神伤害值=女神的攻击-怪物的防御力
Console.WriteLine("开始战斗:");
while (true)
{
MonsterItem[MonsterNum].MonsterHealth -= InjuryValue;//怪物被女神攻击,重新计算血量
if (MonsterItem[MonsterNum].MonsterHealth <= 0)
{
Console.WriteLine("女神{0}向怪物{1}发出攻击,造成{2}点伤害,{3}剩余血量{4}"
, HeroItem[num].HeroName, MonsterItem[MonsterNum].MonsterName, InjuryValue,
MonsterItem[MonsterNum].MonsterName, 0);
Console.WriteLine("女神获得{0}点金币", MonsterItem[MonsterNum].MonsterMoney);
break;
}
else
{
Console.WriteLine("女神{0}向怪物{1}发出攻击,造成{2}点伤害,{3}剩余血量{4}"
, HeroItem[num].HeroName, MonsterItem[MonsterNum].MonsterName, InjuryValue,
MonsterItem[MonsterNum].MonsterName, MonsterItem[MonsterNum].MonsterHealth);
}
}
}
}
}
📢欢迎点赞👍 收藏🌟 留言💬
📢单纯分享日常中的小练习,技术有限,如果发现有错欢迎指导