提问人:Kal Set 提问时间:9/26/2018 最后编辑:rapidDevKal Set 更新时间:10/5/2018 访问量:85
在 Python 中开发我的第一个真正的游戏,但我遇到了一些问题
Working on my first real game in Python, But I've Ran into a few problems
问:
在编写我的游戏时,我在运行它时遇到了问题。例如,僵尸攻击没有正确出来,使用商店时,金币值和生命值不会更新。运行战斗部分时,僵尸第一次生命值较低,然后生命值较高,伤害较多。我不知道我搞砸了什么。有什么帮助/提示吗?
import time
import sys
import random
cls = ("\n"*100)
class Mage:
def __init__(self):
self.maxhp = 50
self.attack = 3.33
self.name = "Mage"
class Warrior:
def __init__(self):
self.maxhp = 70
self.attack = 2.5
self.name = "Warrior"
class Thief:
def __init__(self):
self.maxhp = 35
self.attack = 5
self.name = "Thief"
class Zombie:
def __init__(self):
self.maxhp = 10
self.attack = 1
self.name = "Zombie"
def heal(character_health):
if character_health < character_health:
character_health += 5
print("Healed. Health is now " + character_health + " +5.")
time.sleep(2)
else:
print("No healing available.")
time.sleep(2)
def battle(character_health, character_attack, monster_health, monster_attack, gold):
while True:
character_health_max = character_health
monster_name = "Zombie"
choice1 = input("\nPress 1 to Attack: ")
if choice1 == "1":
monster_health -= character_attack
print("\n" + str(monster_name) + "'s health is now " + str(monster_health))
time.sleep(1)
character_health -= monster_attack
print("\nThe hero's health is now " + str(character_health))
time.sleep(1)
if character_health <= 0:
print("\nThe hero is dead.")
sys.exit("\nThe End")
if monster_health <= 0:
print("\nThe monster is dead.")
time.sleep(2)
print("Your gold has increased by: 5")
gold += 5
monster_health = 10
character_health = character_health_max
time.sleep(2)
menu_list(character_health, character_attack, monster_health, monster_attack, gold)
def store(gold, character_health):
print("\nWelcome to my shop of wonders! My name is Hanz, what can I aid you with today? We have...\nPotions: [1.] EEK")
buy = input("\nWhat will it be? ")
if gold < 5:
print("Sorry, you don't have any gold!")
time.sleep(2)
if buy == "1" and gold >= 5:
print("\nYou now own the Potion EEK! Health increased by 5!")
character_health += 5
gold -= 5
time.sleep(2)
def menu_list(character_health, character_attack, monster_health, monster_attack, gold):
while True:
print(cls)
menu = input("---> Fight [1.] \n---> Heal [2.] \n---> Store [3.] \n---> Quit [4.] \n---> Gold: " + str(gold) + " \n---> ")
if menu == "4":
sys.exit()
if menu == "2":
heal(character_health)
if menu == "1":
battle(character_health, character_attack, monster_attack, monster_health, gold)
if menu == "3":
store(gold, character_attack)
if menu == "Gold":
print("\nNot valid hackerman.")
time.sleep(1)
class Main:
print(cls)
name = input("What is your name: ")
character = input("\nChoose your class: \n----------------- \nMage [1.] \nWarrior [2.] \nThief [3.] \n---> ")
if character == "1":
character_health = Mage().maxhp
print("\nHealth " + str(character_health))
character_attack = Mage().attack
print("\nAttack " + str(character_attack))
character_name = Mage().name
print("\nClass " + str(character_name))
time.sleep(3)
monster_health = Zombie().maxhp
monster_attack = Zombie().attack
gold = 0
menu_list(character_health, character_attack, monster_health, monster_attack, gold)
if character == "2":
character_health = Warrior().maxhp
print("\nHealth " + str(character_health))
character_attack = Warrior().attack
print("\nAttack " + str(character_attack))
character_name = Warrior().name
print("\nClass " + str(character_name))
time.sleep(3)
monster_health = Zombie().maxhp
monster_attack = Zombie().attack
gold = 0
menu_list(character_health, character_attack, monster_health, monster_attack, gold)
if character == "3":
character_health = Thief().maxhp
print("\nHealth " + str(character_health))
character_attack = Thief().attack
print("\nAttack " + str(character_attack))
character_name = Thief().name
print("\nClass " + str(character_name))
time.sleep(3)
monster_health = Zombie().maxhp
monster_attack = Zombie().attack
gold = 0
menu_list(character_health, character_attack, monster_health, monster_attack, gold)
if __name__ == '__main__':
Main()
答:
0赞
rapidDev
9/26/2018
#1
我认为现在是回顾一下在 Python 中将变量传递给函数时会发生什么的好时机。
首先,Python 中的一切都是一个对象!基元也是如此(数字在 Python 中被包装为对象)。每个类的对象都继承自 Python 中的类。object
现在,当您将原语传递给函数并更改值时会发生什么?
举例说明:
def foo(a):
a = 5
b = 1
foo(b)
print(b) # b is still 1! Some objects in Python are immutable including integers, strings, and booleans.
现在我怀疑你的金币和生命值没有改变,因为你传递的是无法改变的不可变对象!
如何解决? 你想传入一个可变对象!与其传递一个整数对象(不可变)来表示字符的运行状况,不如传入字符对象(可变的)。您可以设置该字符对象的新运行状况。
举例说明:
class Warrior:
def __init__(self):
self.maxhp = 70
self.attack = 2.5
self.name = "Warrior"
self.currenthp = 55 # arbitrary number but you may want to have something like this
def heal(warrior):
warrior.currenthp += 5
# somewhere in your Main function
warrior1 = Warrior()
heal(warrior1) # currenthp of this warrior should be 60!
在创建游戏时正确实现 OOP 非常重要。同样,尝试调试其他问题,注意如何实现 OOP。
评论
0赞
Kal Set
9/28/2018
好的,非常感谢!这清除了很多,你的解释也很棒。再次感谢!
上一个:我应该关闭从适配器获取的光标吗?
评论
Player
maxhp
attack
name
take_damage
heal
attack
character_health = Warrior().maxhp
Warrior