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

(尝试制作打地鼠游戏)每当我移动鼠标时,地鼠图像的位置似乎比我不移动鼠标时移动的速度慢 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")


手掌心
浏览 148回答 1
1回答

慕莱坞森

性能下降是由于您 self.drawMap()在事件循环中调用而引起的。每个事件调用一次事件循环。每帧可能发生多个事件,尤其是在移动鼠标时。我建议仅在需要时创建地图。将地图渲染为pygame.Surface对象,blit并将地图Surface渲染到每一帧的显示器上。当地图发生变化时,重新创建地图Surface。创建一个在目标Surface上而不是直接在显示Surface上呈现的“draw”方法:def drawMap(self, traget_surf):&nbsp; &nbsp; # draw on traget_surf&nbsp; &nbsp; # [...]添加一个变量map_surf和map_changed = True. map_changed如果已设置和设置,则在应用程序循环中渲染地图map_changed == False。Surface在每一帧中显示blit。每当需要更改地图时,设置以下内容就足够了:map_surf map_changed = Truemap_surf = pygame.Surface(display_surf.get_size())map_changed = Truewhile nbPixel < 50:&nbsp; &nbsp; # [...]&nbsp; &nbsp; if map_changed:&nbsp; &nbsp; &nbsp; &nbsp; self.drawMap(map_surf)&nbsp; &nbsp; &nbsp; &nbsp; map_changed = False&nbsp; &nbsp; # [...]&nbsp; &nbsp; display_surf.blit(map_surf, (0, 0))&nbsp; &nbsp; display_surf.blit(imageWall, tuple(blockAbovePos))&nbsp; &nbsp; display_surf.blit(imageTarget, tuple(newPos))&nbsp; &nbsp; display_surf.blit(imageWall,tuple(initPosToBlit))&nbsp; &nbsp; display_surf.blit(imagePlayer, tuple(mousePos))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python