2016-10-12 18 views
-3

Ich habe ein textbasiertes RPG in Python 3.5 gemacht. Ich hatte es gut (oder so dachte ich ...), bis ich versuchte zu sagen, dass ich nicht spielen wollte. Und auch, wenn ich die Option machen wollte, von Mobs zu laufen, aber stattdessen beides kombiniert, wenn Funktionen, und sagt die Menge an Schaden und das du 'wegliefst'. Ich habe versucht, Code umzuschalten, aber das hat es noch schlimmer gemacht! Ich brauche Hilfe. Ho zu verwenden wenn Funktion?

import time 
import random 
import sys 
import time 
global gold 
global player_HP 
global Mob_HP_1 
global Mob_HP_2 
global Mob_HP_3 
global Mob_HP_0 
mob_HP_1 = 100 
mob_HP_2 = 125 
mob_HP_3 = 130 
gold = 0 
def Demon_attack(): 
      global Mob_HP_3 
      Mob_HP_3 = 130 
      global gold 
      attack_damage=random.randint(50,200) 
      rewards=random.randint(1,5) 
      print('You do') 
      print(attack_damage) 
      print('damage!') 
      Mob_HP_3 = Mob_HP_3 - attack_damage 
      if Mob_HP_3 < 0: 
       print ('You killed the') 
       print ('Demon!') 
      if Mob_HP_3 > 0: 
       print('The Demon has') 
       print(Mob_HP_3) 
       print('HP left!') 
       choice_attack_3 = input('Do you attack?') 
       if choice_attack_3 == 'A'or 'a': 
        Demon_attack() 
       if choice_attack_3 == 'R' or 'r': 
        print('You run away!') 
        time.sleep (2) 
        battle_start() 
      time.sleep (3) 
      gold=gold+rewards 
      print('You have') 
      print(gold) 
      print('Gold!') 
      battle_start() 

def Headless_Horseman_attack(): 
      global Mob_HP_2 
      Mob_HP_2 = 125 
      global gold 
      attack_damage=random.randint(50,200) 
      rewards=random.randint(1,5) 
      print('You do') 
      print(attack_damage) 
      print('damage!') 
      Mob_HP_2 = Mob_HP_2 - attack_damage 
      if Mob_HP_2 < 0: 
       print ('You killed the') 
       print ('Headless Horseman') 
      if Mob_HP_2 > 0: 
       print('The Headless Horseman has') 
       print(Mob_HP_2) 
       print('HP left!') 
       choice_attack_2 = input('Do you attack?') 
       if choice_attack_2 == 'A' or 'a': 
        Headless_Horseman_attack() 
       if choice_attack_2 == 'R' or 'r': 
        print('You run away!') 
        time.sleep (2) 
        battle_start() 
      time.sleep (2) 
      gold=gold+rewards 
      print ('You have') 
      print(gold) 
      print('Gold!') 
      battle_start() 

def Zombie_attack(): 
      global Mob_HP_1 
      Mob_HP_1 = 100 
      global gold 
      attack_damage=random.randint(50,200) 
      rewards=random.randint(0,5) 
      print('You do') 
      print(attack_damage) 
      print('damage!') 
      Mob_HP_1 = Mob_HP_1 - attack_damage 
      if Mob_HP_1 < 0: 
       print ('You killed the') 
       print ('Zombie!') 
      if Mob_HP_1 > 0: 
       print('The Headless Horseman has') 
       print(Mob_HP_1) 
       print('HP left!') 
       choice_attack_1 = input('Do you attack?') 
       if choice_attack_1 == 'A' or 'a': 
        Zombie_attack() 
       if choice_attack_1 == 'R' or 'r': 
        print('You run away!') 
        time.sleep (2) 
        battle_start() 
      time.sleep (1.5) 
      gold=gold+rewards 
      print('You have') 
      print(gold) 
      print('Gold!') 
      battle_start() 

def battle_start(): 
    global gold 
    if gold > 50: 
     print ('You have killed all the monsters!') 
     play = input('Do you want to play again? Y/N?') 
     if choice == 'Y' or 'y': 
      game_start() 
     if choice == 'N' or 'a': 
      print ('Okay, battle later') 
      time.sleep(4) 
      sys.exit(0) 
    time.sleep (3) 
    attack_damage=random.randint(50,200) 
    rewards=random.randint(1,3) 
    mob=random.randint(1,3) 
    if mob==1: 
     mob_alive = True 
     print('You ran into a Zombie!') 
     print('The Zombie has 100 HP!') 
     choice_attack_1 = input('Do you attack?') 
     if choice_attack_1 == 'A' or 'a': 
      Zombie_attack() 
     if choice_attack_1 == 'R' or 'r': 
      print('You run away!') 
      time.sleep (2) 
      battle_start() 
    if mob==2: 
     mob_alive = True 
     print('You ran into a Headless Horseman!') 
     print('The Headless Horseman has 125 HP!') 
     choice_attack_2 = input('Do you attack?') 
     if choice_attack_2 == 'A' or 'a': 
      Headless_Horseman_attack() 
     if choice_attack_2 == 'R' or 'r': 
      print('You run away!') 
      time.sleep (2) 
      battle_start() 
    if mob==3: 
     mob_alive = True 
     print('You ran into a Demon!') 
     print('The Demon has 130 HP!') 
     choice_attack_3 = input('Do you attack?') 
     if choice_attack_3 == 'A' or 'a': 
      Demon_attack() 
     if choice_attack_3 == 'R' or 'r': 
      print('You run away!') 
      time.sleep (2) 
      battle_start() 
def game_start(): 
    global gold 
    global Mob_HP_0 
    Mob_HP_0 = 25 
    print('You have to get 50 Gold to win!') 
    attack_damage=random.randint(50,200) 
    rewards=random.randint(1,3) 
    print('To Attack, type "A". To Run Away, type "R". Lets pratice...') 
    print('Pratice Mob has 25 HP!') 
    choice_attack = input('Do you attack?') 
    if choice_attack == 'A' or 'a': 
     print('You do') 
     print(attack_damage) 
     print('damage!') 
     Mob_HP_0 = Mob_HP_0 - attack_damage 
    if choice_attack == 'R' or 'r': 
     print('You ran away!') 
     time.sleep (2) 
     game_start() 
     time.sleep (1.5) 
     gold = gold + rewards 
     print('You have ') 
     print(gold) 
     print('gold!') 
    print ('Good job') 
    print ('Lets start the battle...') 
    battle_start() 
print ('Welcome to Battle Deongeons') 
myName = input('Whats your name?') 
print ('Ok ' + myName + ' you need to kill monsters... Be careful though, its capital Sentsive.') 
choice = input("Do you want to play? Y/N") 
if choice == "Y" or "y": 
    print('Lets start the Battle') 
    game_start() 
if choice == "N" or "n": 
    print ('Okay, battle later') 
    time.sleep (4) 
    sys.exit(0) 
+0

Es tut mir leid, könnten Sie verlangsamen und erklären Sie Ihre Frage klarer? –

+2

Bitte geben Sie ein minimales Beispiel. Und versuchen Sie genau zu bestimmen Ursache Ihrer Probleme, einschließlich einer genauen Definition ("Ich brauche Hilfe" ist keine Frage, die für SO geeignet ist) – UnholySheep

+0

Es gibt keine "if-Funktion". Meinst du die ['if'-Anweisung] (https: // docs. python.org/3/tutorial/controlflow.html#if-statements)? –

Antwort

1

Dies ist eine sehr schlechte Frage, aber ist das Ergebnis nicht Boolesche Logik zu verstehen, eher als wenn Aussagen.

If choice == "Y" or "y": 

ist immer wahr. Das expandiert nach

If choice == "Y" or If "y": 

benötigen Sie entweder einen Groß- und Kleinschreibung Vergleich (wenn choice.lower() = „y“), eine Liste Vergleich zu tun (wenn Wahl in „yy“) oder explizit beiden Bedingungen auf eiterh angeben Seite Ihres oder (wenn Wahl == "y" oder Wahl == "Y"

0
  if choice_attack_3 == 'A'or choice_attack_3 == 'a': 
       Demon_attack() 
      elif choice_attack_3 == 'R' or choice_attack_3 == 'r': 
       print('You run away!') 
       time.sleep (2) 
       battle_start() 

Sie müssen choice_attack_3 == auf der anderen Seite des or Anweisung hinzuzufügen. 'a' an sich ist technisch wahr, so dass es beide if-Anweisungen eingeben.

Edit: gleiche mit

if choice == "Y" or choice == "y": 
    print('Lets start the Battle') 
    game_start() 
if choice == "N" or choice == "n": 
Verwandte Themen