[파이썬] 기초 공부, 클래스

 


 무슨 말인지 하나도 모르곘다. 

그냥 따라하고 있는데 이해가... 

#클래스 ★★★★★ 매우중요

1. 클래스 이전에 기초


name = "마린" #유닛이름
hp = 40 #유닛 체력
damage = 5  #유닛 공격력

print("{0} 유닛이 생성되었습니다." .format(name))
print("체력 {0}, 공격력 {1} \n" .format(hpdamage))

#탱크 : 공격 유닛, 탱크, 무기 포, 일반모드 /시즈모드
tank_name = "탱크"
tank_hp = 150
tank_damage = 35

print("{0} 유닛이 생성되었습니다." .format(tank_name))
print("체력 {0}, 공격력 {1} \n" .format(tank_hptank_damage))

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


attack(name"1시"damage)
attack(tank_name"1시"tank_damage)










2. 클래스로 변경


class Unit:
    def __init__(selfnamehpdamage):
        self.name = name
        self.hp = hp
        self.damage = damage
        print("{0} 유닛이 생성되었습니다." .format(self.name))
        print("체력 {0}, 공격력 {1}" .format(self.hpself.damage))

marine1 = Unit("마린"405)
marine2 = Unit("마린"405)
tank = Unit("탱크"15035)







3. __init__    #객체 생성자


4. 멤버변수 등... - 진짜 모르겠다. 이해가 안간다..


wraith1 = Unit("레이스"805)
print("유닛 이름 : {0}, 공격력 : {1}" .format(wraith1.namewraith1.damage))

#a마인드 컨트롤 : 상대방 유닛을 내것으로 만드는 것 
wraith2 = Unit("빼앗은 레이스"805)
wraith2.clocking = True

if wraith2.clocking == True :
    print("{0}는 현재 클로킹 상태입니다." .format(wraith2.name))









5. 메소드 



class Unit:
    def __init__(selfnamehpdamage):
        self.name = name  #멤버변수 클래내에서 정의된 변수
        self.hp = hp
        self.damage = damage
        print("{0} 유닛이 생성되었습니다." .format(self.name))
        print("체력 {0}, 공격력 {1}" .format(self.hpself.damage))

class AttackUnit:
    def __init__(selfnamehpdamage):
        self.name = name  #멤버변수 클래내에서 정의된 변수
        self.hp = hp
        self.damage = damage

    def attack(selflocation):
        print("{0} : {1} 방향으로 적군을 공격합니다. [공격력 : {2}]" .format(self.namelocationself.damage))
    
    def damaged(selfdamage):
        print("{0} : {1} 데미지를 입었습니다." .format(self.namedamage))
        self.hp -= damage
        print("{0} : 현재 체력은 {1} 입니다." .format(self.nameself.hp))
        if self.hp <=0:
            print("{0} : 파괴되었습니다." .format(self.name))
    
#파이어뱃, 공격유닛, 화염방사기
firebat1 = AttackUnit("파이어벳"5016)
firebat1.attack("5시")

#공격을 2번 받음
firebat1.damaged(25)
firebat1.damaged(25)






6. 상속

#일반 유닛
class Unit:
    def __init__(selfnamehp):
        self.name = name  
        self.hp = hp

#공격유닛
class AttackUnit(Unit):  #Unit 클래스 변수 내용 상속
    def __init__(selfnamehpdamage):
        Unit.__init__(selfnamehp)
        self.damage = damage

    def attack(selflocation):
        print("{0} : {1} 방향으로 적군을 공격합니다. [공격력 : {2}]" .format(self.namelocationself.damage))
    
    def damaged(selfdamage):
        print("{0} : {1} 데미지를 입었습니다." .format(self.namedamage))
        self.hp -= damage
        print("{0} : 현재 체력은 {1} 입니다." .format(self.nameself.hp))
        if self.hp <=0:
            print("{0} : 파괴되었습니다." .format(self.name))
    
#파이어뱃, 공격유닛, 화염방사기
firebat1 = AttackUnit("파이어벳"5016)
firebat1.attack("5시")

#공격을 2번 받음
firebat1.damaged(25)
firebat1.damaged(25)








7. 다중상속


#일반 유닛
class Unit:
    def __init__(selfnamehp):
        self.name = name  
        self.hp = hp

#공격유닛
class AttackUnit(Unit):  #Unit 클래스 변수 내용 상속
    def __init__(selfnamehpdamage):
        Unit.__init__(selfnamehp)
        self.damage = damage

    def attack(selflocation):
        print("{0} : {1} 방향으로 적군을 공격합니다. [공격력 : {2}]" .format(self.namelocationself.damage))
    
    def damaged(selfdamage):
        print("{0} : {1} 데미지를 입었습니다." .format(self.namedamage))
        self.hp -= damage
        print("{0} : 현재 체력은 {1} 입니다." .format(self.nameself.hp))
        if self.hp <=0:
            print("{0} : 파괴되었습니다." .format(self.name))

#비행가능 유닛
class Flyable:
    def __init__(selfflying_speed):
        self.flying_speed = flying_speed

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

#공중 공격 유닛
class FlyableAttackUnit(AttackUnit,Flyable):
    def __init__(selfnamehpdamageflying_speed):
        AttackUnit.__init__(selfnamehpdamage)
        Flyable.__init__(selfflying_speed)

#발키리, 공중공격 유닛, 한번에 14발 발사
valkyrie = FlyableAttackUnit("발키리"20065)
valkyrie.fly(valkyrie.name"3시")

    





8. 메소드 오버라이딩


#일반 유닛
class Unit:
    def __init__(selfnamehpspeed):
        self.name = name  
        self.hp = hp
        self.speed = speed

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

#공격 유닛
class AttackUnit(Unit):  #Unit 클래스 변수 내용 상속
    def __init__(selfnamehpspeeddamage):
        Unit.__init__(selfnamehpspeed)
        self.damage = damage

    def attack(selflocation):
        print("{0} : {1} 방향으로 적군을 공격합니다. [공격력 : {2}]" .format(self.namelocationself.damage))
    
    def damaged(selfdamage):
        print("{0} : {1} 데미지를 입었습니다." .format(self.namedamage))
        self.hp -= damage
        print("{0} : 현재 체력은 {1} 입니다." .format(self.nameself.hp))
        if self.hp <=0:
            print("{0} : 파괴되었습니다." .format(self.name))
    

#비행가능 유닛
class Flyable:
    def __init__(selfflying_speed):
        self.flying_speed = flying_speed

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

#공중 공격 유닛
class FlyableAttackUnit(AttackUnit,Flyable):
    def __init__(selfnamehpdamageflying_speed):
        AttackUnit.__init__(selfnamehp0damage#지상스피드는 0으로 처리한다. 
        Flyable.__init__(selfflying_speed)


    def move(selflocation):
        print("[공중 유닛 이동]")
        self.fly(self.namelocation)

#벌쳐 : 지상 유닛, 기동성이 좋음
vulture = AttackUnit("벌쳐"801020)

#배틀크루져 : 공중 유닛, 체력 좋음, 공격력 좋음 
battlecruiser = FlyableAttackUnit("배틀크루져"500253)

vulture.move("11시")
# battlecruiser.fly(battlecruiser.name, "9시")
battlecruiser.move("9시")








9. Pass


#건물 설립
class BuildingUnit(Unit):
    def __init__(selfnamehplocation):
        pass #일단은 넘어간다. 

#서플라이 디폿 : 건물, 1개 건물 = 8 유닛 생성
supply_depot = BuildingUnit("서플라이 디폿"500"7시")


def game_start():
    print("[알림] 새로운 게임을 시작합니다.")

def game_over():
    pass #일단 코드가 완성안되도 넘어간다. 

game_over()
game_start()






10. super #상속시 unit 대신 사용할수 있으나 다중상속은 어려움

class Unit:
    def __init__(self):
        print("Unit 생성자")

class Flyable:
    def __init__(self):
        print("Flyable 생성자")

class FlaybaleUnit(UnitFlyable):
    def __init__(self):
        super().__init__()

#드랍쉽
dropship = FlaybaleUnit()





#건물 설립
class BuildingUnit(Unit):
    def __init__(selfnamehplocation):
        # Unit.__init__(self, name, hp, 0)
        super().__init__(namehp0)  
#Unit 대신 super를 사용한다. 단 다중 상속에서는 어렵다. 
        self.location = location






댓글 쓰기

0 댓글