Python 뿌시기

[Python 문법 6] - 클래스를 이용한 스타크래프트 만들기

pjw250 2024. 9. 26. 23:08

### 스타크래프트 게임 클래스 만들기


from random import *

class Unit:
    def __init__(self,name,hp, speed):
        self.name = name
        self.hp = hp
        self.speed = speed
        print('{0} 유닛이 생성되었습니다.'.format(name)) # self.name 써도 무방

    def move(self, location):
        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)
    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):
    def __init__(self):
        AttackUnit.__init__(self,'탱크',150,1,35)
        self.seize_mode = False
   
    seize_developed = 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

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)
        Flyable.__init__(self, flying_speed)
    def move(self, location):
        self.fly(self.name, location)

# 레이스

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

def game_start():
    print('[알림] 새로운 게임을 시작합니다.')
def game_over():
    print('Player : gg')
    print('[Player] 님이 게임에서 퇴장하셨습니다.')


### GAME START


m1 = Marine()
t1 = Tank()
w1 = Wraith()

attack_units = []
attack_units.append(m1)
attack_units.append(t1)
attack_units.append(w1)

for unit in attack_units:
    unit.move('1h direction')

Tank.seize_developed = True
print('[Announced] Research complete, seize mode')

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('1h direction')

for unit in attack_units:
    unit.damaged(randint(5,21))

game_over()

 

* 출처 나도코딩 [파이썬 코딩 무료 강의 (기본편)]

파이썬 코딩 무료 강의 (기본편) - 6시간 뒤면 여러분도 개발자가 될 수 있어요 [나도코딩] (youtube.com)