Pygame基于字符串的级别绘制一次就会消失

2024-04-24 00:43:39 发布

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

我使用的是python2.7。重新绘制基于字符串的级别时遇到问题。第一帧画得很好,但之后,就再也画不出来了。我一直在想办法解决这个问题。以下是整个源代码:

import pygame
from pygame.locals import *

xScan = 0
yScan = 0
level = "WWWWWWWW" \
        "WSSSSSSW" \
        "WSSSSSSW" \
        "WSSSSSSW" \
        "WSSSSSSW" \
        "WWWWWWWW"


class Player():
    def __init__(self, color, x, y, width, height, outline_size):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.outline_size = outline_size

    def draw(self):
        pygame.draw.rect(screen, self.color, [self.x * 80, self.y * 80, self.width * 80, self.height * 80], self.outline_size)


def draw_level():
    for tile in level:
        global xScan, yScan
        if tile == "W":
            pygame.draw.rect(screen, [0, 0, 0], [xScan * 80, yScan * 80, 80, 80], 0)
        elif tile == "S":
            pygame.draw.rect(screen, [120, 120, 120], [xScan * 80, yScan * 80, 80, 80], 1)

        if xScan >= 0 and xScan < 7:
            xScan += 1
        elif xScan == 7:
            xScan = 0
            yScan += 1
        elif yScan == 5 and xScan == 7:
            xScan = 0
            yScan = 0


def update():
    screen.fill([255, 255, 255])
    draw_level()
    player.draw()
    pygame.display.flip()

pygame.init()
screen = pygame.display.set_mode([640, 480])
pygame.display.set_caption("Squares n' Tiles!")
player = Player([0, 100, 255], 1, 1, 1, 1, 0)

running = True
while running:
    clock = pygame.time.Clock()
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            if event.key == K_UP and player.y * 80 >= 160:
                player.y -= 1
            if event.key == K_DOWN and player.y * 80 < 320:
                player.y += 1
            if event.key == K_LEFT and player.x * 80 >= 160:
                player.x -= 1
            if event.key == K_RIGHT and player.x * 80 < 480:
                player.x += 1
            if event.key == K_c or event.key == K_r:
                player.x = 1
                player.y = 1
        if event.type == pygame.QUIT:
            running = False
    update()

pygame.quit()

我认为问题在于这部分代码:

^{pr2}$

我试着在前一部分的后半部分弄乱数字。以下是一个主要解决问题的变体(尽管其格式不是很一致):

if xScan >= 7:
    xScan = 0
    yScan += 1
elif yScan == 6:
    xScan = 1
    yScan = 0
else:
    xScan += 1

上面代码的唯一问题是左上角的平铺无法绘制。我如何改变我的代码使其正常工作?在每一帧上,我希望屏幕上填充白色,重新绘制关卡,重新绘制玩家,并翻转屏幕。提前谢谢你的帮助。我也希望能对我的代码结构提出建议。在


Tags: andkeyselfeventifdef绘制level
1条回答
网友
1楼 · 发布于 2024-04-24 00:43:39

问题是这个块没有被执行:

    elif yScan == 5 and xScan == 7:
        xScan = 0
        yScan = 0

这是因为在level字符串中的每个字符上循环,所以达到的最大值是xScan=6,yScan=5。在

相反,在进入draw level函数时只需初始化xScan和{}

^{pr2}$

我建议存储几行而不是一行长的字符:

level = "WWWWWWWW\n" \
        "WSSSSSSW\n" \
        "WSSSSSSW\n" \
        "WSSSSSSW\n" \
        "WSSSSSSW\n" \
        "WWWWWWWW\n" # note the new line characters (\n)

然后可以在地图上进行迭代,如下所示:

^{4}$

enumerate获取一个list(或iterable),并为列表中的每个项返回成对的(index,item),例如:

>>> a = 'test'
>>> for i, c in enumerate(a):
...     print('i is {}, c is {}'.format(i, c))
...
i is 0, c is t
i is 1, c is e
i is 2, c is s
i is 3, c is t

相关问题 更多 >