2012-02-14 108 views
-2

我已經做了下面的代碼來使用貼圖來渲染地圖,它通過文件循環並將字母翻譯成瓷磚(矩形)。PyGame不渲染形狀?

currtile_x = 0 
currtile_y = 0 
singlerun = 1 

if singlerun == 1: 
    singlerun = 0 
    with open('townhall.map', 'r') as f: 
     for line in f: 
       for character in line: 
        if character == "\n": 
         currtile_y += 10 
        else: 
         if character == "x": 
          pygame.draw.rect(screen, (1,2,3), (currtile_x, currtile_y, 10, 10), 0) 
          currtile_x += 10 
         else: 
          if character == "a": 
           pygame.draw.rect(screen, (0,255,255), (currtile_x, currtile_y, 10, 10), 0) 
           currtile_x += 10 

這裏是townhall.map文件:

xxxxx 
xaaax 
xaaax 
xaaax 
xxxxx 
+0

你的問題到底是什麼? – Stedy 2012-02-14 22:42:46

回答

0

您的代碼效果很好,當事件循環代碼添加到它。既然你還沒有發佈整個程序,我所能做的就是發佈一個包含你的代碼的工作程序。

import pygame 
from pygame.locals import * 

pygame.init() 
screen = pygame.display.set_mode((300, 300)) 

currtile_x = 0 
currtile_y = 0 
with open('townhall.map') as f: 
    for line in f: 
     for character in line: 
      if character == '\n': 
       currtile_y += 10 
       currtile_x = 0 
      elif character == 'x': 
       pygame.draw.rect(screen, (0,0,0), (currtile_x, currtile_y, 10, 10), 0) 
       currtile_x += 10 
      elif character == 'a': 
       pygame.draw.rect(screen, (0,255,255), (currtile_x, currtile_y, 10, 10), 0) 
       currtile_x += 10 

running = True 
while running: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      running = False 
    pygame.display.update() 
+0

謝謝! 現在將其合併到我的原始代碼! – pixelgeer 2012-02-14 17:27:28

+0

它在我的原始代碼中不起作用; http://pastebin.com/4n8k4fTh – pixelgeer 2012-02-14 17:34:31

+0

@pixelgeer:該代碼有幾個問題......但最明顯的是,地圖(矩形)被渲染一次,然後背景和其他東西被繪在每一次迭代之上。 – 2012-02-14 18:14:46