PyGame 不渲染形状?

发布于 2025-01-05 22:07:03 字数 874 浏览 1 评论 0原文

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

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

I have made the following code to render the map using tiles, it loops through the file and translates letters into tiles (rectangles);

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

Here is the townhall.map file:

xxxxx
xaaax
xaaax
xaaax
xxxxx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2025-01-12 22:07:03

当添加事件循环代码时,您的代码可以很好地工作。由于您还没有发布整个程序,我所能做的就是发布一个包含您的代码的工作程序。

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

Your code works well when event loop code is added to it. Since you haven't posted the whole program, everything I can do is to post a working program that contains your code.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文