패스트캠퍼스 한 번에 끝내는 파이썬 웹 개발 초격차 패키지 pt2 실습문제

정영찬·2022년 2월 2일
0

player 클래스 구현

1) 속성: 닉네임, 미네랄, 가스 , 유닛리스트

이때 유닛리스트는 문자열이 아니라 유닛클래스로부터 만들어진 객체이다.

2) 메서드:생산하기
produce(이름, 미네랄, 가스 ,체력, 방어막, 공격력)

  • player의 미네랄과 가스가 충분한 경우
    ->유닛 객체를 생성하고 유닛리스트에 추가한다.
  • player의 미네랄이 부족한경우
    ->"미네랄이 부족합니다"를 출력
  • player의 가스가 부족한경우
    ->"가스가 부족합니다"를 출력
# 유닛 정보
unit_info={
"probe": {
"name":"프로브",
"mineral":50,
"gas":0,
"hp":20,
"shield":20,
"demage":5
},
"zealot":{
"name":"질럿",
"mineral":100,
"gas":0,
"hp":100,
"shield":60,
"demage":16
},
"dragon":{
"name":"드라군",
"mineral":125,
"gas":50,
"hp":100,
"shield":80,
"demage":20  
    }
}

class Unit:
    """
    인스턴스 속성 : 이름, 체력, 방어막, 공격력
    -> 객체마다 다른 값을 가지는 속성
    """

    def __init__(self, name, hp, shield, demage):
        self.name = name
        self.hp = hp
        self.shield = shield
        self.demage = demage
       
    
   
class Player:
    """
    속성: 닉네임, 미네랄, 가스, 유닛리스트
    """

    def __init__(self, nickname,mineral, gas, unit_list=[]):
        self.nickname = nickname
        self.mineral = mineral
        self.gas = gas
        self.unit_list = unit_list

    def produce(self, name, mineral, gas, hp, shield, demage):
        if self.mineral - mineral < 0:
            print("미네랄이 부족합니다.")
        elif self.gas  - gas < 0:
            print("가스가 부족합니다.")
        else:
            self.mineral -= mineral
            self.gas -= gas
            unit = Unit(name, hp, shield, demage)
            self.unit_list.append(unit)
            print(f"[{name}]을(를) 생산합니다.")

player1 = Player("Bisu", 400, 100)

player1.produce(unit_info['probe']['name'], unit_info['probe']['mineral'], unit_info['probe']['gas'], unit_info['probe']['hp'], unit_info['probe']['shield'], unit_info['probe']['demage'])
player1.produce(unit_info['zealot']['name'], unit_info['zealot']['mineral'], unit_info['zealot']['gas'], unit_info['zealot']['hp'], unit_info['zealot']['shield'], unit_info['zealot']['demage'])
player1.produce(unit_info['dragon']['name'], unit_info['dragon']['mineral'], unit_info['dragon']['gas'], unit_info['dragon']['hp'], unit_info['dragon']['shield'], unit_info['dragon']['demage'])


for unit in player1.unit_list:
    print(f"[{unit.name}] 체력 : {unit.hp} 방어막 : {unit.shield} 공격력 {unit.demage}")

결과

profile
개발자 꿈나무

0개의 댓글