第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

移動(dòng)鼠標(biāo)時(shí)圖像位置不更新/更新較慢

移動(dòng)鼠標(biāo)時(shí)圖像位置不更新/更新較慢

手掌心 2022-05-24 12:49:56
(嘗試制作打地鼠游戲)每當(dāng)我移動(dòng)鼠標(biāo)時(shí),地鼠圖像的位置似乎比我不移動(dòng)鼠標(biāo)時(shí)移動(dòng)的速度慢 3-5 倍,我不確定是什么導(dǎo)致它,因?yàn)槲恢脩?yīng)該根據(jù)經(jīng)過(guò)的時(shí)間進(jìn)行更新。游戲的屏幕為 500x500 像素,圖像為 50x50 像素,還有一個(gè) 10x10 的陣列作為地圖來(lái)決定允許痣出現(xiàn)的位置編碼:從 10x10 地圖中獲取隨機(jī)位置每 30 個(gè)刻度更新痣圖的位置一個(gè)像素獲取鼠標(biāo)的位置(一個(gè)500x500像素的屏幕)獲得鼴鼠應(yīng)該去的方塊的位置(在 10x10 地圖上)圖像在屏幕上繪制的順序:地圖移動(dòng)時(shí)的錘子鼴鼠上方的方塊痣(上升 1 個(gè)像素)鼴鼠原始位置的方塊錘子不動(dòng)問(wèn)題是,當(dāng)我移動(dòng)鼠標(biāo)時(shí),痣的上升速度要慢得多,我不確定是什么問(wèn)題。我還使用打印語(yǔ)句進(jìn)行檢查。   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貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

性能下降是由于您 self.drawMap()在事件循環(huán)中調(diào)用而引起的。每個(gè)事件調(diào)用一次事件循環(huán)。每幀可能發(fā)生多個(gè)事件,尤其是在移動(dòng)鼠標(biāo)時(shí)。

我建議僅在需要時(shí)創(chuàng)建地圖。將地圖渲染為pygame.Surface對(duì)象,blit并將地圖Surface渲染到每一幀的顯示器上。當(dāng)?shù)貓D發(fā)生變化時(shí),重新創(chuàng)建地圖Surface。


創(chuàng)建一個(gè)在目標(biāo)Surface上而不是直接在顯示Surface上呈現(xiàn)的“draw”方法:


def drawMap(self, traget_surf):

    # draw on traget_surf

    # [...]

添加一個(gè)變量map_surf和map_changed = True. map_changed如果已設(shè)置和設(shè)置,則在應(yīng)用程序循環(huán)中渲染地圖map_changed == False。Surface在每一幀中顯示blit。每當(dāng)需要更改地圖時(shí),設(shè)置以下內(nèi)容就足夠了: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))


查看完整回答
反對(duì) 回復(fù) 2022-05-24
  • 1 回答
  • 0 關(guān)注
  • 167 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)