为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

手掌心 2022-05-24 12:49:56
(尝试制作打地鼠游戏)每当我移动鼠标时,地鼠图像的位置似乎比我不移动鼠标时移动的速度慢 3-5 倍,我不确定是什么导致它,因为位置应该根据经过的时间进行更新。游戏的屏幕为 500x500 像素,图像为 50x50 像素,还有一个 10x10 的阵列作为地图来决定允许痣出现的位置编码:从 10x10 地图中获取随机位置每 30 个刻度更新痣图的位置一个像素获取鼠标的位置(一个500x500像素的屏幕)获得鼹鼠应该去的方块的位置(在 10x10 地图上)图像在屏幕上绘制的顺序:地图移动时的锤子鼹鼠上方的方块痣(上升 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")
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

性能下降是由于您 self.drawMap()在事件循环中调用而引起的。每个事件调用一次事件循环。每帧可能发生多个事件,尤其是在移动鼠标时。

我建议仅在需要时创建地图。将地图渲染为pygame.Surface对象,blit并将地图Surface渲染到每一帧的显示器上。当地图发生变化时,重新创建地图Surface。


创建一个在目标Surface上而不是直接在显示Surface上呈现的“draw”方法:


def drawMap(self, traget_surf):

    # draw on traget_surf

    # [...]

添加一个变量map_surf和map_changed = True. map_changed如果已设置和设置,则在应用程序循环中渲染地图map_changed == False。Surface在每一帧中显示blit。每当需要更改地图时,设置以下内容就足够了:map_surf 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))


查看完整回答
反对 回复 2022-05-24
  • 1 回答
  • 0 关注
  • 147 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信