0
A
回答
2
虽然有些人帮助打开一个文件时,我明白你实际上是在寻找导入文本文件作为地图: 我没有写这一点,但已经使用它作为我的比赛为例进行了:
# This code is in the Public Domain
# -- [email protected]
class Map:
def __init__(self, map, tiles):
self.tiles = pygame.image.load(tiles)
l = [line.strip() for line in open(map).readlines()]
self.map = [[None]*len(l[0]) for j in range(len(l))]
for i in range(len(l[0])):
for j in range(len(l)):
tile = l[j][i]
tile = tile_coords[tile]
if tile is None:
continue
elif isinstance(tile, type([])):
tile = random.choice(tile)
cx, cy = tile
if random.choice((0,1)):
cx += 192
if random.choice((0,1)):
cy += 192
self.map[j][i] = (cx, cy)
def draw(self, view, viewpos):
'''Draw the map to the "view" with the top-left of "view" being at
"viewpos" in the map.
'''
sx, sy = view.get_size()
bx = viewpos[0]/64
by = viewpos[1]/64
for x in range(0, sx+64, 64):
i = x/64 + bx
for y in range(0, sy+64, 64):
j = y/64 + by
try:
tile = self.map[j][i]
except IndexError:
# too close to the edge
continue
if tile is None:
continue
cx, cy = tile
view.blit(self.tiles, (x, y), (cx, cy, 64, 64))
def limit(self, view, pos):
'''Limit the "viewpos" variable such that it defines a valid top-left
rectangle of "view"'s size over the map.
'''
x, y = pos
# easy
x = max(x, 0)
y = max(y, 0)
# figure number of tiles in a view, hence max x and y viewpos
sx, sy = view.get_size()
nx, ny = sx/64, sy/64
mx = (len(self.map[0]) - nx) * 64
my = (len(self.map) - ny) * 64
print y, my
return (min(x, mx), min(y, my))
def main():
pygame.init()
win = pygame.display.set_mode((640, 480))
map = Map('map.txt', 'tiles.png')
viewpos = (0,0)
move = False
clock = pygame.time.Clock()
sx, sy = win.get_size()
while 1:
event = pygame.event.poll()
while event.type != NOEVENT:
if event.type in (QUIT, KEYDOWN):
sys.exit(0)
elif event.type == MOUSEBUTTONDOWN:
x, y = viewpos
dx, dy = event.pos
x += dx - sx/2
y += dy - sy/2
viewpos = map.limit(win, (x, y))
move = True
event = pygame.event.poll()
win.fill((0,0,0))
map.draw(win, viewpos)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
1
如果你真的在问如何使用Python加载文件 - 忽略pygame
这一问题 - 那么它很简单。
>>> with open('a.map', 'r') as f:
... for line in f:
... print line,
...
x g g g x
x g g g x
x g x g x
x g g g x
x x x x x
KEY:
x = wall
g = grass/floor
现在不是打印每一行,你可以简单地通过它读取并存储在任何数据结构,你正在使用。
虽然我不知道pygame
什么 - 如果它有一些自定义功能,我不能帮助。
+0
有为pygame构建的地图库,但不清楚id是Jack正在寻找的东西。如:http://www.pygame.org/project-map+loader+for+'tiled'-1158-3692.html – 2012-02-14 14:41:57
0
由于地图只是一个二维数组,因此需要两个循环来完成整个事情。
self.map = [[None]*len(l[0]) for j in range(len(l))]
for i in range(len(l[0])):
for j in range(len(l)):
详情可以找到许多地方。这里有一个: http://www.mechanicalcat.net/richard/log/Python/PyGame_sample__drawing_a_map__and_moving_around_it
里面你会确定画什么。在你的情况下:墙壁,草地或地板,这将是精灵。
相关问题
- 1. 选择将载入地图
- 2. 在地图视图中延迟加载?
- 3. 地图无法在视图中加载
- 4. 谷歌地图V2随机载入地图Android
- 5. 不要在MapActivity中加载地图
- 6. 在传单中加载地图很慢
- 7. 更快地在Gridview中加载图像
- 8. 在iphone中加载地图sdk
- 9. C++ - 在地图循环的地图中插入数据
- 10. OpenLayers:在asp.net中嵌入地图网页
- 11. 在javax.scripting JavaScript环境中导入地图
- 12. 如何在地图中插入结构?
- 13. 在C#中嵌入Google地图#
- 14. 在mapreduce地图类中导入变量
- 15. wget嵌入在php中下载图像
- 16. 谷歌地图api不加载地图
- 17. 谷歌地图API未加载地图
- 18. Javascript正在载入图像
- 19. 从数据库中有效载入网站地图?
- 20. 我的网站无法载入谷歌嵌入地图
- 21. 在OL3中加载本地缩放地图集
- 22. Android谷歌地图贴图未载入“返回”按钮单击
- 23. 谷歌地图Apiv3 +加载地图页面加载+ a为NULL
- 24. 在载入时去特定的地方
- 25. Google地图:进行了更改,现在地图不会加载
- 26. 地图API键未在Android手机上加载地图
- 27. 谷歌地图加载缓慢地图中的PhoneGap
- 28. 如何使用输入流重载将项目插入到地图成员中?
- 29. ListView在视图中一个接一个地加载图像
- 30. 在WPF上的Bing地图中加载自定义图块
您可以在[Python教程](http://docs.python.org/3.2/tutorial/)中找到如何执行'for'循环。 – 2012-02-14 14:51:57