C#作为巨硬推出的纯面向对象语言,曾经期待其与Java抗衡,哪知道生不逢时,早早退出了霸主之位的争夺,但这不能掩盖C#是一个极度简洁优美的语言,开发效率甚至在某些方面优于Java(但是缺乏生态和社区支持)目前C#在工业领域和桌面级开发(.NET)和unity中占有一席之地。
而对于语言的学习,通过制作控制台小游戏不失为一个很好的方法。PS:这个例子是我刚开始学习C#时制作的游戏,过于简单简陋,请谅解
第一关的战斗画面
第二关会被敌人包围,使用大招清屏
第三关购买装备,打败Boss通关
首先我们要明确一点,即使是控制台游戏,也满足和目前3A大作一致的图形渲染刷新和逻辑框架,所以我们可以明确,在控制台中,我们需要刷新些什么
这就是魔塔类控制台游戏大概的游戏框架。现在我们所要做的就是要按照这个框架,将各个模块实现,最后通过main函数来运行就行啦。
和Unity引擎的Awake()、Start()类似,我们需要在启动游戏前,将准备工作都做好。首先是一个游戏中所有角色包括玩家甚至商店的基类,命名为Role,包括了一个游戏角色该有的一切基本属性,血量、技能、状态等等,而玩家和怪物及boss的类都派生于此,市面上的游戏大多在设计角色时也是这种模式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace rpg
{
class Role
{
public string name
{
get;
protected set;
}
public int hp
{
get;
set;
}
public int mp
{
get;
set;
}
public int base_attackValue
{
get;
set;
}
public int defense
{
get;
set;
}
public int dex
{
get;
set;//闪避几率
}
public Dictionary<int, Skill> skillList = new Dictionary<int, Skill>();
//public List states = new List();
public List<item> itemList = new List<item>();
public Role(string name, int hp, int mp,int base_attackValue, int defense, int dex, int x,int y)
{
this.name = name;
this.hp = hp;
this.mp = mp;
this.base_attackValue = base_attackValue;
this.defense = defense;
this.dex = dex;
pos.x = x;
pos.y = y;
}
public Pos pos=new Pos(0,0);
public virtual void use_skill(Player player, Monster monster, int skillType,ref string battleinfo)
{
if (player.skillList.ContainsKey(skillType))
{
if (skillType == 0)
{
monster.hp = monster.hp + monster.defense - player.skillList[0].damage;
}
else if (skillType == 2)
{
player.hp = player.hp + player.skillList[2].damage;
}
else if (skillType == 3)
{
if (monster.hp < 200)
{
monster.hp = 0;
}
else
{
battleinfo += "敌方生命值不可能斩杀\n";
}
}
//else if (skillType == 1)
//{ new
// states.Add()
//}
}
}
}
class Skill
{
public string skillname;
public int skillTpye;
public int damage;
public int skill_overtime;
public int execute_dangmage;
public int mpcost;
public Skill(string name,int skillTpye, int damage, int skill_overtime, int execute_dangmage,int mpcost)
{
this.skillname = name;
this.skillTpye = skillTpye;
this.damage = damage;
this.skill_overtime = skill_overtime;
this.execute_dangmage = execute_dangmage;
this.mpcost = mpcost;
}
}
//class state
//{
// public int skill_time;
//}
class item
{
public string name;
public int add_Hp;
public int add_baseattack;
public int add_cure_time;
public int add_defense;
public int add_dex;
//public int add_mp;
public int cost;
public item(string name, int add_Hp,int add_baseattack, int add_cure_time, int add_defense, int add_dex,int cost)
{
this.name = name;
this.add_Hp = add_Hp;
this.add_baseattack = add_baseattack;
this.add_cure_time = add_cure_time;
this.add_defense = add_defense;
this.add_dex = add_dex;
this.cost = cost;
}
}
class special_item : item
{
public string[,] skilleffectArea;
public special_item(string name, int add_Hp,int add_baseattack, int add_cure_time, int add_defense, int add_dex,int cost) : base(name,add_Hp, add_baseattack, add_cure_time, add_defense, add_dex,cost)
{
skilleffectArea=new string[1,5];
}
}
class NPC
{
public List<item> itemlist = new List<item>();
public string name;
public int id;
public Pos pos=new Pos(logicControl.Width-3,logicControl.Height-30);
public NPC(string _name, int _id,int x,int y)
{
name = _name;
id = _id;
pos.x = x;
pos.y = y;
}
public void SetPos(int x, int y)
{
pos.x= x;
pos.y = y;
}
public virtual void OnTalk(Player player, out string text)
{
text = "这里是造换塔控制台!请输入购买的商品:1高浓缩口粮2高频共振刀3皮肤硬化剂4肾上腺激素";
salelist();
}
public void salelist()
{
item item1 = new item("高浓缩口粮", 200, 0, 0, 0, 0, 50);
item item2 = new item("高频共振刀", 0, 100, 0, 0, 0, 300);
item item3 = new item("皮肤硬化剂", 0, 0, 0, 50, 0, 300);
item item4 = new item("肾上腺激素", 0, 0, 0, 0, 20, 300);
itemlist.Add(item1);
itemlist.Add(item2);
itemlist.Add(item3);
itemlist.Add(item4);
}
public object AfterDisappear()
{
return null;
}
}
class Gate : NPC
{
public Gate(string name,int id,int x,int y) : base(name, id, x, y)
{
this.name = name;
this.id = id;
pos.x = x;
pos.y = y;
}
public override void OnTalk(Player player, out string text)
{
text = "这里是构造传送塔,必须使全区域安全后才能传送";
}
}
}
之后是关于场景中所有实例的位置坐标,就是所有游戏中人物、商店、传送点等等一切元素都应该拥有的坐标,将其单独命名成一个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace rpg
{
class Pos
{
public int x;
public int y;
public Pos(int x,int y)
{
this.x = x;
this.y = y;
}
}
好了,一个基本角色基类的定义已经完成了,接下来就是各种角色包括商店的子类定义了
class Player : Role
{
public int level;
public int exp=0;
public int money;
public Player(string name, int hp, int mp, int base_attackValue, int defense, int dex,int x,int y,int level,int money) : base(name, hp, mp, base_attackValue,defense, dex,x,y)
{
this.level = level;
this.money = money;
}
public void Levelupdate(Monster monster)
{
int exp_advancepoint = 100+(level-1)*100;
if(monster.name== "网络安全警卫")
{
this.exp += 100;
this.money += 300;
}
else if(monster.name == "硅基生物")
{
this.exp += 100;
this.money += 100;
}
if (exp >= exp_advancepoint)
{
level++;
dex = dex + 10;
base_attackValue = base_attackValue + 60;
exp = 0;
}
}
public override void use_skill(Player player, Monster monster, int skillType, ref string battleinfo)
{
if (player.skillList.ContainsKey(skillType))
{
if (skillType == 0)
{
monster.hp = monster.hp + monster.defense - player.skillList[0].damage-player.base_attackValue;
}
else if (skillType == 2&&player.mp>player.skillList[2].mpcost)
{
player.hp = player.hp + player.skillList[2].damage;
player.mp = player.mp - player.skillList[2].mpcost;
}
else if(skillType == 2 && player.mp <player.skillList[2].mpcost)
{
Console.WriteLine("血氧泵数量不足!");
}
else if (skillType == 3)
{
if (monster.hp < 200)
{
monster.hp = 0;
}
else
{
Console.WriteLine("敌方生命值不可能斩杀");
}
}
//else if (skillType == 1)
//{ new
// states.Add()
//}
}
}
public void PosReset()
{
pos.x = 2;
pos.x = 2;
}
}
接下来命名基于Role基类的敌人的角色类Monster
class Monster : Role
{
static Random randpos = new Random();
//public int exp_drow;
//public int money_drow;
public Monster(string name, int hp, int mp, int base_attackValue ,int defense, int dex,int x,int y) : base(name, hp, mp,base_attackValue, defense, dex,x,y)
{
}
static public Monster CreateSi_enemy()
{
Pos pos = logicControl.RandPos();
Monster monster = new Monster("硅基生物",100,100,0,20,15,pos.x,pos.y);
Skill skill = new Skill("硅素突刺", 0, 80, 0, 0,0);
monster.skillList.Add(0, skill);
return monster;
}
static public Monster create_safeguard()
{
Pos pos = logicControl.RandPos();
Monster monster = new Monster("网络安全警卫",300,100,0,20,20,pos.x,pos.y);
Skill skill = new Skill("快速斩击", 0, 80, 0, 0,0);
monster.skillList.Add(0, skill);
return monster;
}
static public Monster create_BOSS()
{
Monster monster = new Monster("高级安全警卫娜塔莎", 1000, 1000, 0,25, 40,10,10);
Skill skill = new Skill("重力子射线放出装置", 0, 200, 0, 0, 500);
//Skill skill1 = new Skill("死亡之刃", 0, 120, 0, 0, 0);
//monster.skillList.Add(0, skill1);
monster.skillList.Add(0, skill);
return monster;
}
public override void use_skill(Player player, Monster monster, int skillType,ref string battleinfo)
{
if (player.skillList.ContainsKey(skillType))
{
if (skillType == 0&&monster.mp>= monster.skillList[0].mpcost)
{
player.hp = player.hp + player.defense - monster.skillList[0].damage;
monster.mp = monster.mp - monster.skillList[0].mpcost;
}
else if (skillType == 2)
{
//player.hp = player.hp + player.skillList[2].damage;
}
else if (skillType == 3)
{
if (monster.hp < 200)
{
monster.hp = 0;
}
else
{
//battleinfo += "敌方生命值不可能斩杀\n";
Console.WriteLine("敌方生命值不可能斩杀");
}
}
else if (skillType ==4&&monster.mp>=monster.skillList[4].mpcost)
{
player.hp = player.hp + player.defense - monster.skillList[4].damage;
monster.mp = monster.mp - monster.skillList[4].mpcost;
}
else
{
Console.WriteLine("电池电量不足无法使用技能");
}
}
}
}
游戏中的各种数据元素定义完成了,接下来是一些回合制战斗机制,在玩家和怪物坐标重合时会进入下面的战斗逻辑
class under_canvasbattle
{
static Random randomnum = new Random();
static public void Roundbattle_set(int input, Player player, Monster monster,ref string battleinfo)
{
if (player.hp > 0 && monster.hp > 0)
{
int first_attack = randomnum.Next(1, 101);//先攻判断
int hero_slip = randomnum.Next(1, 101);//闪避判定
int devil_slip = randomnum.Next(1, 101);//闪避判定
//int devil_skill = randomnum.Next(1, 101);
///魔王先攻
if (first_attack > 50)
{
//battleinfo += "此回合先攻的是" + monster.name + "!\n";
//Console.WriteLine(battleinfo);
Console.WriteLine("此回合先攻的是{0}!",monster.name);
if (hero_slip > player.dex)
{
Console.WriteLine("{0}使用了{1}!", monster.name, monster.skillList[0].skillname);
monster.use_skill(player, monster,0,ref battleinfo);
//battleinfo += monster.name + "使用了" + monster.skillList[0].skillname;
//Console.WriteLine(battleinfo);
}
else
{
//battleinfo += player.name + "闪避成功!\n";
Console.WriteLine("{0}使用了{1}!{2}闪避成功!", monster.name, monster.skillList[0].skillname, player.name);
}
if (player.hp > 0 && input == 1)
{
if (devil_slip > monster.dex)
{
Console.WriteLine("{0}使用了{1}!", player.name, player.skillList[0].skillname);
player.use_skill(player, monster, 0, ref battleinfo);
//battleinfo += player.name + "使用了" + player.skillList[0].skillname + "!\n";
}
else
{
//battleinfo += monster.name + "闪避成功!\n";
Console.WriteLine("{0}使用了{1}!但是{2}闪避成功!", player.name, player.skillList[0].skillname, monster.name);
}
}
//else if (player.hp > 0 && input == 2)
//{
// player.useskill(player.player_skills, monster);
//}
else if (player.hp > 0 && input == 3)
{
player.use_skill(player,monster,2,ref battleinfo);
//battleinfo += player.name + "使用了" + player.skillList[2].skillname + "!\n";
Console.WriteLine("{0}使用了{1}!", player.name, player.skillList[2].skillname);
}
else if (player.hp <= 0)
{
Console.WriteLine("你死了,游戏结束!");
}
if (player.hp > 0 && input == 4)
{
if (devil_slip > monster.dex)
{
player.use_skill(player, monster, 3, ref battleinfo);
//battleinfo += player.name + "成功使用了" + player.skillList[3].skillname + "!\n";
Console.WriteLine("{0}使用了{1}!", player.name, player.skillList[3].skillname);
}
else
{
//battleinfo += monster.name + "闪避成功!\n";
Console.WriteLine("{0}使用了{1}!但是{2}闪避成功!", player.name, player.skillList[3].skillname, monster.name);
}
}
}
//
///、
///勇者先攻
else if (first_attack < 50)
{
// battleinfo += "此回合" + player.name + "先攻!\n";
Console.WriteLine("此回合先攻的是{0}!", player.name);
if (input == 1)
{
if (devil_slip > monster.dex)
{
player.use_skill(player, monster, 0, ref battleinfo);
Console.WriteLine("{0}使用了{1}!", player.name, player.skillList[0].skillname);
}
else
{
//battleinfo += monster.name + "闪避成功!\n";
Console.WriteLine("{0}使用了{1}!但是{2}闪避成功!", player.name, player.skillList[0].skillname, monster.name);
}
}
else if (input == 3)
{
player.use_skill(player, monster, 2, ref battleinfo);
Console.WriteLine("{0}使用了{1}!", player.name, player.skillList[2].skillname);
}
else if (input == 4)
{
if (devil_slip > monster.dex)
{
player.use_skill(player, monster, 3, ref battleinfo);
//battleinfo += player.name + "成功使用了" + player.skillList[3].skillname + "!\n";
Console.WriteLine("{0}使用了{1}!", player.name, player.skillList[3].skillname);
}
else
{
Console.WriteLine("{0}使用了{1}!但是{2}闪避成功!", player.name, player.skillList[3].skillname, monster.name);
}
}
if (monster.hp > 0)
{
if (hero_slip > player.dex)
{
Console.WriteLine("{0}使用了{1}!", monster.name, monster.skillList[0].skillname);
monster.use_skill(player, monster, 0, ref battleinfo);
}
else
{
Console.WriteLine("{0}使用了{1}!{2}闪避成功!", monster.name, monster.skillList[0].skillname, player.name);
}
}
}
}
else
{
if (player.hp <= 0)
{
battleinfo += "你死了,游戏结束\n";
}
else if (monster.hp <= 0)
{
battleinfo+=monster.name+"死了,胜利!\n";
}
}
}
}
此类的战斗函数在游戏框架总逻辑里会在玩家触碰敌人时被用到
就如开篇所讲的那样,画面在每次读取玩家操作后都会进行刷新,在操作之后,游戏逻辑框架会更新状态。首先是状态栏
static public void drawinfo(Player player)
{
//string s = string.Format("hp:{0}level:{1}atk:{2}$:{3}exp:{4}", player.hp, player.level, player.base_attackValue, player.money, player.exp);
//for (int i = 0; i < s.Length; ++i)
//{
// buffer[0, i] = Convert.ToString(s[i]);
//}
Console.WriteLine("=============================");
Console.WriteLine("姓名:{0} 生命值:{1}", player.name, player.hp);
Console.WriteLine("炭基电池电量:{0} 攻击力:{1}", player.mp, player.base_attackValue);
Console.WriteLine("防御:{0} 闪避:{1}", player.defense, player.dex);
Console.WriteLine("等级:{0} 经验值:{1}", player.level, player.exp);
Console.WriteLine("无机及有机材料:{0}", player.money);
Console.WriteLine("=============================");
//for(int i = 0; i < player.itemList.Count; i++)
//{
//}
}
其次是刷新核心、由这些代码构成,由四个函数构成,在每次操作时都会调用一次
static public void Draw_all()
{
Clear_map();
Create_mapedge();
DrawOther();
Refresh();
}
static public void Clear_map()//刷新函数之一
{
for (int i = 0; i < buffer.GetLength(0); i++)
{
for (int j = 0; j < buffer.GetLength(1); j++)
{
buffer[i, j] = " ";
}
}
}
static public void Create_mapedge()//创建边框
{
for (int i = 0; i < buffer.GetLength(0); i++)
{
buffer[0, i] = "##";
buffer[Height - 1, i] = "##";
}
for (int i = 0; i < buffer.GetLength(1); i++)
{
buffer[i, 0] = "##";
buffer[i, Width - 1] = "##";
}
}
static public void DrawOther()
{
buffer[player.pos.x,player.pos.y] ="雾";
for(int i = 0; i < monsterlist.Count; i++)
{
if (monsterlist[i].name == "硅基生物")
buffer[monsterlist[i].pos.x, monsterlist[i].pos.y] = "硅";
else if(monsterlist[i].name == "网络安全警卫")
{
buffer[monsterlist[i].pos.x, monsterlist[i].pos.y] = "警";
}else if(monsterlist[i].name == "高级安全警卫娜塔莎")
{
buffer[monsterlist[i].pos.x, monsterlist[i].pos.y] = "娜";
}
}
for (int i = 0; i < npcslist.Count; i++)
{
if (npcslist[i].name == "造换塔控制台")
{
buffer[npcslist[i].pos.x, npcslist[i].pos.y] = "台";
}
else if(npcslist[i].name == "构造传送塔")
{
buffer[npcslist[i].pos.x, npcslist[i].pos.y] = "门";
}
}
}
static void Refresh()
{
Console.Clear();
drawinfo(player);
for (int i = 0; i < buffer.GetLength(0); i++)
{
for (int j = 0; j < buffer.GetLength(1); j++)
{
Console.Write(buffer[i, j]);
}
Console.WriteLine();
}
drawbattleinfo();
}
static public void drawbattleinfo()
{
Console.WriteLine(massage_below);
}
以及战斗回合刷新,两个字符串用来显示任务和战斗信息
public static void GameOver()
{
game_over = true;
victory = false;
}
public static void OnStageClear()
{
game_over = true;
victory = true;
}
static void ClearStage()
{
game_over = false;
victory = false;
player.PosReset();
mission_target = "";
massage_below = "";
}
刷新,数据,战斗等元素凑齐之后,就可以用游戏整体运行逻辑将它们串接起来就行了,这里将设计好的三个关卡,依次放到了mian函数里,这样整个游戏就基本完成了。
这里是初始数据变量声明
class logicControl
{
static Random random = new Random();
public const int Width = 30;
public const int Height = 30;
public static string[,] buffer = new string[Width, Height];
//public static ConsoleCanvasString Canvas = new ConsoleCanvasString(Width, Height);
public static Player player = new Player("雾亥", 3000, 1000, 100, 25, 40, 3, 3, 1, 1000);
public static under_canvasbattle under_Canvasbattle = new under_canvasbattle();
public static List<Monster> monsterlist = new List<Monster>();
public static List<NPC> npcslist = new List<NPC>();
public static Skill normal_skill = new Skill("斩击", 0, 80, 0, 0, 0);
public static Skill overtime_skill = new Skill("抵近射击", 1, 20, 3, 0, 5);
public static Skill cure_myself = new Skill("纳米供给血氧泵", 2, -60, 0, 0, 10);
public static Skill execute_skill = new Skill("拟态重力子炮", 3, 0, 0, 200, 10);
public static NPC npc = new NPC("造换塔控制台", 0, Width - 3, Height - 3);
public static Gate gate = new Gate("构造传送塔", 0, 3, Height - 3);
static public string mission_target = "";
static public string massage_below = "";
static public string[,] gravity_weapon = new string[2, 5];
public static bool game_over;
public static bool victory;
static public void create_weapon()//用于显示大招效果,已弃用
{
for (int i = player.pos.x; i < gravity_weapon.GetLength(0); i++)
{
for (int j = player.pos.y; j < gravity_weapon.GetLength(1); j++)
{
gravity_weapon[i, j] = "■";
}
}
}
static public void create_weapon_destoryemeny()//配合大招的动画效果已弃用
{
for (int k = 0; k < monsterlist.Count; k++)
{
for (int i = player.pos.x-5; i < player.pos.x+5; i++)
{
for (int j = player.pos.y-5; j <player.pos.y+5; j++)
{
if (monsterlist[k].pos.x==j||monsterlist[k].pos.y == i)
{
player.Levelupdate(monsterlist[k]);
monsterlist.Remove(monsterlist[k]);
}
}
}
}
}
public static Pos RandPos()//用于随机生成人物位置
{
while (true)
{
Pos pos=new Pos(0,0);
pos.x= random.Next(2,Width-2);
pos.y = random.Next(2, Height -2);
if (buffer[pos.x,pos.y] ==" ")
{
return pos;
}
}
}
}
对于是否消灭完关卡敌人的判断
public bool IsPosEmpty(Pos pos)
{
if (pos.x < 1 || pos.y < 1 || pos.x >Width-1 || pos.y >Height-1)
{
return false;
}
for(int i=0;i<monsterlist.Count;i++)
{
if(pos.x==monsterlist[i].pos.x&&pos.y==monsterlist[i].pos.y)
return false;
}
for (int i = 0; i <npcslist.Count; i++)
{
if (pos.x == npcslist[i].pos.x && pos.y ==npcslist[i].pos.y)
return false;
}
if (pos.x == player.pos.x && pos.y ==player.pos.y)
{
return false;
}
return true;
}
控制玩家输入判定的核心函数,也是游戏控制的核心
static void Jud_MovePlayer()//移动角色
{
if (player.pos.x >1 &&player.pos.x <= Width - 1 && player.pos.y >1 && player.pos.y <=Height - 1)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.UpArrow)
{
player.pos.x -= 1;
}
else if (key.Key == ConsoleKey.DownArrow)
{
player.pos.x += 1;
}
else if (key.Key == ConsoleKey.LeftArrow)
{
player.pos.y -= 1;
}
else if (key.Key == ConsoleKey.RightArrow)
{
player.pos.y+= 1;
}
else
{
massage_below += "无法行动\n";
}
}
if (player.pos .x< 1 || player.pos.x >= Width - 1 || player.pos.y < 1 || player.pos.y >= Height - 1)
{
massage_below += "无法行动!\n";
}
//int next_pos = MapPos(next_x, next_y);
for (int i = 0; i < monsterlist.Count; i++)
{
if (player.pos.x == monsterlist[i].pos.x && player.pos.y == monsterlist[i].pos.y)
{
//massage_below = "遭遇" + monsterlist[i].name + "开始战斗:1、斩击,3、纳米供给血氧泵4、拟态重力子炮\n";
Console.WriteLine("遭遇{0}开始战斗:1、斩击,3、纳米供给血氧泵4、拟态重力子炮", monsterlist[i].name);
string c = Console.ReadLine();
int input = int.Parse(c);
while (input != 1 && input != 3 && input != 4)
{
//massage_below += "输入错误请重新输入\n";
Console.WriteLine("输入错误请重新输入");
string b = Console.ReadLine();
input = int.Parse(b);
}
while (player.hp > 0 && monsterlist[i].hp > 0)
{
under_canvasbattle.Roundbattle_set(input, player,monsterlist[i], ref massage_below);
input = -1;
//massage_below += "开始战斗:1、斩击,3、纳米供给血氧泵4、拟态重力子炮\n";
Console.WriteLine("继续开始战斗:1、斩击,3、纳米供给血氧泵4、拟态重力子炮");
string b = Console.ReadLine();
input = int.Parse(b);
while (input != 1 && input != 3 && input != 4)
{
//massage_below += "输入错误请重新输入\n";
Console.WriteLine("输入错误请重新输入");
string k= Console.ReadLine();
input = int.Parse(k);
}
}
if (player.hp>0)
{
player.Levelupdate(monsterlist[i]);
monsterlist.Remove(monsterlist[i]);
}
else if (monsterlist[i].hp > 0)
{
break;
}
}
}
for (int i = 0; i < npcslist.Count; i++)
{
if (npcslist[i].name == "造换塔控制台")
{
if (player.pos.x == npcslist[i].pos.x && player.pos.y == npcslist[i].pos.y)
{
string s;
npcslist[i].OnTalk(player, out s);
Console.WriteLine(s);
string c = Console.ReadLine();
int input = int.Parse(c);
while (input != 1 && input != 2 && input != 3 && input != 4)
{
//massage_below += "输入错误请重新输入\n";
Console.WriteLine("输入错误请重新输入");
string b = Console.ReadLine();
input = int.Parse(b);
}
if (player.money >= npcslist[i].itemlist[input - 1].cost)
{
player.base_attackValue += npcslist[i].itemlist[input - 1].add_baseattack;
player.hp += npcslist[i].itemlist[input - 1].add_Hp;
player.defense += npcslist[i].itemlist[input - 1].add_defense;
player.dex += npcslist[i].itemlist[input - 1].add_dex;
player.money -= npcslist[i].itemlist[input - 1].cost;
//Console.WriteLine("购买成功!");
massage_below += "购买成功\n";
}
else
{
massage_below += "购买失败\n";
//Console.WriteLine("购买成功!");
}
}
}
else if(npcslist[i].name == "构造传送塔")
{
if (player.pos.x == npcslist[i].pos.x && player.pos.y == npcslist[i].pos.y)
{
string s;
npcslist[i].OnTalk(player, out s);
massage_below += s;
}
}
}
}
关卡和运行主函数(游戏核心)
static public void level_1()
{
NPC npc1 = new NPC("造换塔控制台", 0, Width - 4, Height -4);
npcslist.Add(npc);
npcslist.Add(gate);
Monster monster1 = Monster.CreateSi_enemy();
Monster monster2 = Monster.create_safeguard();
Monster monster3 = Monster.CreateSi_enemy();
Monster monster4 = Monster.create_safeguard();
monsterlist.Add(monster1);
monsterlist.Add(monster2);
monsterlist.Add(monster3);
monsterlist.Add(monster4);
massage_below = "击败所有安全警卫和硅素生物\n";
}
static public void level_2()
{
massage_below= "";
NPC npc1 = new NPC("造换塔控制台", 0, Width - 4, Height - 4);
npcslist.Add(npc);
Monster monster1 = Monster.CreateSi_enemy();
Monster monster2 = Monster.create_safeguard();
Monster monster3 = Monster.CreateSi_enemy();
Monster monster4 = Monster.create_safeguard();
Monster monster5 = Monster.create_BOSS();
monsterlist.Add(monster1);
monsterlist.Add(monster2);
monsterlist.Add(monster3);
monsterlist.Add(monster4);
monsterlist.Add(monster5);
massage_below = "击败所有安全警卫和硅素生物迎战高级安全警卫娜塔莎\n";
}
static public void level_3()
{
massage_below = "";
NPC npc1 = new NPC("造换塔控制台", 0, Width - 4, Height - 4);
npcslist.Add(npc);
npcslist.Add(gate);
Monster monster1 = Monster.CreateSi_enemy();
Monster monster2 = Monster.create_safeguard();
Monster monster3 = Monster.CreateSi_enemy();
Monster monster4 = Monster.create_safeguard();
monsterlist.Add(monster1);
monsterlist.Add(monster2);
monsterlist.Add(monster3);
monsterlist.Add(monster4);
massage_below = "击败所有安全警卫和硅素生物\n";
}
public static void enemy_movement()
{
for(int i = 0; i < monsterlist.Count; i++)
{
if (monsterlist[i].pos.x < player.pos.x)
{
monsterlist[i].pos.x++;
}
else if (monsterlist[i].pos.y <player.pos.y)
{
monsterlist[i].pos.y++;
}
else if(monsterlist[i].pos.x > player.pos.x)
{
monsterlist[i].pos.x--;
}
else if(monsterlist[i].pos.y > player.pos.y)
{
monsterlist[i].pos.y--;
}
}
}
static void Main(string[] args)
{
player.skillList.Add(0,normal_skill);
player.skillList.Add(2, cure_myself);
player.skillList.Add(3, execute_skill);
Clear_map();
level_1();
Create_mapedge();
DrawOther();
Draw_all();
while (true)
{
Jud_MovePlayer();
Draw_all();
if (player.hp<=0|| (monsterlist.Count == 0 && player.pos.x == gate.pos.x && gate.pos.y == player.pos.y))
{
break;
}
}
if (player.hp > 0)
{
Clear_map();
level_3();
DrawOther();
Draw_all();
while (true)
{
Jud_MovePlayer();
enemy_movement();
Draw_all();
if (monsterlist.Count > 0)
{
Console.WriteLine("是否使用重力子射线装置?按y或n");
string fire = Console.ReadLine();
while (fire != "y" && fire != "n")
{
Console.WriteLine("输入错误,按y或n");
fire = Console.ReadLine();
}
if (fire == "y")
{
for (int i = 0; i < monsterlist.Count; i++)
{
player.Levelupdate(monsterlist[i]);
}
monsterlist.Clear();
}
}
if (player.hp <= 0 || (monsterlist.Count == 0 && player.pos.x == gate.pos.x && gate.pos.y == player.pos.y))
{
break;
}
}
}
if (player.hp > 0)
{
Clear_map();
level_2();
Create_mapedge();
DrawOther();
Draw_all();
while (true)
{
Jud_MovePlayer();
Draw_all();
if ((player.hp <= 0) ||monsterlist.Count == 0)
{
break;
}
}
if(player.hp > 0)
{
Console.WriteLine("你使超构造体恢复了秩序");
}
}
else
{
Console.WriteLine("游戏结束!");
}
Console.ReadKey();
}
}
到此大部分游戏框架都已完成,由于是初学者,有设计缺陷的话请指出,在下感激不尽。