问题描述
我正在编写一个拼字游戏,当检查板上字母的运动时,没有任何反应。我成功地将图像加载到拼字板上,但是单击并移动鼠标时移动字母的命令不起作用。
这是游戏开始运行的第一部分。
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 调用的顺序。 在查看事件之前应该先画板!