如何解决鼠标移动时Pygame不会显示字母磁贴的移动
我正在编写一个拼字游戏,当检查板上字母的运动时,没有任何反应。我成功地将图像加载到拼字板上,但是单击并移动鼠标时移动字母的命令不起作用。
这是游戏开始运行的第一部分。
def rungame(self):
"""Start the main loop of the game."""
while True:
self.check_events()
self.draw_board()
self.update_screen()
以及接下来的事件:
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
a_selected = self.a_rect.collidepoint(mouse_pos)
if a_selected:
self.dragging = True
elif event.type == pygame.MOUSEBUTTONUP:
self._check_tile_letter_collision()
self._drop_letter()
self.dragging = False
elif event.type == pygame.MOUSEMOTION:
mouse_pos = pygame.mouse.get_pos()
self._move_let(mouse_pos)
到目前为止,一切看起来都很好。但问题来了:
def _move_let(self,mouse_pos):
if self.dragging:
if 219 < mouse_pos[0] < 680 and 118 < mouse_pos[1] < 580:
self.a_rect.x = mouse_pos[0]
self.a_rect.y = mouse_pos[1]
print(self.a_rect.x,self.a_rect.y)
所以我尝试打印结果并查看是否有一些错误,但打印调用工作正常,我看到归因于鼠标图像的 x 和 y 值,但图像没有移动。 我确保在最后调用了 blit 和 display 方法,所以这不是问题。
解决方法
已解决,问题出在 rungame 方法中 tve 调用的顺序。 在查看事件之前应该先画板!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。