PyGame不渲染形状?

2024-10-04 05:22:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了以下代码来使用tiles渲染地图,它在文件中循环并将字母转换为tiles(矩形)

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

这是你的名字市政厅.map文件:

xxxxx
xaaax
xaaax
xaaax
xxxxx

Tags: 文件inrectmapforiflinepygame
1条回答
网友
1楼 · 发布于 2024-10-04 05:22:22

当向代码中添加事件循环代码时,代码运行良好。既然你还没有发布整个程序,我所能做的就是发布一个包含你代码的工作程序。你知道吗

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()

相关问题 更多 >