有点简陋,呵呵。
首先血量,攻击力等弄个属性以方便以后扩展程序:
public int HP { get; set; }//血量
public long ATK { get; set; }//攻击力
public int defense { get; set; }//防御力
public int money { get; set; }//钱
public short san { get; set; }//san值
public long Monster1_HP { get; set; }
public long Monster2_HP { get; set; }
public int Monster1_ATK { get; set; }
public int Monster2_ATK { get; set; }
等以后一时兴起可以在属性块中增添逻辑以此来改变各种值。例如如果我想弄个困难就可以在属性块中进行相应的逻辑处理 (如下所示):
public double A;
public double a
{
get
{
return A;
}
set
{
A=value*0.3;
}
}
因为现在属性块中没有逻辑,所以可以使用自动属性。这时,编译器会给你自动配个字段
关于属性,1.读取该值时花的时间不应太长。2.用属性还可以增加安全性,一般与属性配对的字段都用private来修饰。3.别动不动对一些无关紧要的字段用属性,这没必要,反而增加开销
然后我们在基类中用个构造函数:
public Basis(int hp, long atk, int Money, short San, int Defense, int monster1_HP, int monster2_HP, int monster1_ATK, int monster2_ATK)
{
HP = hp;
ATK = atk;
money = Money;
san = San;
defense = Defense;
Monster1_HP = monster1_HP;
Monster2_HP = monster2_HP;
Monster1_ATK = monster1_ATK;
Monster2_ATK = monster2_ATK;
}
当我们在继承的类中有 有参构造函数时,要在派生类中提供对应的实参,这可以用base关键字解决:
class B
{
public int b;
public B(int a)
{
b=a;
}
}
class A:B
{
public A():base (2)
{
}
可以用this关键字来用当前类的另外一些构造函数:
public int b;
public A(int c)
{
b=c;
}
public A():this (4)
{
}
不要把需要在某个特定时期执行的代码放在静态构造函数中,因为.NET库不确保它什么时候执行
一个类只能由一个静态函数
swith有switch表达式,用=>来表示返回值,下划线表示默认值
技能代码:
public void Ji()
{
Random random = new();
int jineng = random.Next(1, 4);
if (hero > 0)
{
if (guai2 == 1 && Monster1_HP >= 0 && count % 2 == 0 && checkpoint == 2)
{
if (defense <= Monster1_ATK)
{
switch (jineng)
{
case 1:
HP -= (Monster1_ATK -= defense);
Monster1_ATK += defense;
WriteLine("你遭到了平A");
WriteLine($"你还有{HP}血");
break;
case 2:
for (int i = 0; i < 3; i++)
{
HP -= (Monster1_ATK -= defense);
Monster1_ATK += defense;
WriteLine("你遭到了连续打击");
WriteLine($"你还有{HP}血");
}
break;
case 3:
Monster1_ATK += 30;
WriteLine($"怪物的攻击力升级了,现在为{Monster1_ATK}");
break;
}
}
else
{
HP -= 1;
WriteLine($"你还有{HP}血1");
}
}
if (guai2 == 2 && Monster1_HP >= 0 && count % 2 == 0 && checkpoint == 2)
{
if (defense <= Monster1_ATK)
{
switch (jineng)
{
case 1:
HP -= (Monster1_ATK -= defense);
Monster1_ATK += defense;
WriteLine("你遭到1号怪物的平A");
WriteLine($"你还有{HP}血");
break;
case 2:
san -= 2;
WriteLine($"你遭到精神污染,san值为{san}");
break;
case 3:
Monster1_HP += 100;
WriteLine($"怪物的血量恢复了{Monster1_HP}");
break;
}
}
else
{
HP -= 1;
WriteLine($"你还有{HP}血");
}
}
if (guai2 == 2 && Monster2_HP >= 0 && count % 2 == 0 && checkpoint == 2)
{
if (defense <= Monster2_ATK)
{
switch (jineng)
{
case 1:
HP -= (Monster2_ATK -= defense);
Monster1_ATK += defense;
WriteLine("你遭到2号怪物的平A");
WriteLine($"你还有{HP}血");
break;
case 2:
HP -= 200;
WriteLine($"你遭到了2号怪物的真实伤害{HP}");
break;
case 3:
Monster2_HP += 200;
WriteLine($"2号怪物的血量恢复了{Monster2_HP}");
break;
}
}
else
{
HP -= 1;
WriteLine($"你还有{HP}血");
}
}
if (guai2 == 3 && Monster1_HP >= 0 && count % 2 == 0 && checkpoint == 2)
{
if (defense <= Monster1_ATK)
{
switch (jineng)
{
case 1:
HP -= (Monster1_ATK -= defense);
Monster1_ATK += defense;
WriteLine("你遭到怪物的平A");
WriteLine($"你还有{HP}血");
break;
case 2:
if (san >= 40)
{
san = 10;
WriteLine($"你遭到了怪物的真理碾压,san值为{san}");
}
break;
case 3:
if (count % 4 == 0)
{
HP -= 1250;
WriteLine($"你一下子遭到了高级凝视,一下子失去大量血量,你还有{HP}血");
}
break;
}
}
else
{
HP -= 1;
WriteLine($"你还有{HP}血");
}
}
if (hero == 1 && Monster1_HP >= 0 && count % 2 == 0 && checkpoint == 0)
{
if (defense <= Monster1_ATK)
{
switch (jineng)
{
case 1:
HP -= (Monster1_ATK -= defense);
Monster1_ATK += defense;
WriteLine("你遭到怪物的平A");
WriteLine($"你还有{HP}血");
break;
case 2:
if (san < 60)
{
Monster1_ATK += 1000;
WriteLine($"恐惧,怪物的攻击力升级了,现在为{Monster1_ATK}");
}
break;
case 3:
if (Monster1_HP <= 0)
{
Monster1_HP += 2000;
WriteLine($"怪物的血量恢复了{Monster1_HP}");