Answer the question
In order to leave comments, you need to log in
How can I change / shorten / decorate the code?
'''
1. Создавать воинов из json (также как оружие)
2. Добавить показатель брони воина (полученный урон = урон оружия - броня)
3. Добавить шанс попадания оружием
4. Для класса Weapon добавить метод, вычисляющий средний урон за удар
5. Добавить свои виды оружия, сбаллансировать их по урону (ср. урон 20 +- 10)
6. Возможность "битвы" 2х2, 4х4 ... NxN. Нужно продумать логику нанесения ударов.
7. На пов. оценку: визуализировать с помощью pygame либо другого игр. движка
'''
import random,os
from names import createWarriorTeam,createWeapons,Warrior,Weapons
warriors = createWarriorTeam(os.path.join(os.getcwd(),'WARRIORS.json'))
weapons = createWeapons(os.path.join(os.getcwd(),'WEAPONS.json'))
class Battlefield():
def __init__(self,warriors,weapons):
self.warriors = warriors
self.weapons = weapons
def _getweapons(self):
self.weapons = [Weapons(x,self.weapons[x]) for x in self.weapons]
def _getcomands(self):
self.teams = {}
self.teamscount = {}
for warr in self.warriors:
warr = warr.upper()
self.team = [Warrior(x,warr,self.weapons) for x in self.warriors[warr]]
self.teams[warr] = self.team
self.teamscount[warr] = len(self.team)
print('-' * 100)
for tc in self.teamscount:
print('{0}:{1}'.format(tc,self.teamscount[tc]),end=' ')
print()
print('-'*100)
def _fight(self):
while True:
liveTeams = 0
lteams = list(self.teams)
random.shuffle(lteams)
for lt in lteams:
warrior = random.choice(self.teams[lt])
otherTeams = [x for x in lteams if x!=lt]
enemyTeam = random.choice(otherTeams)
enemywarrior = random.choice(self.teams[enemyTeam])
if enemywarrior.is_alive:
warrior.hit(enemywarrior)
for ltlive in lteams:
if (([False]*self.teamscount[ltlive])==[x.is_alive for x in self.teams[ltlive]]):
liveTeams+=1
if liveTeams==len(self.teams)-1:
break
self._end()
self._averageDamage()
def _end(self):
for x in self.teams:
if not all([not x.is_alive for x in self.teams[x]]):
winner = x
break
print('Победила команда {0}!'.format(winner))
print('-'*100)
for t in self.teams:
for w in self.teams[t]:
print('Имя:{0} Команда:{1} Здоровье:{2} Броня:{3} Оружие:{4}({5})'.format(w.name, w.team, w.health,
w.armor, w.weapon.name,w.weapon.damage))
def _averageDamage(self):
print('-' * 100)
warriors =[]
for t in self.teams:
warriors+=self.teams[t]
for x in warriors:
print('Оружие:{0} Средний урон:{1}({2})'.format(x.weapon.name,
x.weapon.getAverageDamage(),x.weapon.damage))
def start(self):
self._getweapons()
self._getcomands()
self._fight()
a = Battlefield(warriors,weapons)
a.start()
Answer the question
In order to leave comments, you need to log in
replace string notation
https://shultais.education/blog/python-f-strings
Read about pep8:
for example:
otherTeams, enemyTeam, etc. - so it is not recommended to name variables
For. f-strings already said:
print('Имя:{0} Команда:{1} Здоровье:{2} Броня:{3} Оружие:{4}({5})'.format(w.name, w.team, w.health,
w.armor, w.weapon.name,w.weapon.damage))
print(
f'Имя:{w.name} '
f'Команда:{w.tem} '
f'Здоровье:{w.health}'
f'Броня:{w.armor}'
f'Оружие:{w.weapon.name}({w.weapon.damage})'
)
if ([False]*self.teamscount[ltlive])==[x.is_alive for x in self.teams[ltlive]])
if not any([x.is_alive for x in self.teams[ltlive]])
? Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question