python - 移动鼠标时图像位置不更新/更新较慢
问题描述
(尝试制作打地鼠游戏)每当我移动鼠标时,地鼠图像的位置似乎比我不移动鼠标时移动的速度慢 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")
#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
解决方案
性能下降是由于您 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))
推荐阅读
- user-interface - 尝试为登录屏幕固定背景
- twa - TWA 性能缓慢
- amazon-web-services - S3 存储桶复制对象
- wordpress - 上传的图像未从 mac 苹果系统保存到 wordpress
- javascript - 如何在 chrome 扩展上获得一个有效的 AJAX/PHP 脚本
- c# - NReco 是否支持以编程方式填写 PDF 表单?
- java - 我可以从 JUnit 5 获得测试的代码覆盖率吗?
- sql-server - 如何调整以下 T-SQL 查询
- c# - 自定义 UserControl 的自定义 DependencyProperty 未正确绑定
- python - 为什么我无法从手机连接到计算机?