2012-02-14 83 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