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. jQuery Mobile Google地圖 - 地圖僅在重新載入後顯示
- 4. 在Python中加載地理地圖
- 5. 在InfoWindow中嵌入地圖
- 6. 在C++地圖中插入
- 7. 在地圖視圖中延遲加載?
- 8. 地圖無法在視圖中加載
- 9. 插入載體爲我停留在試圖找出如何插入載體在地圖中的值在C++
- 10. 谷歌地圖V2隨機載入地圖Android
- 11. jquery自動從mysql載入地圖
- 12. 有效地載入重複圖像
- 13. Slick2D +平鋪無法載入地圖
- 14. 在用戶位置加載地圖 - iOS中的谷歌地圖
- 15. 在Chrome中未加載Typescript源地圖
- 16. 在Google地圖中加載動畫infowindow
- 17. 在UIwebview中加載本地圖像
- 18. 在webview中加載本地圖像
- 19. 更快地在Gridview中加載圖像
- 20. 在AngularJS中加載異步Google地圖
- 21. 在傳單中加載地圖很慢
- 22. 不要在MapActivity中加載地圖
- 23. 在iphone中加載地圖sdk
- 24. 地圖不能在android中加載
- 25. 在本地HTML中加載圖像
- 26. 重載[]在C++ STL地圖
- 27. C++ - 在地圖循環的地圖中插入數據
- 28. 在C++中插入嵌套地圖
- 29. 在MonoGame中導入TMX地圖
- 30. 如何在地圖中輸入條目?
您可以在[Python教程](http://docs.python.org/3.2/tutorial/)中找到如何執行'for'循環。 – 2012-02-14 14:51:57