2017-12-30 7 views
-2

나는 건강을 표시파이썬 텍스트 게임 건강 줄

def do_health 
    print health,"/ 200" 

을 사용하고 텍스트 어드벤처를 만들고있어하지만 난

|----------   | 
     50% 

가 따라 같은 비율 및 인쇄 무언가로 변환 할 플레이어가 남긴 건강 비율은 어느 정도이지만 유휴 상태의 상태 표시 줄을 만드는 데는 다른 곳에서는 찾을 수 없습니다.

미리 감사드립니다. (당신의 최대 건강에 해당 될 :

+0

일부 솔루션을 사용할 수 있습니다. htt ps : //stackoverflow.com/questions/3173320/text-progress-bar-in-the-console –

답변

0

수행해야 할 모든 것은 대시의 번호로 현재의 건강을 변환하고 최대 대시 수 (healthDashes이 경우에 20)를 정의하는 몇 가지 간단한 변환입니다 200 : maxHealth).

왼쪽에 80의 건강 상태가 있다고 생각해보십시오. 따라서 예를 들어 healthDashes(20)/maxHealth(200)을 입력하면 10이됩니다.이 값은 우리가 건강을 나눔으로써 우리가 원하는 대시 수로 변환하는 값입니다. 그러면 현재 health80이고 대시 수는 80/10 => 8 dashes입니다. 비율은 직선입니다 : (health(80)/maxHealth(200))*100 = > 40 percent.

지금 파이썬에서 당신은 그냥 lodic 이상 적용하고 당신은 얻을 것이다 : 여기

do_health() 
> 
|--------   | 
     40% 

변화와 몇 가지 더 예입니다 : 당신이 메소드를 호출하면

health = 80  # Current Health 
maxHealth = 200 # Max Health 
healthDashes = 20 # Max Displayed dashes 

def do_health(): 
    dashConvert = int(maxHealth/healthDashes)       # Get the number to divide by to convert health to dashes (being 10) 
    currentDashes = int(health/dashConvert)       # Convert health to dash count: 80/10 => 8 dashes 
    remainingHealth = healthDashes - currentDashes     # Get the health remaining to fill as space => 12 spaces 

    healthDisplay = ''.join(['-' for i in range(currentDashes)])  # Convert 8 to 8 dashes as a string: "--------" 
    remainingDisplay = ''.join([' ' for i in range(remainingHealth)]) # Convert 12 to 12 spaces as a string: "   " 
    percent = str(int((health/maxHealth)*100)) + "%"     # Get the percent as a whole number: 40% 

    print("|" + healthDisplay + remainingDisplay + "|")    # Print out textbased healthbar 
    print("   " + percent)          # Print the percent 

당신이 결과를 얻을 수 health의 값 :

|----------   | # health = 100 
     50% 
|--------------------| # health = 200 
     100% 
|     | # health = 0 
     0% 
|------    | # health = 68 
     34% 
관련 문제