现在我正在尝试为我的朋友制作一个小游戏,以便在 Python 3.3 中获得乐趣。 一切顺利,直到我忘记了如何获得某些变量(在我的情况下为黄金,损坏和XP)来通过不同的功能(Menu,Hills)。
如果有人可以帮我修复这段代码,那么前面提到的三个变量可以在我的函数之间保持不变,那就太棒了。这是我的代码。
floats = values.ToArray();
答案 0 :(得分:1)
在这种情况下,我想你只需要在本地方法范围之外声明这些变量,并在修改它们时使用global关键字。像这样:
import os
import random
import time
damage = None
xp = None
gold = None
def Menu():
damage = 1
print("\nyour attack value is", damage,"\nyour xp level is", xp,"\nand you have", gold,"gold.\n")
sword = input("Old Joe Smith: Do you need a sword? ")
sword = sword.lower()
if sword == "yes":
damage = damage + 9
print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n")
time.sleep(2)
Hills()
if sword == "yeah":
damage = damage + 9
print("Old Joe Smith: *Gives you a sharp steel sword* Good luck on your travels!\n\nYour new attack value is",damage,"\n")
time.sleep(2)
Hills()
elif sword == "no":
print("Old Joe Smith: Well, if you say so... ...good luck anyway!\n")
Hills()
else:
print("Old Joe Smith: I'm sorry, what?")
time.sleep(1)
Menu()
def Hills():
print("*You walk through the forest, when out of nowhere a minotaur appears!*")
fight = input("What will you do, run or fight? ")
fight = fight.lower()
if fight == "run":
print("You escape, barely.")
Cave()
if fight == "fight":
input("Press enter")
if damage > 5:
print("You win! you looted 10 gold and got 5 xp.")
global gold
gold = gold + 10
global xp
xp = xp + 5
Cave()
elif damage < 5:
print("You died. Game over.")
Menu()
else:
print("How the hell did you get exactly 5 damage?")
Menu()
else:
print("Your lack of a proper response makes the minotaur charge. It kills you instantly.")
Menu()
def Cave():
print("You stumble into a cave. there are two routes in the cave. which way do you want to go, left or right?")
print("Welcome to 'RPG Game', a role-playing game developed in Python 3.3.\nThis game was developed by bamf_mccree.\n")
print("Old Joe Smith: Hello, adventurer, and welcome to Dankhill, in the centre of Whitewood forest. \nThis was once a peaceful place, but the evil Lord Draktha has enslaved most of the civilians of our realm.")
time.sleep(4)
Menu()