1 import random, datetime, time 2 import _thread 3 index = 0 4 player_list = [] 5 6 7 8 class player(object): 9 def __init__(self, name, ability): 10 self.name = name 11 self.ability = ability 12 13 def startmatch(self,setplayerlistafter): 14 global index 15 if index == 0: 16 for i in range(0, len(self.setplayerlistafter)): 17 print(self.name, self.ability, 'VS', self.setplayerlistafter[i].name, self.setplayerlistafter[i].ability) 18 if i == len(self.setplayerlistafter) - 1: 19 index = 0 20 i = 0 21 else: 22 index = 1 23 if self.flag == 1: 24 time.sleep(0.1) 25 else: 26 break 27 else: 28 pass 29 if self.flag==1: 30 self.startmatch(self.setplayerlistafter) 31 else: 32 pass 33 34 def stopmatch(self): 35 a=random.random() 36 time.sleep(2+a) 37 self.flag=0 38 39 def setplayerlistafter(self,playerlistafter): 40 self.setplayerlistafter=playerlistafter 41 self.flag=1 42 43 class sports(object): 44 def __init__(self, name, maxplayer): 45 self.name = name 46 self.maxplayer = maxplayer 47 48 def resister(self, player): 49 global player_list 50 print(len(player_list)) 51 if(len(player_list)>=self.maxplayer): 52 print('当前竞技场',self.name,'入场玩家已达到上限',self.maxplayer,'人','玩家',player.name,'入场失败') 53 else: 54 player_list.append(player) 55 print('玩家:',player.name,'入场,战力为:',player.ability) 56 57 def rules(self,player,differ): 58 global player_list 59 self.differ=differ 60 setplayerlistafter=player_list 61 for i in player_list: 62 if i.name==player.name: 63 setplayerlistafter.remove(i) 64 for i in setplayerlistafter[:]: 65 if abs(i.ability-player.ability)>=differ: 66 setplayerlistafter.remove(i) 67 print('............',i.ability,player.ability) 68 player.setplayerlistafter(setplayerlistafter) 69 70 if __name__ == '__main__': 71 print('#################初始化竞技场################') 72 pkground=sports('pkground',10) 73 print('竞技场名字',pkground.name,'最大人数上限',pkground.maxplayer) 74 print('##################玩家入场##################') 75 pkground.resister(player('name1',998)) 76 pkground.resister(player('name2', 958)) 77 pkground.resister(player('name3', 498)) 78 pkground.resister(player('name4', 798)) 79 pkground.resister(player('name5', 1998)) 80 pkground.resister(player('wdx', 1000)) 81 pkground.resister(player('name6', 990)) 82 pkground.resister(player('name7', 957)) 83 pkground.resister(player('name8', 490)) 84 pkground.resister(player('name9', 799)) 85 pkground.resister(player('name10', 1990)) 86 print('#################初始化竞规则################') 87 wdx=player('wdx', 2000) 88 pkground.rules(wdx,500) 89 print('玩家不与已入场玩家战力差距',pkground.differ,'以上PK,玩家不与自己PK') 90 _thread.start_new_thread(wdx.stopmatch,()) 91 wdx.startmatch(wdx.setplayerlistafter)