本文整理汇总了Python中Map.Map.addAction方法的典型用法代码示例。如果您正苦于以下问题:Python Map.addAction方法的具体用法?Python Map.addAction怎么用?Python Map.addAction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Map.Map
的用法示例。
在下文中一共展示了Map.addAction方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from Map import Map [as 别名]
# 或者: from Map.Map import addAction [as 别名]
class Game:
def __init__(self):
self.map = Map()
self.numUnits = 1
self.numPlayers = 1
self.playerIndex = -1
self.turn = 1
self.players = []
def __saveable__(self):
""" Returns a saveable representation of the game. """
d = {}
d['players'] = map(saveable, self.players)
d['map'] = saveable(self.map)
d['turn'] = self.turn
d['playerIndex'] = self.playerIndex
d['currentPlayer'] = saveable(self.currentPlayer)
return d
@classmethod
def __load__(cls, d):
g = cls()
g.map = load(Map, d['map'], game=g)
g.players = map(partial(load, Player, game=g), d['players'])
g.turn = d['turn']
g.playerIndex = d['playerIndex']
g.currentPlayer = load(Player, d['currentPlayer'], game=g)
g.numUnits = len(g.map.units)
g.numPlayers = len(g.players)
return g
def save(self, filename):
d = saveable(self)
with open(filename, 'w') as f:
json.dump(d, f)
print 'Game saved.'
@classmethod
def load(cls, filename):
Player.loadedPlayers = {}
with open(filename) as f:
d = json.load(f)
print 'Game loaded.'
return load(cls, d)
def start(self):
self.players.append(HumanPlayer(self))
for x in xrange(self.numPlayers - 1):
self.players.append(AIPlayer(self))
self.map.createSquareMap(self.numUnits, self.players, 10, 10, 50)
self.numUnits *= self.numPlayers
def cyclePlayers(self):
self.playerIndex += 1
if self.playerIndex == self.numPlayers:
self.playerIndex = -1
self.currentPlayer = None # None indicates the turn is over
else:
self.currentPlayer = self.players[self.playerIndex]
def nextPlayerAction(self):
self.cyclePlayers()
if self.currentPlayer:
self.currentPlayer.doTurn()
else:
self.nextTurn()
def nextTurn(self):
self.save('savefile.save')
self.resetPlayerCycle()
self.nextPlayerAction()
self.turn += 1
def resetPlayerCycle(self):
self.playerIndex = -1
def moveAction(self, fun):
if isinstance(self.currentPlayer, HumanPlayer):
self.currentPlayer.currentUnit.tile.setChosenByReach(self.currentPlayer.currentUnit.moves)
self.map.addAction(partial(self.currentPlayer.currentUnit.move, fun=fun))
return True
return False
def nextUnitAction(self):
if isinstance(self.currentPlayer, HumanPlayer):
self.currentPlayer.nextUnitAction()
return True
return False
def nextTurnAction(self):
if isinstance(self.currentPlayer, HumanPlayer):
self.currentPlayer.endTurn()
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from Map import Map [as 别名]
# 或者: from Map.Map import addAction [as 别名]
#.........这里部分代码省略.........
@classmethod
def load(cls, filename):
Player.loadedPlayers = {}
with open(filename) as f:
d = json.load(f)
print 'Game loaded.'
return load(cls, d)
def start(self):
if game.mode == 'single':
self.players.append(HumanPlayer(self))
for x in xrange(self.numPlayers - 1):
self.players.append(AIPlayer(self))
else:
for x in xrange(self.numPlayers):
self.players.append(HumanPlayer(self))
if self.mapSize == 0:
self.map.createSquareMap(self.numUnits, self.players, 50, 1)
elif self.mapSize == 1:
self.map.createSquareMap(self.numUnits, self.players, 35, 2)
else:
self.map.createSquareMap(self.numUnits, self.players, 20, 3)
self.numUnits *= self.numPlayers
def cyclePlayers(self):
self.playerIndex += 1
if self.playerIndex == self.numPlayers:
self.playerIndex = -1
self.currentPlayer = None # None indicates the turn is over
else:
self.currentPlayer = self.players[self.playerIndex]
if isinstance(self.currentPlayer, HumanPlayer):
self.turnUnits = filter(lambda x: x.owner == self.currentPlayer, self.map.units)
def nextPlayerAction(self):
self.cyclePlayers()
if self.currentPlayer:
self.currentPlayer.doTurn()
else:
self.nextTurn()
def nextTurn(self):
self.save('savefile.save')
self.resetPlayerCycle()
self.nextPlayerAction()
self.turn += 1
def resetPlayerCycle(self):
self.playerIndex = -1
def moveAction(self, fun):
if isinstance(self.currentPlayer, HumanPlayer):
if not self.currentPlayer.currentUnit.moves:
print 'This unit cannot move!'
return False
self.currentPlayer.currentUnit.tile.setChosenByReach(
self.currentPlayer.currentUnit.moves)
self.map.addAction(partial(
self.currentPlayer.currentUnit.move, fun=fun))
return True
return False
def attackAction(self, fun=None):
if isinstance(self.currentPlayer, HumanPlayer):
if not self.currentPlayer.currentUnit.range or \
self.currentPlayer.currentUnit.range in ((0,), [0]):
print 'This unit cannot attack!'
return False
unit = self.currentPlayer.currentUnit
unit.tile.setChosenByDist(unit.range)
self.map.addAction(partial(unit.attackTile, fun=fun))
return True
return False
def buildAction(self, building):
if isinstance(self.currentPlayer, HumanPlayer):
return self.currentPlayer.currentUnit.build(building)
return False
def recruitAction(self, unit):
if isinstance(self.currentPlayer, HumanPlayer):
return self.currentPlayer.currentUnit.recruit(unit)
return False
def nextUnitAction(self):
if isinstance(self.currentPlayer, HumanPlayer):
self.currentPlayer.nextUnitAction()
return True
return False
def nextTurnAction(self):
if isinstance(self.currentPlayer, HumanPlayer):
self.currentPlayer.endTurn()
return True
return False