本文整理汇总了Python中map.Map.move方法的典型用法代码示例。如果您正苦于以下问题:Python Map.move方法的具体用法?Python Map.move怎么用?Python Map.move使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类map.Map
的用法示例。
在下文中一共展示了Map.move方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: open
# 需要导入模块: from map import Map [as 别名]
# 或者: from map.Map import move [as 别名]
import yaml
from map import Map
if __name__ == '__main__':
with open('sample_map.yaml') as f:
data = yaml.load(f)
map = Map(data)
while True:
direction = input()
if direction.startswith('look'):
item = direction
map.move(direction)
示例2: __init__
# 需要导入模块: from map import Map [as 别名]
# 或者: from map.Map import move [as 别名]
class Game:
def __init__(self):
"""
:type self: object
"""
self.screen = global_data.screen
self.map = Map('WORLD_MAP')
#self.current_map = 'WORLD_MAP'
self.maps = {self.map.name: self.map}
self.player = Player('L')
self.player.move(100, 100)
self.walk_speed = 3
self.clock = pygame.time.Clock()
self.last_input = None
def check_map_width(self, x):
if x >= len(self.map.map_tiles):
return len(self.map.map_tiles)-1
elif x < 0:
return 0
return x
def check_map_height(self, y):
if y >= len(self.map.map_tiles[0]):
return len(self.map.map_tiles[0])-1
elif y < 0:
return 0
return y
def key_return(self):
x_, y_ = 0, 0
if self.last_input[K_LEFT]:
x_ = self.check_map_width((self.map.camera.x*(-1)+self.player.x)/32)
y_ = self.check_map_height((self.map.camera.y*(-1)+self.player.y+42)/32)
elif self.last_input[K_RIGHT]:
x_ = self.check_map_width((self.map.camera.x*(-1)+self.player.x+24)/32)
y_ = self.check_map_height((self.map.camera.y*(-1)+self.player.y+42)/32)
elif self.last_input[K_UP]:
x_ = self.check_map_width((self.map.camera.x*(-1) + self.player.x+16)/32)
y_ = self.check_map_height((self.map.camera.y*(-1) + self.player.y+16*(1))/32)
elif self.last_input[K_DOWN]:
x_ = self.check_map_width((self.map.camera.x*(-1) + self.player.x+16)/32)
y_ = self.check_map_height((self.map.camera.y*(-1) + self.player.y+48)/32)
if self.map.map_tiles[x_][y_].object is not None and self.map.map_tiles[x_][y_].object.type == 'NPC':
if self.map.map_tiles[x_][y_].object.has_dialog:
file_name = u'%s_%s' % (self.player.name, self.map.map_tiles[x_][y_].object.name)
if file_name.upper() not in dialog.DIALOGUES:
dialog.load_dialog(file_name.upper())
dialog.dialog(self.player, self.map, self.map.map_tiles[x_][y_].object.get_position, file_name)
def move_left(self, x_, y_):
if self.map.map_tiles[x_][y_].can_walk:
if not self.map.move(self.walk_speed, 0):
if not (self.player.x + self.walk_speed*(-1)) < 0:
self.player.move(self.walk_speed*(-1), 0)
self.player.change_sprite(self.walk_speed*(-1), 0)
def move_right(self, x_, y_):
if self.map.map_tiles[x_][y_].can_walk:
if not self.map.move(self.walk_speed*(-1), 0):
if not (self.player.x + self.walk_speed) > global_data.screen_width - (self.player.image.get_width()/4):
self.player.move(self.walk_speed, 0)
self.player.change_sprite(self.walk_speed, 0)
def move_up(self, x_, y_):
if self.map.map_tiles[x_][y_].can_walk:
if not self.map.move(0, self.walk_speed):
if not (self.player.y + self.walk_speed*(-1)) < 0:
self.player.move(0, self.walk_speed*(-1))
elif self.map.map_tiles[x_][y_].object is not None:
self.object_map_collider(self.map.map_tiles[x_][y_].object, x_, y_)
self.player.change_sprite(0, self.walk_speed*(-1))
def move_down(self, x_, y_):
if self.map.map_tiles[x_][y_].can_walk:
if not self.map.move(0, self.walk_speed*(-1)):
if not (self.player.y + self.walk_speed) > global_data.screen_height - \
(self.player.image.get_height()/4):
self.player.move(0, self.walk_speed)
self.player.change_sprite(0, self.walk_speed)
def object_map_collider(self, object, x_, y_):
if object.type == 'PORTAL':
if not self.map.map_tiles[x_][y_].object.name in self.maps:
self.maps[self.map.map_tiles[x_][y_].object.name] = Map(self.map.map_tiles[x_][y_].object.name)
self.map = self.maps[self.map.map_tiles[x_][y_].object.name]
""" Recebe o input do jogador e realiza a ação de acordo com a tecla pressionada """
def player_input(self, key_pressed):
"""
calcula qual será a posição futura do personagem em relação ao mapa geral usando a posição da
camera em relação ao mapa e do personagem e do personagem em relação a camera
"""
#x_, y_ = (self.map.camera.x*(-1)+self.player.x)/32, (self.map.camera.y*(-1)+self.player.y)/32
if key_pressed[K_RETURN]:
self.key_return()
elif key_pressed[K_LEFT]:
#.........这里部分代码省略.........
示例3: load_joysticks
# 需要导入模块: from map import Map [as 别名]
# 或者: from map.Map import move [as 别名]
class Engine:
def load_joysticks(self):
self.controllers = []
for i in range(0, pygame.joystick.get_count()):
self.controllers.append(Controller(i))
def __init__(self, x, y, **kw):
self.x = x
self.y = y
self.load_joysticks()
if not len(self.controllers):
self.controllers.append(FallbackController())
self.players_pos = []
self.map = Map(x, y)
self.ais = []
if 'map' in kw:
self.map.load_from_png_file(kw['map'])
self.display = pygame.display.set_mode((x * 50, y * 50))
@staticmethod
def init():
numpass, numfail = pygame.init()
if numfail:
pygame.quit()
raise RuntimeError('Could not initialize %d modules (%d pass)'
% (numfail, numpass))
pygame.key.set_repeat(400, 30) # TODO : regler valeurs
def flip(self):
"""
"""
for x in range(self.x):
for y in range(self.y):
item = self.map[(x, y)]
pygame.draw.circle(self.display, Map.to_color(item),
((50 * x) + 25, (50 * y) + 25), 23)
pygame.display.flip()
def __del__(self):
pass
def __repr__(self):
pass
def update(self, event, position):
ctab = IController.to_tuple_tab()
if event in ctab:
item = self.map[position]
assert item in [Map.PACMAN, Map.GHOST]
return self.map.move(position, (position[0] + ctab[event][0],
position[1] + ctab[event][1]))
else:
return False
def populate(self, ghostno):
uid = 0
for lno, line in enumerate(self.map):
while Map.SPAWN in line:
if ghostno:
ghostno -= 1
self.ais.append(GhostAI((line.index(Map.SPAWN), lno)))
else:
self.players_pos.append((line.index(Map.SPAWN), lno))
line[line.index(Map.SPAWN)] = Map.GHOST
while Map.ENEMY_SPAWN in line:
line[line.index(Map.ENEMY_SPAWN)] = Map.PACMAN
self.ais.append(
PacmanAI((line.index(Map.PACMAN), lno)))
def run(self):
stop = False
self.populate(3 - len(self.controllers))
ticks = [IController.LEFT] * len(self.controllers)
watchs = [IController.RIGHT] * len(self.controllers)
pygame.time.set_timer(USEREVENT, 150)
# tick, watch = (IController.LEFT, IController.RIGHT)
tick_watcher = False
while not stop:
for ev in event.get():
if ev.type == QUIT:
print('Shutdown required, exiting gracefully')
return 0
if ev.type == USEREVENT:
tick_watcher = True
break
compute = False
# print('COUOU')
for idx, ctrl in enumerate(self.controllers):
if ctrl.match_device(ev):
ev2 = ctrl.pump(ev)
# print('matching device', self.controllers.index((ctrl)), ':', ev)
#.........这里部分代码省略.........
示例4: Scene
# 需要导入模块: from map import Map [as 别名]
# 或者: from map.Map import move [as 别名]
class Scene(object):
image = None
def __init__(self, _engine):
super(Scene, self).__init__()
self._ais = []
self._engine = _engine
self._resx, self._resy = _engine.getResolution()
self.surface = pygame.Surface((self._resx, self._resy))
drawText(self.surface, "Wczytywanie mapy...", 48, (255, 255, 255), (self._resx / 2, self._resy / 2))
self._map = Map(_engine)
self._hub = Hub(_engine, self._map)
self._cursor = Cursor(_engine, self._map)
self._ais.append(AI(_engine, self._map, _engine.players[0], 0))
self._ais.append(AI(_engine, self._map, _engine.players[1], 1))
def screenUpdated(self):
self._resx, self._resy = self._engine.getResolution()
self.surface = pygame.transform.smoothscale(self.surface, (self._resx, self._resy))
self._map.screenUpdated()
self._hub.screenUpdated()
self._cursor.screenUpdated()
def show(self):
try:
while self._engine.tick():
for event in pygame.event.get():
if event.type == QUIT:
self._engine.quit()
elif event.type == KEYUP:
if event.key == K_ESCAPE:
raise Pause()
elif event.key == K_TAB:
for n, tank in enumerate(self._engine.players[0]['tanks']):
if tank.focus:
tank.setFocus(False)
n = (n + 1) % len(self._engine.players[0]['tanks'])
self._engine.players[0]['tanks'][n].setFocus(True)
break
if self._engine.state & engine.STATE_FAIL:
self.surface.fill((0, 0, 0))
drawText(self.surface, "Game Over", 50, (140, 0, 0), (self._resx / 2, self._resy / 2))
self._engine.show(self.surface)
continue
if self._engine.state & engine.STATE_WIN:
self.surface.fill((0, 0, 0))
drawText(self.surface, "Gratulacje!", 50, (0, 140, 0), (self._resx / 2, self._resy / 2))
self._engine.show(self.surface)
continue
if self._engine.timeLeft() == (0, 0):
if self._engine.players[0]['score'] > self._engine.players[1]['score']:
raise GameWin()
raise GameOver()
if not len(self._engine.players[0]['tanks']):
raise GameOver()
if not len(self._engine.players[1]['tanks']):
raise GameWin()
for tank in self._engine.players[0]['tanks']:
if tank.focus: break
else:
self._engine.players[0]['tanks'][0].setFocus(True)
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self._map.move(map.DIRECTION_LEFT)
if keys[K_RIGHT]:
self._map.move(map.DIRECTION_RIGHT)
if keys[K_UP]:
self._map.move(map.DIRECTION_UP)
if keys[K_DOWN]:
self._map.move(map.DIRECTION_DOWN)
for ai in self._ais:
ai.update()
self._map.update()
self._hub.update()
self._cursor.update()
self._map.draw(self.surface)
self._hub.draw(self.surface)
self._cursor.draw(self.surface)
self._engine.show(self.surface)
except Pause:
self._engine.state ^= engine.STATE_GAME | engine.STATE_MENU
except GameOver:
self._engine.state |= engine.STATE_FAIL
#.........这里部分代码省略.........