Coding/Python

[python] ํด๋ž˜์Šค(๋‹ค์ค‘์ƒ์†)

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

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


class AttackUnit(Unit): #์ž์‹ ํด๋ž˜์Šค
def __init__(self, name, hp, damage):
Unit.__init__(self, name, hp)
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))

#๋ฉ”๋”•: ์˜๋ฌด๋ณ‘

#ํŒŒ์ด์–ด๋ฑƒ : ๊ณต๊ฒฉ ์œ ๋‹›, ํ™”์—ผ ๋ฐฉ์‚ฌ๊ธฐ
firebat1 = AttackUnit("ํŒŒ์ด์–ด๋ฑƒ", 50, 16)
firebat1.attack("5์‹œ")

#๊ณต๊ฒฉ ๋‘ ๋ฒˆ ๋ฐ›๋Š”๋‹ค๊ณ  ๊ฐ€์ •
firebat1.damaged(25)
firebat1.damaged(25)

#๋‹ค์ค‘ ์ƒ์† : ๋ถ€๋ชจ๊ฐ€ ๋‘˜. ๊ณต์ค‘ ์œ ๋‹›์„ ๋งŒ๋“ค๋ฉด ์ผ๋ฐ˜์  ์œ ๋‹›์˜ ํŠน์„ฑ๊ณผ ๋‚  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ์–ด์•ผ.

# ๋“œ๋ž์‰ฝ: ๊ณต์ค‘ ์œ ๋‹›, ์ˆ˜์†ก๊ธฐ. ๋งˆ๋ฆฐ / ํŒŒ์ด์–ด๋ฑƒ / ํƒฑํฌ ๋“ฑ์„ ์ˆ˜์†ก. ๊ณต๊ฒฉ ๊ธฐ๋Šฅ 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, damage)
Flyable.__init__(self, flying_speed)


#๋ฐœํ‚ค๋ฆฌ : ๊ณต์ค‘ ๊ณต๊ฒฉ ์œ ๋‹›, ํ•œ ๋ฒˆ์— 14๋ฐœ ๋ฏธ์‚ฌ์ผ ๋ฐœ์‚ฌ
valkyrie = FlyableAttackUnit("๋ฐœํ‚ค๋ฆฌ", 200, 6, 5)
valkyrie.fly(valkyrie.name, "3์‹œ")

 

 

 

 

 

 

ใ…Žใ…Ž,,,,,,

์–ด๋ ต๋„ค