|
Re: Self in the Argument List: Redundant is not Explicit
|
Posted: Nov 30, 2011 2:26 AM
|
|
I am learning python for 1 week. I was trying to create a class Player(object) in which there are player creation function , player print stats, player attacks enemy and so and so... but I got so many strange issues with classes, this might be my little knowing the language or just it`s more complicated than it looks...You can review a simple prototype here # for dices from random import randint
class Player(object): #player must have attribites: #HP, ATK, DEF, SPD and S.Skill! def __init__(self): self.player={ "Name": " ", "Class":" ", "HP": 0, "ATK":0, "DEF":0, "SPD":0, "SKILL": " "} #container for the player def class_atributes(self, HP, ATK, DEF, SPD, SKILL): self.player["HP"] = randint(1,10)+HP # 5 for thieves, 10 for fighters, 15 for defenders self.player["ATK"] = randint(1,6)+ATK #2 for thieves, 5 for fiighters, 3 for defenders self.player["DEF"] = randint(1,6)+DEF #2 for thieves, 3 for fighters, 5 for defenders self.player["SPD"] = randint(1,6)+SPD #6 for thieves 2 for fighters and defenders self.player["SKILL"] = SKILL
def player_creation(self): self.name = str(input("Enter your name: ")) self.class_p = str(input("Enter your class: ")) if self.class_p == "fighter": self.player["Name"] = self.name self.player["Class"] = self.class_p self.class_atributes(10, 5,3,2,"Charge!") elif self.class_p == "thief": self.player["Name"] = self.name self.player["Class"] = self.class_p self.class_atributes(5, 2,2,6,"Critical!") elif self.class_p == "defender": self.player["Name"] = self.name self.player["Class"] = self.class_p self.class_atributes(15,3,5,2,"Counter!") else: print("Unknown class! Please refer to:\nfighter, thief, defender\n") return "player_creation"
def player_print(self): print("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") print("Name: %s \t|Class: %s"%(self.player["Name"], self.player["Class"])) print("HP :%r"%("#"*self.player["HP"])) print("ATK:%r"%("#"*self.player["ATK"])) print("DEF:%r"%("#"*self.player["DEF"])) print("SPD:%r"%("#"*self.player["SPD"])) print("SKILL:%s"%(self.player["SKILL"])) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
def player_turn(self, player, enemy): print("It`s %s turn. Take a wise action\n"%(self.player["Name"])) self.action = str(input("(A)ttack, (S)pecial, (M)enu: ")) if self.action == 'a' or self.action == 'A': self.attack = randint(1,6)+self.player["ATK"] self.attack -= enemy.player["DEF"] enemy.player["HP"] -= self.attack # damage enemy return 0 elif self.action == 'm' or self.action == 'M': self.player_print() else: print("Error usage, turn wasted... Be more wise") return 0 def player_death(self, player, enemy): print("""It was a gruesome battle.... but after hevy damages and deep wounds %s didn`t make it and fell under %s the cunning tactics and peerless skill"""%(self.player["Name"], enemy.player["Name"]))
def battle(self, enemy): #init player speeds self.p1turn = self.player["SPD"] self.p2turn = enemy.player["SPD"] while self.player["HP"] >=0 or enemy.player["HP"] >=0: self.p1turn += self.player["SPD"] self.p2turn += enemy.player["SPD"] if self.p1turn >= 100: self.p1turn = self.player_turn(player,enemy) if self.p2turn >= 100:
############################################################################# ##############################
p1 = Player() p2 = Player() p3 = Player() p1.player_creation() p2.player_creation() p1.player_print() p2.player_ print() p1.battle(p2)
|
|