Coding/Python

[python] ํด๋ž˜์Šค(๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ)

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 3. 11:36

๊ณต๋ถ€ํ•  ๊ฒƒ์€ ๋งŽ๊ณ , 

์ดํ•ด๋Š” ์ž˜ ์•ˆ๋˜๊ณ , 

์ •๋ฆฌํ•˜๊ธฐ์—” ์–ด๋ ต๊ธฐ์—,

ํ•„๊ธฐ๋ฅผ ๊ทธ๋ƒฅ ๋ณต๋ถ™ํ•ฉ๋‹ˆ๋‹ค. 

 

 

#์ผ๋ฐ˜์œ ๋‹›
class Unit: #๋ถ€๋ชจ ํด๋ž˜์Šค
def __init__(self, name, hp, speed):
self.name = name
self.hp = hp
self.speed = speed

def move(self, location):
print("[์ง€์ƒ ์œ ๋‹› ์ด๋™]")
print("{0} : {1} ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. [์†๋„ {2}]"\
.format(self.name, location, self.speed))


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


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


# ๋“œ๋ž์‰ฝ: ๊ณต์ค‘ ์œ ๋‹›, ์ˆ˜์†ก๊ธฐ. ๋งˆ๋ฆฐ / ํŒŒ์ด์–ด๋ฑƒ / ํƒฑํฌ ๋“ฑ์„ ์ˆ˜์†ก. ๊ณต๊ฒฉ ๊ธฐ๋Šฅ X

#๋‚  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ ๊ฐ€์ง„ ํด๋ž˜์Šค
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)


# ์ž์‹ ํด๋ž˜์Šค์˜ ๋งค์†Œ๋“œ๋ฅผ ์“ฐ๊ณ ์‹ถ์„๋•Œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์ด๋ผ๊ณ  ํ•จ


#๋ฒŒ์ณ : ์ง€์ƒ ์œ ๋‹›, ๊ธฐ๋™์„ฑ์ด ์ข‹์Œ
vulture = AttackUnit("๋ฒŒ์ณ", 80, 10, 20)

#๋ฐฐํ‹€ ํฌ๋ฃจ์ €: ๊ณต์ค‘ ์œ ๋‹›, ์ฒด๋ ฅ ์ข‹๊ณ , ๊ณต๊ฒฉ๋ ฅ๋„ ์ข‹์Œ
battlecruiser = FlyableAttackUnit("๋ฐฐํ‹€ํฌ๋ฃจ์ €", 500, 25, 3)

vulture.move("11์‹œ")
battlecruiser.move("9์‹œ")