Coding/Python

[python] 파이썬으둜 λ§Œλ“  μŠ€νƒ€ν¬λž˜ν”„νŠΈ

κΉ€μœ λ‹ˆμ½˜ 2021. 11. 3. 13:05

κ·Έλ ‡λ‹€,, κ³ λŒ€λ‘œ λ”°λΌμ³€λŠ”λ° μ—λŸ¬λ‚˜λŠ” λ―ΈμŠ€ν…Œλ¦¬,,

μ™œ?

 

 

μ–Έμ  κ°€ μ΄ν•΄ν•΄μ„œ λšλ”±λšλ”± κ³ μΉ  수 μžˆλŠ” λ‚  였길 바라며,,,,

 

μŠ€νƒ€λ„ λͺ¨λ₯΄κ² κ³  νŒŒμ΄μ¬λ„ λͺ¨λ₯΄κ² λ‹€~~~~~~~~~

 

from random import *


#일반유
class Unit:  #λΆ€λͺ¨ 클래슀
    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))


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))



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

    #μŠ€νŒ€νŒ© : 일정 μ‹œκ°„λ™μ•ˆ 곡격 속도을 증가, 체λ ₯ 10 κ°μ†Œ
    def stimpack(self):
        if self.hp >= 10:
            self.hp -= 10
            print("{0} : μŠ€νŒ€νŒ©μ„ μ‚¬μš©ν•©λ‹ˆλ‹€. (hp 10 κ°μ†Œ)".format(self.name))
        else:
            print("{0} : 체λ ₯이 λΆ€μ‘±ν•˜μ—¬ μŠ€νŒ€νŒ©μ„ μ‚¬μš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ".format(self.name))


#탱크
class Tank(AttackUnit):
    #μ‹œμ¦ˆλͺ¨λ“œ : 탱크λ₯Ό 지상에 κ³ μ •μ‹œμΌœ 더 높은 νŒŒμ›Œλ‘œ 곡격 κ°€λŠ₯. 이동 λΆˆκ°€
    seize_developed = False #μ‹œμ¦ˆλͺ¨λ“œ κ°œλ°œμ—¬λΆ€

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

    def set_seize_mode(self):

        #ν˜„μž¬ μ‹œμ¦ˆλ³΄λ“œκ°€ 아닐 λ•Œ --> μ‹œμ¦ˆλͺ¨λ“œ
        if Tank.seize_developed == 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





#λ‚  수 μžˆλŠ” κΈ°λŠ₯ 가진 클래슀
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))

#곡쀑 곡격 μœ λ‹› 클래슀
class FlyableAttackUnit(AttackUnit, Flyable): #이쀑상
    def __init__(self, name, hp, damage, flying_speed):
        AttackUnit.__init__(self, name, hp, 0, damage) #지상 μŠ€ν”Όλ“œλŠ” 0으둜 처
        Flyable.__init__(self, flying_speed)

    def move(self, location):
        print("[곡쀑 μœ λ‹› 이동]")
        self.fly(self.name, location)

#레이슀
class Wraith(FlyableAttackUnit):
    def __init__(self):
        FlyableAttackUnit.__init__("레이슀", 80, 20, 5)
        self.clocked = False #ν΄λ‘œν‚Ή λͺ¨λ“œ (ν•΄μ œμƒνƒœ)

    def clocking(self):
        if self.clocked == True: #ν΄λ‘œν‚Ή λͺ¨λ“œ --> ν•΄μ œ
            print("{0} : ν΄λ‘œν‚Ή λͺ¨λ“œλ₯Ό ν•΄μ œν•©λ‹ˆλ‹€. ".format(self.name))
            self.clocked = False

def game_start():
    print("[μ•Œλ¦Ό] μƒˆλ‘œμš΄ κ²Œμž„μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.")


def game_over():
    print("Player : gg") # good game
    print("[ν”Œλ ˆμ΄μ–΄] λ‹˜μ΄ κ²Œμž„μ—μ„œ 퇴μž₯ν•˜μ…¨μŠ΅λ‹ˆλ‹€.")


#μ‹€μ œ κ²Œμž„ 진행
game_start()

#마린 3개 생성
m1 = Marine()
m2 = Marine()
m3 = Marine()

#탱크 2개
t1 = Tank()
t2 = Tank()

#레이슀 1개
w1 = Wraith()

#μœ λ‹› 일괄 관리
attack_units =[]
attack_units.append(m1)
attack_units.append(m2)
attack_units.append(m3)
attack_units.append(t1)
attack_units.append(t2)
attack_units.append(w1)


# μ „κ΅° 이동
for unit in attack_units:
    unit.move("1μ‹œ")

#탱크 μ‹œμ¦ˆλͺ¨λ“œ 개발
Tank.seize_developed = True
print("[μ•Œλ¦Ό] 탱크 μ‹œμ¦ˆ λͺ¨λ“œ 개발이 μ™„λ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.")

#곡격 λͺ¨λ“œ μ€€λΉ„(마린: μŠ€νŒ€νŒ©, 탱크 : μ‹œμ¦ˆλͺ¨λ“œ, 레이슀: ν΄λ‘œν‚Ή)
for unit in attack_units:
    if isinstance(unit, Marine):
        unit.stimpack()
    elif isinstance(unit,Tank):
        unit.set_seize_mode()
    elif isinstance(unit, Wraith):
        unit.clocking()

#μ „κ΅° 곡격
for unit in attack_units:
    unit.attack("1μ‹œ")

#μ „κ΅° ν”Όν•΄
for unit in attack_units:
    unit.damaged(randint(5, 21)) #곡격은 랜덀으둜 λ°›μŒ (5~21)

game_over()