Press "Enter" to skip to content

06.1: Tworzenie pozostałych klas

Cele lekcji

Po zakończeniu tej lekcji…

  • Nie nauczysz się niczego nowego. Jednak musimy utworzyć pozostałe klasy, aby w końcu móc zagrać w grę.

 

Kolejne klasy

W tej lekcji utworzymy pozostałe klasy wymagane w grze.

Przy okazji: programiści niekiedy nazywają te klasy „klasami biznesowymi” lub „obiektami biznesowymi”. I choć tworzymy grę, a nie aplikację biznesową, to będziemy się posługiwać tymi nazwami.

 

Etap 1: Uruchom aplikację Visual Studio i otwórz swoje rozwiązanie.

 

Etap 2: W przeglądarce Solution Explorer prawym przyciskiem myszy kliknij projekt Engine, wybierz polecenie Add i wskaż pozycję Class.

Wprowadź nazwę klasy (HealingPotion) i dodaj jej właściwości tak samo jak w przypadku klasy Player.

Skopiuj poniższy kod, aby mieć pewność, że kod jest bezbłędny. Wielkości liter są szczególnie ważne podczas używania klas, ponieważ w języku C# rozróżniane są małe i wielkie litery.

Rozszyfrowanie zastosowanie niektórych właściwości może nie być łatwe, ale omówię je wszystkie w kolejnych lekcjach.

 

HealingPotion.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
    public class HealingPotion
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string NamePlural { get; set; }
        public int AmountToHeal { get; set; }
    }
}

 

Item.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
    public class Item
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string NamePlural { get; set; }
    }
}

 

Location.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
    public class Location
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

 

Monster.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
    public class Monster
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int MaximumHitPoints { get; set; }
        public int CurrentHitPoints { get; set; }
        public int MaximumDamage { get; set; }
        public int RewardExperiencePoints { get; set; }
        public int RewardGold { get; set; }
    }
}

 

Quest.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
    public class Quest
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int RewardExperiencePoints { get; set; }
        public int RewardGold { get; set; }
    }
}

 

Weapon.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
    public class Weapon
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string NamePlural { get; set; }
        public int MinimumDamage { get; set; }
        public int MaximumDamage { get; set; }
    }
}

 

Podsumowanie

W tej lekcji nie dowiedzieliśmy się niczego nowego, jednak aby w grze cokolwiek się działo, trzeba było dodać powyższe klasy.

 

Łącza do tej lekcji

Kod źródłowy w serwisie GitHub

Kod źródłowy w serwisie Dropbox

    Leave a Reply

    Your email address will not be published. Required fields are marked *