我不知道具體該怎么解釋,但我會盡力而為。
這是一個簡單的 OOP 示例,您可以在其中創建玩家來殺死一條龍。關鍵是為每個玩家分配特定數量的傷害以對龍造成傷害,以殺死它。
例如,如果一條龍有 200 點生命值并選擇 2 名玩家攻擊它,則玩家將被分配每人造成 100 點傷害。
這是OOP的結構:
class Player:
def __init__(self, name):
self.name = name
self.damage_to_deal = {} #Format: {'dragon': damage to deal to him}
class Dragon:
def __init__(self, name, health):
self.name = name
self.health = health
self.number_of_players_attacking = 0
#Add a player as someone who'll attack this dragon
def add_player(self, player):
self.number_of_players_attacking = 1
player.damage_to_deal[self.name] = self.calculate_partition_damage()
def calculate_partition_damage(self):
if self.number_of_players_attacking == 0:
self.partition_damage = 0
else:
self.partition_damage = self.health/self.number_of_players_attacking #health / number of players = damage to do for each player
return self.partition_damage
這是我正在測驗的操作:
#Create 3 players
player_A = Player(name='Josh')
player_B = Player(name='Steven')
player_C = Player(name='Robert')
#Create 2 Dragons
dragon_A = Dragon(name='Gamacial', health=475)
dragon_B = Dragon(name='Nibiru', health=150)
#All 3 players will attack the dragon A
dragon_A.add_player(player_A)
dragon_A.add_player(player_B)
dragon_A.add_player(player_C)
#Only player A and B will attack dragon B
dragon_B.add_player(player_A)
dragon_B.add_player(player_B)
#The damage each player will have to... {'to this dragon': x damage} to kill the dragons
print(f'{player_A.name} will have to do: {player_A.damage_to_deal}')
print(f'{player_B.name} will have to do: {player_B.damage_to_deal}')
print(f'{player_C.name} will have to do: {player_C.damage_to_deal}')
這將列印以下內容:
Josh will have to do: {'Gamacial': 475.0, 'Nibiru': 150.0}
Steven will have to do: {'Gamacial': 237.5, 'Nibiru': 75.0}
Robert will have to do: {'Gamacial': 158.33333333333334}
問題是,由于 Josh 是第一個負責殺死 Gamacial 和 Nibiru 的玩家,他被指派對這些龍造成全部傷害,盡管 Steven 和 Robert 也負責殺死那條龍。(同樣的問題與龍尼比魯)。
換句話說,它永遠是最后一個會造成實際正確傷害的玩家,而其他玩家總是比計算落后一步,因為他們在計算下一個玩家之前已經被分配了任務。
因此它應該列印:
Josh will have to do: {'Gamacial': 158.33333333333334, 'Nibiru': 75.0}
Steven will have to do: {'Gamacial': 158.33333333333334, 'Nibiru': 75.0}
Robert will have to do: {'Gamacial': 158.33333333333334}
有什么方法可以讓所有玩家保持同步嗎?那么在史蒂文被分配與喬希相同的龍之后,喬希得到更新等等?
uj5u.com熱心網友回復:
也許你應該稍后計算,畢竟玩家注冊了龍:
class Player:
def __init__(self, name):
self.name = name
self.damage_to_deal = {} #Format: {'dragon': damage to deal to him}
def deal_damage(self):
for name in self.damage_to_deal:
target = self.damage_to_deal[name]
self.damage_to_deal[name] = target.health / target.number_of_players_attacking
return self.damage_to_deal
class Dragon:
def __init__(self, name, health):
self.name = name
self.health = health
self.number_of_players_attacking = 0
#Add a player as someone who'll attack this dragon
def add_player(self, player):
self.number_of_players_attacking = 1
player.damage_to_deal[self.name] = self # calculate later
登記:
#Create 3 players
player_A = Player(name='Josh')
player_B = Player(name='Steven')
player_C = Player(name='Robert')
#Create 2 Dragons
dragon_A = Dragon(name='Gamacial', health=475)
dragon_B = Dragon(name='Nibiru', health=150)
#All 3 players will attack the dragon A
dragon_A.add_player(player_A)
dragon_A.add_player(player_B)
dragon_A.add_player(player_C)
#Only player A and B will attack dragon B
dragon_B.add_player(player_A)
dragon_B.add_player(player_B)
然后計算:
#The damage each player will have to... {'to this dragon': x damage} to kill the dragons
print(f'{player_A.name} will have to do: {player_A.deal_damage()}')
print(f'{player_B.name} will have to do: {player_B.deal_damage()}')
print(f'{player_C.name} will have to do: {player_C.deal_damage()}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/484823.html
