2016-10-25 5 views
-1

저는 더 많은 코드를 작성해야 할 필요가있는 경우 초보자도 Python만큼 오버플로 할 수 있습니다. 나는 2 일 동안 아무 것도 할 수 없다는 것에 막혔다. 개선이된다면 내 코드를 고치고 효율적으로 만들 수 있도록 말해줘. 또한 아래 함수를 어떻게 호출해야할지 모르겠다. 내가하고 싶은 것을 가진 상자를 나열했지만 어떻게해야할지 모르겠다.도움을 요청하고, 함수를 호출하고, 프로그램을 개선하는 데 도움이 필요합니다.

import random 
import time 

startingP_health = 30 
startingE_health = 30 

def player_attack(): 
    global startingE_health 
    time.sleep(1) 
    print ("What ability would you like to use? (free speach(fs), capatalism(c), or punch(p)") 
    ability_choice = input() 

    if(ability_choice == "fs"): 
     enemy_health = startingE_health-3 
     enemy_heath = int(enemy_health) 
    elif(ability_choice == "c"): 
     enemy_health = startingE_health-(random.randint(1,6)) 
     enemy_heath = int(enemy_health) 
    elif(ability_choice == "p"): 
     enemy_health = startingE_health-(random.randint(2,4)) 
     enemy_heath = int(enemy_health) 
    else: 
     print("you fell.") 

    time.sleep(1) 

    print ("Enemie's health is now: ",enemy_health) 
    print("") 

    return int(enemy_health) 


def enemy_attack(): 
    global startingP_health 
    time.sleep(1) 
    print ("Enemy kicks you") 
    print("") 
    player_health = startingP_health - (random.randint(1,3) 
    player_health = int(player_health) 
    time.sleep(1) 
    print ("Your health is now ",player_health) 
    print ("") 
    return int(player_health) 

def battle_trotsky(): 
    global player_health 
    print ("Enemy appears") 
    print ("") 
    time.sleep(1) 
    while player_health > 0 and enemy_health > 0: 
     ############################## 
     #call function player_attack 
     #call enemy_attack 
     ############################## 
     if player_health <=0: 
      break 
     if enemy_health <= 0: 
      time.sleep(1) 
      print ("You have killed the enemy") 

     if player_health <= 0: 
      print("Sorry you failed the mission you must restart the mission")) 

    ################################ 
    #initate function sequence 
    ################################ 
+1

에 오신 것을 환영합니다 스택 오버플로! 코드가 작동하는 경우 http://codereview.stackexchange.com/을 고려할 수 있습니다. 그렇지 않으면 무엇이 잘못 되었는가에 대한 상세하고 구체적인 설명을 포함하십시오. –

+0

@leaf 기본적으로 3 명의 적을 당신 앞에두고, 3 가지 공격 유형 중 1 가지를 선택하는 게임을 만들고 싶었습니다. 스토리 타입의 게임이지만 줄거리를 추가 할 수 있습니다. (전투 기계가 작동하기를 바랄뿐입니다.) (지금은 명확합니까?) –

답변

0
import random 
import time 

:

startingP_health = 30 
startingE_health = 30 
enemy_health = startingE_health 
player_health = startingP_health 

이 기능은 아무것도 반환하지 않는 당신 때문에, global enemy_health을 선언 한 다음 새 값을 할당하십시오.

def player_attack(): 
    global enemy_health 
    time.sleep(1) 
    print ("What ability would you like to use? (free speech(fs), capitalism(c), or punch(p)") 
    ability_choice = input() 

enemy_health = starting health - 3을 수행했기 때문에 이것은 작동하지 않았습니다. 이는 매회 건강 상태가 시작될 때부터 다시 시작된다는 것을 의미합니다. 대신 여기, -=은 현재 값에서 뺄된다 enemy_health = enemy_health - 3을 :

if(ability_choice == "fs"): 
     enemy_health -= 3 
    elif(ability_choice == "c"): 
     enemy_health -= random.randint(1,6) 
    elif(ability_choice == "p"): 
     enemy_health -= random.randint(2,4) 
    else: 
     print("you fell.") 
    time.sleep(1) 
    print ("Enemy's health is now: ",enemy_health) 
    print("") 


def enemy_attack(): 
    global player_health 
    time.sleep(1) 
    print ("Enemy kicks you") 
    print ("") 
    player_health -= random.randint(1,3) 
    time.sleep(1) 
    print ("Your health is now ",player_health) 
    print ("") 

def battle_trotsky(): 
    global player_health 
    global enemy_health 
    print ("Enemy appears") 
    print ("") 
    time.sleep(1) 
    while player_health > 0 and enemy_health > 0: 

기능이 name() 통해 호출되며, 본 :

 player_attack() 
     enemy_attack() 
     if enemy_health <= 0: 
      time.sleep(1) 
      print ("You have killed the enemy") 

     if player_health <= 0: 
      print("Sorry you failed the mission you must restart the mission") 

battle_trotsky() 
+0

2 정말 고맙습니다. 정확히 설명 할 필요가있는 것입니다. 여러 명의 원수와 진부한 스토리 라인으로 게임을 만들려고 노력하고 있습니다. 어떤 부분에서 나를 도울 수 있다고 생각하니, 아니면 그냥 게시해야합니까? 여기 붙어있을 때. 당신의 대답에 상관없이 정직하게 도와 줘서 고마워요. –

+0

@ Uri.K 너 자신을 알아 내려고 노력했다. 목표를 향한 노력을 보여 주면 사람들은 훨씬 더 많은 도움을받을 수 있습니다 :) 그리고 행운을 빕니다 ... 모두가 정사각형 하나에서 시작해야했습니다! – TemporalWolf

0

이 사이트는 좋은 질문이 아닙니다. 구체적인 것은 아니지만이 코드는 강력한 권한으로 수정해야합니다. 나는 왜 당신이 잠을 자고 있는지 잘 모르겠다. print ("") 문은 이전 인쇄물에서 '\ n'으로 바꿀 수 있으며, 플레이어의 건강을 시작 건강 - 번호로 계속 재 할당하고있다. 절대 0에 도달하지 않으므로 건강을 글로벌하게해야합니다. 여기

는 작업 예제

import random 

player_health = 30 
enemy_health = 30 

def player_attack(): 
    global enemy_health 
    ability_choice = input("What ability would you like to use? (free speach(fs), capatalism(c), or punch(p)") 

    if(ability_choice == "fs"): 
     enemy_health -= 3 
    elif(ability_choice == "c"): 
     enemy_health -= random.randint(1,6) 
    elif(ability_choice == "p"): 
     enemy_health -= random.randint(2,4) 
    else: 
     print("you fell.") 
    print ("Enemy's health is now: "+str(enemy_health)+'\n') 

def enemy_attack(): 
    global player_health 
    print ("Enemy kicks you") 
    player_health -= random.randint(1,3) 
    print ("Your health is now "+str(player_health)+"\n") 

def battle_trotsky(): 
    print ("Enemy appears\n") 
    while player_health > 0 and enemy_health > 0: 
     player_attack() 
     if check_game_over(): 
      return 
     enemy_attack() 
     if check_game_over(): 
      return 

def check_game_over(): 
    global player_health, enemy_health 
    if enemy_health <= 0: 
     print ("You have killed the enemy\n") 
     return True 
    elif player_health <= 0: 
     print("Sorry you failed the mission you must restart the mission") 
     return True 
    else: 
     return False 

if __name__=='__main__': 
    battle_trotsky() 
가 지속 있도록 전역으로 enemy_healthplayer_health을 선언 할 필요가
+0

raw_input()은 python2입니다. OP가 python3으로 지정되었습니다. 여기서 input()은 원하는 것입니다. – TemporalWolf

+0

나는 3 개의 공격 유형 중 1 개를 선택하기 전에 기본적으로 3 개의 적을 표시하는 게임을 만들고 싶었습니다. 하나의 죽을 때까지 모든 공격으로 업데이트 된 건강과 지속적인 전투를했습니다. 나는 일주일 전에 파이썬을 배우기 시작했고, 나는 추측 할 때, 하지만 코드 완성을 도울 수 있다고 생각하면 3 분 정도 걸릴 것입니다. 시간이 있다면 당신에게 너무 많이 부탁하고 싶지는 않습니다. –

+0

이 코드는 한 명의 적과 플레이어에게만 적용됩니다. 변경 사항을 이해하고 나머지는 직접 구현하려고 시도하십시오. –

관련 문제