【问题标题】:How do I make pygame recognize a single mouse click as a single mouse click? [duplicate]如何让 pygame 将单击鼠标识别为单击鼠标? [复制]
【发布时间】:2021-07-18 18:11:32
【问题描述】:

问题是,当我在我的 pygame 游戏中单击箭头时,pygame 将其识别为多次鼠标单击。

我尝试过的:

像这样创建KeyDownListener 类:

class KeyDownListener:
    def __init__(self):
        self.down = False
        self.is_up = False

    def update(self, key_pressed):
        if not key_pressed:
            self.is_up = True
            self.down = False
        else:
            if self.is_up:
                self.down = True
            else:
                self.down = False

            self.is_up = False

但这没有用,因为当我点击箭头时什么也没发生。

我的Arrow 班级:

    class Arrow(pygame.sprite.Sprite):
        default_imgresizer = pygame.transform.scale
        default_imgflipper = pygame.transform.flip

        def __init__(self, win, x, y, rotate=False):
            pygame.sprite.Sprite.__init__(self)

            self.win = win
            self.x = x
            self.y = y

            self.image = pygame.image.load("./arrow.png")
            self.image = self.default_imgresizer(self.image, [i // 4 for i in self.image.get_size()])

            if rotate:
                self.image = self.default_imgflipper(self.image, True, False)

            self.rect = self.image.get_rect(center=(self.x, self.y))

        def is_pressed(self):
            return pygame.mouse.get_pressed(3)[0] and self.rect.collidepoint(pygame.mouse.get_pos())

我的事件循环:

    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False

        screen.blit(computer_screen, (0, 0))

        current_profile.win.blit(current_profile.image, current_profile.rect)

        arrow_left.win.blit(arrow_left.image, arrow_left.rect)
        arrow_right.win.blit(arrow_right.image, arrow_right.rect)

        if arrow_right.is_pressed():
            with suppress(IndexError):
                current_profile_num += 1
                current_profile = profiles[current_profile_num]
        elif arrow_left.is_pressed():
            with suppress(IndexError):
                current_profile_num -= 1
                current_profile = profiles[current_profile_num]

        current_profile.increase_size()

        back_button.win.blit(back_button.image, back_button.rect)

        if back_button.is_pressed():
            return

        # boy.self_blit()

        pygame.display.update()

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    您必须使用MOUSEDOWN 事件而不是pygame.mouse.get_pressed()pygame.mouse.get_pressed() 返回按钮的当前状态,而 MOUSEBUTTONDOWNMOUSEBUTTONUP 仅在按下按钮时出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 2011-08-10
      相关资源
      最近更新 更多