用c#与数据库做一个文字小游戏(一)

计划

人物系统(人物表)
{
人物升级,人物属性
}
怪物系统(怪物表)
{
怪物名字,属性,掉落的东西
}
背包系统
{
物品名,物品数量。
}

装备系统
{
装备属性
}

宠物系统

第一个系统,人物系统准备。

人物必然需要升级与攻击力,用数据库后触发器,当经验值满足一定条件时,等级与攻击力增加

//修改后的

ALTER  TRIGGER [dbo].[trigger_update] 
on   [dbo].[PERSON]
after update
as 
declare  @a int ;
declare  @b int ;

 set @a = (select  empirical from [dbo].[PERSON] )
 set @b=  (select  Grade from [dbo].[PERSON] ) 
if  (@a > = (@b*10))
begin 
update [dbo].[PERSON]
set  Grade=Grade+1,Empirical=0
WHERE  Grade=(SELECT Grade FROM deleted)and Empirical=(SELECT Empirical FROM deleted)
END

ALTER  TRIGGER [dbo].[trigger_update1] 
on   [dbo].[PERSON]
after update
as 
if  update (grade)
begin 
update [dbo].[PERSON]
set  wuli=wuli+1
WHERE  wuli=(SELECT wuli FROM deleted)
END
为了方便对战等操作把人物与怪物的函数分别写入person monster类中

PERSON类

 public int person_hp,person_grade,person_wuli;
        public int Tiqu_person_hp()//提取人物hp
        {
            sql.Open();
            string strsql_hp = "select hp from person where name ='红凯'";
            SqlCommand Tiqu_person_hp_command = new SqlCommand(strsql_hp, sql);
            SqlDataReader Tiqu_person_hp_reader = Tiqu_person_hp_command.ExecuteReader();
            while (Tiqu_person_hp_reader.Read())
            {
                person_hp = Tiqu_person_hp_reader.GetInt32(Tiqu_person_hp_reader.GetOrdinal("hp"));
            }
            sql.Close();
            Tiqu_person_hp_reader.Close();
            return person_hp;
        }
        public int Tiqu_person_grade()//提取人物等级
        {
            sql.Open();
            string strsql_grade = "select grade from person where name ='红凯'";
            SqlCommand Tiqu_person_grade_command = new SqlCommand(strsql_grade, sql);
            SqlDataReader Tiqu_person_grade_reader = Tiqu_person_grade_command.ExecuteReader();
            while (Tiqu_person_grade_reader.Read())
            {
                person_grade = Tiqu_person_grade_reader.GetInt32(Tiqu_person_grade_reader.GetOrdinal("grade"));
            }
            sql.Close();
            Tiqu_person_grade_reader.Close();
            return person_grade;
        }
        public int Tiqu_person_wuli()//提取人物攻击力
        {
            sql.Open();
            string strsql_wuli = "select wuli from person where name ='红凯'";
            SqlCommand Tiqu_person_wuli_command = new SqlCommand(strsql_wuli, sql);
            SqlDataReader Tiqu_person_wuli_reader = Tiqu_person_wuli_command.ExecuteReader();
            while (Tiqu_person_wuli_reader.Read())
            {
                person_wuli = Tiqu_person_wuli_reader.GetInt32(Tiqu_person_wuli_reader.GetOrdinal("wuli"));
            }
            sql.Close();
            Tiqu_person_wuli_reader.Close();
            return person_wuli;
        }
        public int attack()//战斗
        {
            MONSTER mONSTER = new MONSTER();
            int person_hp, person_wuli, monster_wuli, monster_hp;
            person_hp = Tiqu_person_hp();
            person_wuli = Tiqu_person_wuli();
            monster_hp = mONSTER.Tiqu_monster_hp();
            monster_wuli = mONSTER.Tiqu_monster_wuli();
            while (monster_hp >= 0 && person_hp >= 0)
            {
                person_hp -= monster_wuli;
                monster_hp -= person_wuli;
                if (monster_hp <= 0)
                {
                    break;
                }            
                else if(person_hp<=0)
                {
                 return 0;
                }                                                      
            }
            return 1; 
            
        }
    }

monster类:

   public int Tiqu_monster_hp()//提取怪物hp
        {
            sql.Open();
            string strsql_hp = "select hp from monster where name ='小蛐蛐'";
            SqlCommand Tiqu_monster_hp_command = new SqlCommand(strsql_hp, sql);
            SqlDataReader Tiqu_monster_hp_reader = Tiqu_monster_hp_command.ExecuteReader();
            while (Tiqu_monster_hp_reader.Read())
            {
               monster_hp = Tiqu_monster_hp_reader.GetInt32(Tiqu_monster_hp_reader.GetOrdinal("hp"));
            }
            sql.Close();
            Tiqu_monster_hp_reader.Close();
            return monster_hp;
        }
        public int Tiqu_monster_wuli()//提取怪物攻击力
        {
            sql.Open();
            string strsql_wuli = "select wuli from monster where name ='小蛐蛐'";
            SqlCommand Tiqu_monster_wuli_command = new SqlCommand(strsql_wuli, sql);
            SqlDataReader Tiqu_monster_wuli_reader = Tiqu_monster_wuli_command.ExecuteReader();
            while (Tiqu_monster_wuli_reader.Read())
            {
                monster_wuli = Tiqu_monster_wuli_reader.GetInt32(Tiqu_monster_wuli_reader.GetOrdinal("wuli"));
            }
            sql.Close();
            Tiqu_monster_wuli_reader.Close();
            return monster_wuli;
        }
        public string Tiqu_monster_equipment()//提取怪物装备
        {
            sql.Open();
            string strsql_equipment = "select equipment from monster where name ='小蛐蛐'";
            SqlCommand Tiqu_monster_equipment_command = new SqlCommand(strsql_equipment, sql);
            SqlDataReader Tiqu_monster_equipment_reader = Tiqu_monster_equipment_command.ExecuteReader();
            while (Tiqu_monster_equipment_reader.Read())
            {
                monster_equipment = Tiqu_monster_equipment_reader.GetString(Tiqu_monster_equipment_reader.GetOrdinal("equipment"));
            }
            sql.Close();
            Tiqu_monster_equipment_reader.Close();
            return monster_equipment;
        }

持续更新。。。

你可能感兴趣的:(c#入门,数据库入门)