代码
代码如下(示例):
import random
import sys
class Player():
def __init__(self, image ,name , isPlayer):
self.name = name
self.money = 10000
self.isGoingToMove = False
self.movable = True
self.image = image
self.position = 0
self.temp_position = False
self.dice_value = 0
self.locatedBuilding = 0
self.showText = []
self.isPlayer = isPlayer
self.ownedBuildings = []
self.isShowText = False
self.soundPlayList = 0
self.caishen = 0
self.shuaishen = 0
self.tudishen = 0
self.pohuaishen = 0
def judgePosition(self,buildings): # 位置判断 返回值是所在位置的建筑
for each in buildings:
for every in each.location:
if self.position == every:
return each
'''
try:
for every in each.location:
if self.position == every:
print(each.name)
except:
if self.position == every:
print(each.name)
'''
def buyaBuilding(self,isPressYes): # 购买方法
if isPressYes and self.locatedBuilding.owner != self.name:
self.locatedBuilding.owner = self.name
self.locatedBuilding.wasBought = True
self.ownedBuildings.append(self.locatedBuilding)
self.money -= self.locatedBuilding.price
self.showText = [self.name + '购买了' + self.locatedBuilding.name + '!']
self.soundPlayList = 1
return True
else:
return False
def addaHouse(self,isPressYes):
try:
if isPressYes and self.locatedBuilding.owner == self.name:
self.locatedBuilding.builtRoom += 1
self.money -= self.locatedBuilding.payment
self.showText = [self.name + '在' + self.locatedBuilding.name + '上!','盖了一座房子!',\
'有%d' % self.locatedBuilding.builtRoom + '个房子了!',\
"它的过路费是%d" % (self.locatedBuilding.payment * \
(self.locatedBuilding.builtRoom + 1)) ]
self.soundPlayList = 2
return True
else:
return False
except:
pass
def move(self,buildings,allplayers): # 移动方法 返回值是所在的建筑位置
self.dice_value = random.randint(1,6)
self.position += self.dice_value
if self.position >= 16:
self.position -= 16
self.locatedBuilding = self.judgePosition(buildings)
self.isShowText = True
return self.eventInPosition(allplayers)
def eventInPosition(self,allplayers):
building = self.locatedBuilding
if building.name != '空地':
if self.locatedBuilding.wasBought == False: #
if self.isPlayer == True:
textLine0 = self.name +'扔出了' + '%d'% self.dice_value + '点!'
textLine1 = self.name +'来到了' + building.name + '!'
textLine2 = '购买价格:%d' % building.price
textLine3 = '过路收费:%d' % building.payment
textLine4 = '是否购买?
self.showText = [textLine0,textLine1,textLine2,textLine3,textLine4]
return True
else :
self.addaHouse(not self.buyaBuilding(True))
elif building.owner == self.name: # 路过自己的房子开始加盖建筑!
if self.pohuaishen == 1:
textLine0 = self.name + '破坏神附体!'
textLine1 = '摧毁了自己的房子!'
building.owner = 'no'
building.wasBought = False
self.showText = [textLine0,textLine1]
self.pohuaishen = 0
else:
if self.isPlayer == True:
textLine0 = self.name + '扔出了' + '%d'% self.dice_value + '点!'
textLine1 = '来到了ta的'+ self.locatedBuilding.name +'!'
textLine2 = '可以加盖小房子!'
textLine3 = '加盖收费:%d' % building.payment
textLine4 = '是否加盖?'
self.showText = [textLine0,textLine1,textLine2,textLine3,textLine4]
return True
else:
self.addaHouse(True)
else:
for each in allplayers: # 被收费!
if self.locatedBuilding.owner == each.name and each.name != self.name:
if self.caishen == 1:
textLine0 = self.name + '财神附体!'
textLine1 = '免除过路费%d!' % (building.payment * (building.builtRoom + 1))
self.showText = [textLine0,textLine1]
self.caishen = 0
else:
if self.tudishen == 1:
textLine0 = self.name + '土地神附体!'
textLine1 = '强占土地!'
textLine2 = building.name + '现在属于'+ self.name
self.locatedBuilding.owner = self.name
self.showText = [textLine0,textLine1,textLine2]
self.tudishen = 0
else:
if self.pohuaishen == 1:
textLine0 = self.name + '破坏神附体!'
textLine1 = '摧毁了对手的房子!'
building.owner = 'no'
building.wasBought = False
self.showText = [textLine0,textLine1]
self.pohuaishen = 0
else:
textLine0 = self.name + '扔出了' + '%d'% self.dice_value + '点!'
textLine1 = self.name+ '来到了'+ each.name+'的:'
textLine2 = building.name + ',被收费!'
if self.shuaishen == 1:
textLine3 = '过路收费:%d*2!' % (building.payment * (building.builtRoom + 1)*2)
self.shuaishen = 0
else:
textLine3 = '过路收费:%d' % (building.payment * (building.builtRoom + 1))
textLine4 = '哦!'+ self.name +'好倒霉!'
self.showText = [textLine0,textLine1,textLine2,textLine3,textLine4]
self.money -= building.payment * (building.builtRoom + 1)
each.money += building.payment * (building.builtRoom + 1)
self.soundPlayList = 3