图像位置不更新/移动鼠标时更新速度较慢

2024-06-23 19:33:09 发布

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

(试着做一个打鼹鼠的游戏)每当我移动鼠标时,鼹鼠图像的位置似乎比我不移动鼠标时移动慢3-5倍,我不确定是什么原因造成的,因为位置应该根据时间的推移进行更新

游戏的屏幕是500x500像素,图像是50x50像素,还有一个10x10的阵列,作为一个地图来决定允许鼹鼠出现的位置

守则:

  1. 从10x10地图中获取随机位置

  2. 每30个刻度更新一个像素的摩尔图片位置

  3. 获取鼠标的位置(一个500x500像素的屏幕)

  4. 获取模块的位置(在10x10地图上)

  5. 在屏幕上绘制图像的顺序:

    • 地图

    • 锤子在移动时移动

    • 摩尔上方的方块

    • 摩尔(增加1个像素)

    • 位于鼹鼠原始位置的块

    • 当锤子不移动时,将其固定住

问题是,当我移动鼠标时,鼹鼠上升的速度要慢得多,我不确定是什么问题。我还使用打印语句进行检查

    def moleGoUp(self):
        nbPixel = 0
        #returns a random position
        initialPos = self.returnRandPosition()
        while nbPixel < 50:
            tickCounter = pygame.time.get_ticks() % 30
            if tickCounter == 0:
                nbPixel += 1
            #gets position of mouse
            mousePos = pygame.mouse.get_pos()
            #blits the background block that the mole is supposed to go to
            blockAbovePos = [initialPos[1] * 50, initialPos[0] * 50 - 50]

            #blits the mole at position (goes up by one pixel every 20 ticks)
            newPos = [initialPos[1] * 50, (initialPos[0]*50 - nbPixel)]
            initPosToBlit = [initialPos[1] * 50, initialPos[0] * 50]
            for event in pygame.event.get():
                mousePos = pygame.mouse.get_pos()
                if event.type == pygame.QUIT:
                    sys.exit()
                #draws the map
                self.drawMap()
                # blits the hammer
                display_surf.blit(imagePlayer, tuple(mousePos))
                # counts how many ticks has passed
                tickCounter = pygame.time.get_ticks() % 30
                print("in event loop")

            display_surf.blit(imageWall, tuple(blockAbovePos))
            display_surf.blit(imageTarget, tuple(newPos))
            #blits the background at the original position of the mole
            display_surf.blit(imageWall,tuple(initPosToBlit))
            #blits the hammer
            display_surf.blit(imagePlayer, tuple(mousePos))
            print("out of event loop")

            #blits the background over the mole
            if nbPixel == 50:
                display_surf.blit(imageWall, (initialPos[1]*50, initialPos[0]*50 - nbPixel))
            pygame.display.update()

打印输出:

in event loop
out of event loop
in event loop
out of event loop
in event loop
out of event loop
in event loop
out of event loop
in event loop
out of event loop


Tags: oftheinloopeventdisplay像素out
1条回答
网友
1楼 · 发布于 2024-06-23 19:33:09

性能下降是由于在事件循环中调用self.drawMap()而导致的。每个事件调用一次事件循环。每帧可能发生多个事件,尤其是移动鼠标时。
我建议仅在需要时创建地图。将贴图渲染为^{}对象,并^{}将贴图曲面渲染到每个帧的显示器上。地图更改后,重新创建地图曲面

创建在目标表面上而不是直接在显示器表面上渲染的“绘制”方法:

def drawMap(self, traget_surf):
    # draw on traget_surf
    # [...]

添加变量map_surfmap_changed = True。如果设置了map_changed并设置了map_changed == False,则在应用程序循环中呈现贴图blit将{}表面添加到每个帧中的显示器。每当需要更改映射时,只需设置map_changed = True

map_surf = pygame.Surface(display_surf.get_size())
map_changed = True

while nbPixel < 50:

    # [...]

    if map_changed:
        self.drawMap(map_surf)
        map_changed = False


    # [...]

    display_surf.blit(map_surf, (0, 0))

    display_surf.blit(imageWall, tuple(blockAbovePos))
    display_surf.blit(imageTarget, tuple(newPos))
    display_surf.blit(imageWall,tuple(initPosToBlit))
    display_surf.blit(imagePlayer, tuple(mousePos))

相关问题 更多 >

    热门问题