亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

移動鼠標時圖像位置不更新/更新較慢

移動鼠標時圖像位置不更新/更新較慢

手掌心 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 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號