[英]Python 2.7.8: printing color/value with out the format
我是python的新手,学习了大约一个月。 我遇到一个问题,当我运行此代码时,应该以红色打印出数字。 第二个示例显示了它真正打印出来的内容,但我感到困惑。 请帮忙。
应该可以打印('Enemy HP:', 1150/1200)
但实际上会打印('Enemy HP:', '\\x1b[91m1150/1200\\x1b[0m\\n')
import random
class bcolors:
HEADER = '\033[95m'
OKBLUE = "\x1b[94m"
OKGREEN = "\x1b[92m"
WARNING = '\033[93m'
FAIL = '\x1b[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Person:
def __init__(self, hp, mp, atk, df, magic):
self.maxhp = hp
self.hp = hp
self.maxmp = mp
self.mp = mp
self.atkl = atk - 10
self.atkh = atk + 10
self.df = df
self.magic = magic
self.actions = ["Attack", "Magic"]
def generate_damage(self):
return random.randrange(self.atkl, self.atkh)
def generate_spell_damage(self, i):
mgl = self.magic[i]["dmg"] - 5
mgh = self.magic[i]["dmg"] + 5
return random.randrange(mgl, mgh)
def take_damage(self, dmg):
self.hp -= dmg
if self.hp < 0:
self.hp = 0
return self.hp
def get_hp(self):
return self.hp
def get_max_hp(self):
return self.maxhp
def get_mp(self):
return self.mp
def get_max_mp(self):
return self.maxmp
def reduce_mp(self, cost):
self.mp -= cost
def get_spell_name(self, i):
return self.magic[i]["name"]
def get_spell_mp_cost(self, i):
return self.magic[i]["cost"]
def choose_action(self):
i = 1
print(bcolors.OKBLUE + bcolors.BOLD + "Actions" + bcolors.ENDC)
for item in self.actions:
print(str(i) + ":", item)
i += 1
def choose_magic(self):
i = 1
print(bcolors.OKBLUE + bcolors.BOLD + "Magic" + bcolors.ENDC)
for spell in self.magic:
print(str(i) + ":", spell["name"], "(cost:", str(spell["cost"]) + ")")
i = 1
from classes.game import Person, bcolors
magic = [{"name": "Fire", "cost": 10, "dmg": 100},
{"name": "Thunder", "cost": 10, "dmg": 124},
{"name": "Blizzard", "cost": 10, "dmg": 100}]
player = Person(460, 65, 60, 34, magic)
enemy = Person(1200, 65, 45, 25, magic)
running = True
i = 0
print(bcolors.FAIL + bcolors.BOLD + "AN ENEMY ATTACKS!" + bcolors.ENDC)
while running:
print("======================")
player.choose_action()
choice = input("Choose action:")
index = int(choice) - 1
if index == 0:
dmg = player.generate_damage()
enemy.take_damage(dmg)
print("You attacked for", dmg, "points of damage.")
elif index == 1:
player.choose_magic()
magic_choice = int(input("Choose magic:")) - 1
magic_dmg = player.generate_spell_damage(magic_choice)
spell = player.get_spell_name(magic_choice)
cost = player.get_spell_mp_cost(magic_choice)
current_mp = player.get_mp()
if cost > current_mp:
print(bcolors.FAIL + "\nNot enough MP\n" + bcolors.ENDC)
continue
player.reduce_mp(cost)
enemy.take_damage(magic_dmg)
print(bcolors.OKBLUE + "\n" + spell + " deals", str(magic_dmg), "points of damage" + bcolors.ENDC)
enemy_choice = 1
enemy_dmg = enemy.generate_damage()
player.take_damage(enemy_dmg)
print("Enemy attacks for", enemy_dmg)
print("----------------------------")
print("Enemy HP:", bcolors.FAIL + str(enemy.get_hp()) + "/" + str(enemy.get_max_hp()) + bcolors.ENDC + "\n")
print("Your HP:", bcolors.OKGREEN + str(player.get_hp()) + "/" + str(player.get_max_hp()) + bcolors.ENDC)
print("Your MP:", bcolors.OKBLUE + str(player.get_mp()) + "/" + str(player.get_max_mp()) + bcolors.ENDC + "\n")
if enemy.get_hp() == 0:
print(bcolors.OKGREEN + "You Win!", + bcolors.ENDC)
running = False
elif player.get_hp() == 0:
print(bcolors.FAIL + "Your enemy has defeated you!" + bcolors.ENDC)
running = False
您的代码在Python 3中可以很好地工作,其中print
是一个函数:
>>> print("x", "y")
x y
它的意思是“先打印第一个参数,然后打印分隔符(默认为空格),然后打印第二个参数。
不过在Python 2中:
>>> print("x", "y")
('x', 'y')
打印包含字符串的元组的表示形式。
因此,您可以使用具有许多优点的Python 3,也可以像这样更改代码:
print("Enemy HP:" + bcolors.FAIL + str(enemy.get_hp()) + "/" +
str(enemy.get_max_hp()) + bcolors.ENDC + "\n")
# note the + instead of ,
为了打印单个字符串。
对您的其他人打印有用吗?
您在类bcolors的OKGREEN上有一个双引号“”,而不是简单的”,也许它来自这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.