一个有趣的(无聊的)关于类的Python王者荣耀小游戏(咳咳)
最近老师给我们布置了一个类的作业,我在网上看(瞥)了好多教程,一时间 心领神会,心血来潮,,心有灵犀,,,不对。。不管了,写了一个无聊的游戏练练手
o(# ̄▽ ̄)==O)) ̄0 ̄")o 金钢飞拳~!!
import random as rd
import time
import threading
import os# 生命体
class Life:def __init__(self, name='LIFE', life=100, attack=100, defend=100, money=300):self.name = nameself.life = lifeself.attack = attackself.defend = defendself.money = moneyself.vis_lt = []def fight(self):pass# 武器装备,技能
class Weapon:def __init__(self):pass# 英雄
class Hero(Life):def __init__(self, name='镜', life=3000, attack=200, defend=100, money=300, ocu_name='打野'):super().__init__(name, life, attack, defend, money)self.ocu_name = ocu_nameself.move()self.kill_monster = 0self.kill_hero = 0self.hurt_level = self.attack # 伤害水平def __str__(self):return '%s:%s life:%s attack:%s defend:%s money:%s position:%s' % (self.ocu_name, self.name, self.life, self.attack, self.defend, self.money, self.position)# 1英雄随机运动寻找目标def move(self):enemy = monsterif isinstance(enemy, Monsters) and len(enemy.mst_lt) != 0:count = 0while len(self.vis_lt) < 1: # 视野空 可在此设置最小同时打野数count += 1self.p_x = rd.randrange(0, 400, 10)self.p_y = rd.randrange(0, 400, 10)self.position = (self.p_x, self.p_y)for mon in enemy.mst_lt:if abs(mon.p_x - self.p_x) <= 20 and abs(mon.p_y - self.p_y) <= 20: # 视野范围20x20self.vis_lt.append(mon) # 发现野怪mon.vis_lt.append(self) # 同时被发现,可能被多个野怪发现if len(self.vis_lt) != 0 and self.life > 0:print('搜寻%s次 终于找到%s只野怪:' % (count, len(self.vis_lt)), *self.vis_lt, sep='\n')else:print('生命值%s,you have been killed by %s!!!\n累计打野数:%s 累计击败英雄数%s' % (self.life, self.vis_lt[0].name, self.kill_monster, self.kill_hero))else:print('所有野怪均被消灭!!!')os._exit(0)# 2攻击目标,次线程def fight(self):enemy = self.vis_lt[0]if isinstance(enemy, Hero):enemy.life -= self.attackelif isinstance(enemy, Monster):Times = 0while enemy.life > 0 and self.life > 0:Times += 1enemy.life -= self.hurt_levelprint('\n[打野第%s下]---> %s 生命值:%s' % (Times, enemy.name, enemy.life))time.sleep(0.3) # 打野速度体现while enemy.life <= 0 and self.life > 0:self.kill_monster += 1self.money += enemy.money # 增加金币,回血self.life += enemy.moneyprint('[%s Killed %s野怪, 回血%s 生命值:%s]\n\n继续寻找下一只野怪。。。' % (self.name, enemy.name, enemy.money, self.life))self.vis_lt.remove(enemy)monster.mst_lt.remove(enemy)break# 重复搜索,打野过程def mainloop(self):while len(self.vis_lt) != 0:if self.life > 0:self.fight()self.move()else:break# 野怪
class Monster(Life):def __init__(self, name='LIFE', life=500, attack=180, defend=100, money=800, mon_name='野怪'):super().__init__(name, life, attack, defend, money)self.mon_name = mon_nameself.p_x = rd.randrange(0, 400, 10)self.p_y = rd.randrange(0, 400, 10)self.position = (self.p_x, self.p_y)def __str__(self):return '%s:%s life:%s attack:%s defend:%s money:%s positon:%s' % (self.mon_name, self.name, self.life, self.attack, self.defend, self.money, self.position)# 报复目标,次线程def fight(self):while len(self.vis_lt) != 0:enemy = self.vis_lt[0]Times = 0while enemy.life > 0 and self.life > 0:Times += 1enemy.life -= self.attackprint('\n(你被%s报复第%s下)---> %s 生命值:%s' % (self.name, Times, enemy.name, enemy.life))time.sleep(0.3) # 野怪攻击速度体现else:break# 批量生成野怪
class Monsters:def __init__(self):self.mst_lt = []for i in range(1, 20):setattr(self, 'monster_%s' % i, Monster('腥红石像%s号' % i, 1000, attack=180, money=800, mon_name='BUFF'))self.mst_lt.append(eval('self.monster_%s' % i))# 批量生成英雄
class Heroes:def __init__(self):self.mst_lt = []for i in range(1, 10):setattr(self, 'hero_%s' % i, Hero('亚瑟%s号' % i, 1000, 100, 200, ocu_name='对抗路'))self.mst_lt.append(eval('self.hero_%s' % i))# 法师
class Rabbi(Hero):def __init__(self, name):super().__init__(name)pass# 射手
class Shooter(Hero):def __init__(self, name):super().__init__(name)pass# 战士
class Soldier(Hero):def __init__(self, name,):super().__init__(name)pass# 刺客
class Assassin(Hero):def __init__(self, name,):super().__init__(name)pass# 野怪反击
def back_atk():jug = 0 # 跳出循环的判定t = Nonewhile jug == 0:if len(monster.mst_lt) != 0:for mon in monster.mst_lt:if len(mon.vis_lt) != 0:if mon.vis_lt[0].life > 0: # 只攻击最近的敌人t = threading.Thread(target=mon.fight)t.start()else:jug = 1t.join()if __name__ == '__main__':print('#' * 25 + ' welcome to 王者RY ' + '#' * 25 + '\n登录/回车进入:')monster = Monsters()hero1 = Rabbi('妲己')t1 = threading.Thread(target=hero1.mainloop)t2 = threading.Thread(target=back_atk)t1.start()t2.start()
我这个代码写的有多余的部分,其实是我还没有把它调用,比如武器,敌方英雄等等,但是老师给的作业我算是完成了。
还有一个问题就是,这是个自动的,唯一的交互就是改代码数值,当然你可以写个input()实现简单交互,哈哈。
不知道我这第一篇博客有没有人看,嘿嘿,有的话,欢迎提意见和问题。