簡體   English   中英

Pygame 鼠標移動時不顯示字母瓦的移動

[英]Pygame won't display the movement of the letter tile when the mouse moves

我正在編寫一個拼字游戲,當檢查板上字母的運動時,什么也沒有發生。 我成功地在拼字游戲板上加載了圖像,但是在單擊和移動鼠標時移動字母的命令不起作用。

這是游戲開始運行的第一部分。

    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 調用順序。 在檢查事件之前應先繪制棋盤!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM