欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-18 14:45:40
...

目前的效果图:

用c#与数据库做一个小游戏(二)
用c#与数据库做一个小游戏(二)
注:背景网图,侵删。

已完成的功能:人物的打怪升级(触发器自动修改属性),掉落物品,金钱,背包,商店购买,回城回血(进度条),背景音乐。

背包:

  public void bag_add(string goods)//检查并插入背包中

        {
            sql.Open();
            string bag_check = "select *from bag where bag_name = '" + goods + "'";
            SqlCommand command_bagcheck = new SqlCommand(bag_check, sql);
            SqlDataReader reader = command_bagcheck.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Close();
                string bag_add = "update bag set number+=1 where bag_name ='" + goods+"'";
                SqlCommand command_bagadd = new SqlCommand(bag_add, sql);
                try
                {
                    command_bagadd.ExecuteNonQuery();

                }
                catch
                {
                    MessageBox.Show("更新物品数量失败");
                }
                finally
                {
                    sql.Close();
                  
                }
            }
            else
            {
                reader.Close();
                string bag_insert = "insert into bag values ('" + goods + "'," + 1 + ")";
                SqlCommand command_insert = new SqlCommand(bag_insert, sql);
                try
                {
                    command_insert.ExecuteNonQuery();
                }
                catch
                {
                    MessageBox.Show("插入物品失败");
                }
                finally
                {
                    sql.Close();
                }
            }
        }

商店:

        public int Tiqu_shop_money(string shop_name)//提取商店物品金钱
        {
            sql.Open();
            string strsql_money = "select money from shop where itemname ='" + shop_name + "'";
            SqlCommand Tiqu_shop_wuli_command = new SqlCommand(strsql_money, sql);
            SqlDataReader Tiqu_shop_money_reader = Tiqu_shop_wuli_command.ExecuteReader();
            while (Tiqu_shop_money_reader.Read())
            {
                shop_money = Tiqu_shop_money_reader.GetInt32(Tiqu_shop_money_reader.GetOrdinal("money"));
            }
            sql.Close();
            Tiqu_shop_money_reader.Close();
            return shop_money;
        }
 public int losemoney(string shop_name)//购买物品减少金钱
        {
            SSHOP shop1 = new SSHOP();
            PERSON person = new PERSON();
            int allmoney = person.Tiqu_person_money();
            int lose_money = shop1.Tiqu_shop_money(shop_name);
            if(allmoney>=lose_money)
            {
              sql.Open();
            string lose_money1 = "update person set person_money-="+lose_money;
            SqlCommand command_losemoney = new SqlCommand(lose_money1, sql);
            try
            {
                command_losemoney.ExecuteNonQuery();

            }
            catch
            {
                MessageBox.Show("减少金钱失败");
            }
            finally
            {
                sql.Close();
                  
            }
              return 1;
            }
            else
            {

                return 0;
            }
            
           
        }