PyGame没有呈现形状?

时间:2012-02-14 17:03:27

标签: python render draw pygame rect

我制作了以下代码来使用图块渲染地图,它遍历文件并将字母转换为图块(矩形);

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

1 个答案:

答案 0 :(得分: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()