IT 공부/python

[ python ] 프로젝트 starcraft 개인공부

JONGI-N CHOI 2020. 12. 9. 13:43

기본 Unit Classs

class Unit:
    # init는 생성자로써, 객체가 만들어질 때 자동으로 생성
    def __init__(self, name, hp, speed):
        self.name = name
        self.hp = hp
        self.speed = speed
        print("{0} 유닛이 생성되었습니다.".format(name))

    def move(self, location):
        print("[지상 유닛 이동]")
        print("{0} :{1} 방향으로 이동합니다. [속도 {2}]"
              .format(self.name, location, self.speed))

    def damaged(self, damage):
        print("{0} : {1} 데미지를 입었습니다".format(self.name, damage))
        self.hp -= damage
        print("{0} : 현재 체력은 {1} 입니다.".format(self.name, self.hp))
        if self.hp <= 0:
            print("{0} : 파괴되었습니다.".format(self.name))

 

AttackUnit Class

class AttackUnit(Unit):

    def __init__(self, name, hp, speed, damage):
        Unit.__init__(self, name, hp, speed)
        self.damage = damage

    def attack(self, location):
        print("{0} : {1} 방향으로 적군을 공격합니다. [공격력 {2}]"
              .format(self.name, location, self.damage))

Marine Class

class Marine(AttackUnit):
    def __init__(self):
        AttackUnit.__init__(self, "마린", 40, 1, 5)

        # 스팀팩
    def stimpack(self):
        if self.hp > 10:
            self.hp -= 10
            print("{0} : 스팀팩을 사용합니다. (hp 10 감소)".format(self.name))
        else:
            print("{0} : 체력이 부족하여 스팀팩을 사용하지 않습니다.".format(self.name))

Tank Class

class Tank(AttackUnit):
    # 시즈모드
    seize_developed = False

    def __init__(self):
        AttackUnit.__init__(self, "탱크", 150, 1, 35)
        self.seize_mode = False

    def set_seize_mode(self):
        if Tank.seize_developed == False:
            return
        # 시즈모드 아닐 때 -> 시즈모드
        if self.seize_mode == False:
            print("{0} : 시즈모드로 전환합니다.".format(self.name))
            self.damage *= 2
            self.seize_mode = True

        # 시즈모드 일 때 -> 시즈모드 해제
        else:
            print("0 : 시즈모드를 해제합니다. ".format(self.name))
            self.damage /= 2
            self.seize_mode = False

Flyble Class

class Flyable:
    def __init__(self, flying_speed):
        self.flying_speed = flying_speed

    def fly(self, name, location):
        print("{0} : {1} 방향으로 날아갑니다. [속도 {2} ]"
              .format(name, location, self.flying_speed))

FlyableAttackUnit Class

class FlyableAttackUnit(AttackUnit, Flyable):
    def __init__(self, name, hp, damage, flying_speed):
        AttackUnit.__init__(self, name, hp, 0, damage)  # 지상 speed 0
        Flyable.__init__(self, flying_speed)

    def move(self, location):
        print("[공중 유닛 이동]")
        self.fly(self.name, location)

Wraith Class

class Wraith(FlyableAttackUnit):
    def __init__(self):
        FlyableAttackUnit.__init__(self, "레이스", 80, 20, 5)
        self.clocked = False

    def clocking(self):
        if self.clocked == True:
            print("{0} : 클로킹 모드를 해제합니다.".format(self.name))
            self.clocked = False

        else:
            print("{0} : 클로킹 모드를 설정합니다.".format(self.name))
            self.clocked = True

 

 

 

전체 class 구성도

AttackUnit는 Unit를 상속받고  Marine 과 Tank 유닛이 AttackUnit을 상속받는다. 

Marine은 stimpack기능을 추가 Tank는 seize_mode를 추가

FlyableAttackUnit은 Flyable 과 AttackUnit 을 다중 상속 받고, Wraith 는 FlyableAttackUnit를 상속받는다. 

Wraith는 clocking 기능을 추가

 

참고자료 <youtube, 나도코딩 >

www.youtube.com/watch?v=kWiCuklohdY&t=12932s&ab_channel=%EB%82%98%EB%8F%84%EC%BD%94%EB%94%A9