pygame的键盘输入滞后

时间:2019-03-23 14:04:46

标签: python pygame

所以我才刚刚开始学习使用Pygame进行编码游戏,并且我一直在从头开始制作蛇。但是,我遇到了一个与键盘输入事件延迟有关的异常问题,从我的搜索来看,它是来自pygame.draw.rect()循环内的for的,如图所示:

    for row in range(25):
        for column in range(25):
            pygame.draw.rect(screen, blue,
            [(margin + (width+margin) * row,
             margin + (height+margin) * column),
            (width, height)])
            grid[row][column] = 0

            wall1 = pygame.draw.rect(screen, red,
            [(margin + (width+margin) * row,
            margin),
            (width, height)])
            grid[row][0] = 9

            wall2 = pygame.draw.rect(screen, red,
            [(margin,
            margin + (height+margin) * column),
            (width, height)])
            grid[0][column] = 9

            wall3 = pygame.draw.rect(screen, red,
            [(margin + (width+margin) * row,
            margin + 24*(height+margin)),
            (width, height)])
            grid[row][24] = 9

            wall4 = pygame.draw.rect(screen, red,
            [(margin + 24*(width+margin),
            margin + (height+margin) * column),
            (width, height)])
            grid[24][column] = 9

我使用的变量是:

width = 20
height = 20
margin = 5

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
black = (0, 0, 0)

grey = (205, 205, 205)
yellow = (255, 255, 0)

startX = 6
startY = 6
moveX = 1
moveY = 0

grid = []
for row in range(25):
    grid.append([])
    for column in range(25):
        grid[row].append(0)

我确实提出了使用unionall()的解决方案,但是我不知道如何在循环中获取每个绘制的rect的{​​{1}}。另一个解决方案是使用rect,但我完全不知道如何使用它。我也尝试使用pygame.surfarray.make_surface()pygame.time.Clock().tick将FPS设置为60,但是都没有用。请帮忙!

0 个答案:

没有答案