本文整理汇总了Python中position.Position.set方法的典型用法代码示例。如果您正苦于以下问题:Python Position.set方法的具体用法?Python Position.set怎么用?Python Position.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类position.Position
的用法示例。
在下文中一共展示了Position.set方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Tile
# 需要导入模块: from position import Position [as 别名]
# 或者: from position.Position import set [as 别名]
class Tile( object ):
@staticmethod
def create( tile_type, player ):
return tile_type_object[ tile_type ]( player )
def __init__( self, tile_type, player ):
self.__position = Position()
self.__render_position = [ 0, 0 ]
self.image = texture_manager.load( tile_image_path[ tile_type ] )
self.player = player
self.type = tile_type
self.beetle_ontop = None
# Set the tile type colour
self.image.fill( tile_type_colours[ tile_type ], special_flags = pygame.BLEND_ADD )
# Set the player colour, i can't think of a better way to do this atm...
for y in range( self.image.get_height() ):
for x in range( self.image.get_width() ):
colour = self.image.get_at( ( x, y ) )
if colour[ 0 ] == 255 and colour[ 1 ] == 255 and colour[ 2 ] == 255:
self.image.set_at( ( x, y ), player.get_colour() )
def get_position( self ):
return self.__position
def set_position( self, position, board ):
self.__position.set( position )
self.__render_position = Draw.get_render_position( self.__position )
def render( self, surface ):
Draw.image( surface, self.image, self.__position )
def adjacent_position_movement_is_wide_enough( self, board, begin, end ):
# distance between begin and end should be 1
# firstly find adjacent positions that are shared by begin and end
begin_adjacent_positions = list( begin.get_adjacent_positions() )
end_adjacent_positions = list( end.get_adjacent_positions() )
shared_adjacent_positions = [ x for x in begin_adjacent_positions if x in end_adjacent_positions ]
assert len( shared_adjacent_positions ) == 2
# if both shared positions are occupied then we cannot move between them
# i.e. if either position is free, then we can pass
if len( board.get_tiles_with_position( shared_adjacent_positions[0] ) ) == 0 or \
len( board.get_tiles_with_position( shared_adjacent_positions[1] ) ) == 0:
return True
return False
def position_is_reachable( self, board, target_position, free_positions ):
# we need to walk from get_position() to target_position
# whilst checking that the pieces either side of each position on the route...
#...are large enough for us to fit through
# it's a stupid recurrsive search but does the job (i think)
def can_create_path_between( begin, end, visited_nodes ):
adjacent_positions_to_begin = begin.get_adjacent_positions()
adjacent_free_positions_to_begin = [ x for x in adjacent_positions_to_begin if x in free_positions ]
# calculate if we can move to adjacent position
valid_positions = [ x for x in adjacent_free_positions_to_begin \
if self.adjacent_position_movement_is_wide_enough( board, begin, x ) \
and x not in visited_nodes ]
# Find positions that have an adjacent tile that is not the current tile
valid_positions_with_adjacent_tile = []
for position in valid_positions:
adjacent_tiles = [ x for x in board.get_adjacent_tiles( position ) if x != self ]
if len( adjacent_tiles ) > 0:
valid_positions_with_adjacent_tile.append( position )
visited_nodes.extend( valid_positions_with_adjacent_tile )
if end in valid_positions_with_adjacent_tile:
return True
for position in valid_positions_with_adjacent_tile:
if can_create_path_between( position, end, visited_nodes ):
return True
return False
return can_create_path_between( self.get_position(), target_position, [] )
def has_occupied_shared_tile( self, board, a, b, apart_from = [] ):
a_adjacent_positions = list( a.get_adjacent_positions() )
b_adjacent_positions = list( b.get_adjacent_positions() )
shared_adjacent_positions = [ x for x in a_adjacent_positions if x in b_adjacent_positions ]
assert len( shared_adjacent_positions ) == 2
shared_adjacent_tiles = [
len( [ x for x in board.get_tiles_with_position( shared_adjacent_positions[0] ) if x not in apart_from ] ),
len( [ x for x in board.get_tiles_with_position( shared_adjacent_positions[1] ) if x not in apart_from ] )
]
return shared_adjacent_tiles.count( 0 ) == 1
示例2: __init__
# 需要导入模块: from position import Position [as 别名]
# 或者: from position.Position import set [as 别名]
class Sprite:
def __init__(self, pos=None, sheet=None, parent=None, width=None, height=None):
if pos is None:
self.pos = Position(pos=[0, 0])
else:
self.pos = Position(pos=pos)
self.parent = parent
self.width = width
self.height = height
self.sheet = QPixmap(sheet)
self.pix = None
self.states = {'static':
{'pix' : [],
'delay' : 1000}}
self.state = 'static'
self.step = 0
self.delay = 50
self.timer = QTimer()
self.timer.timeout.connect(self.animate)
self.timer.setInterval(self.delay)
self.timer.start()
def set_sheet(self, sheet):
self.sheet = QPixmap(sheet)
def set_static(self, pix_pos=None, x_shift=None, y_shift=None, x_offset=0, y_offset=0,
z=1, scale=1.0):
if pix_pos is None:
pix_pos = Position()
else:
pix_pos = Position(pos=pix_pos)
if x_shift is None or y_shift is None:
self.states['static']['pix'].append(self.sheet)
else:
self.states['static']['pix'].append(self.sheet.copy(pix_pos.x(), pix_pos.y(), x_shift, y_shift))
self.pix = self.parent.m_scene.addPixmap(self.states['static']['pix'][0])
self.pix.setPos(self.pos.x(), self.pos.y())
self.pix.setOffset(x_offset, y_offset)
self.pix.setZValue(z)
self.pix.setScale(scale)
def move_sprite(self, pos):
self.pos.set(pos)
self.pix.setPos(self.pos.x(), self.pos.y())
def set_state(self, state):
self.state = state
self.timer.setInterval(self.states[state]['delay'])
self.step = 0
self.pix.setPixmap(self.states[state]['pix'][self.step])
def add_state(self, name, maps=[], delay=50):
self.states[name] = {}
self.states[name]['pix'] = maps
self.states[name]['delay'] = delay
def animate(self):
self.step += 1
if self.step >= len(self.states[self.state]['pix']):
self.step = 0
#print('step:', self.step)
self.pix.setPixmap(self.states[self.state]['pix'][self.step])