简体   繁体   English

Python pygame pygame.mouse.get_pressed()[0] 当我移动鼠标时的响应,而不是当鼠标在同一位置时的响应 position

[英]Python pygame pygame.mouse.get_pressed()[0] responses when i move mouse, not when the mouse is in the same position

Im making a 2d game where you can shoot, so the logic is when pygame.mouse.get_pressed()[0] (mouse is pressed) bullets are created.我正在制作一个可以射击的 2d 游戏,所以逻辑是在pygame.mouse.get_pressed()[0] (按下鼠标)子弹时。 Problem is, when im holding my finger on mouse, and key is pressed, my code creates one bullet when i start clicking, when im still clicking nothings happening, when i move my mouse, the bullets start to create.问题是,当我将手指放在鼠标上并按下键时,我的代码会在我开始单击时创建一颗子弹,当我仍然单击时什么也没发生,当我移动鼠标时,子弹开始创建。 Pygame dont create bullets when the mouse is in a single position, i have to move mouse. Pygame 当鼠标在单个 position 时不要创建子弹,我必须移动鼠标。 Whats wrong.怎么了。 Here is the important fragment of code.这是代码的重要片段。 (its in a def run()) (它在 def run() 中)

its in a loop ( https://i.stack.imgur.com/0Y86f.png )它在一个循环中( https://i.stack.imgur.com/0Y86f.png

pygame.mouse.get_pressed() is not an event, but returns the current state of the button. pygame.mouse.get_pressed()不是事件,而是返回按钮当前的state。 You have to use the MOUSEBUTTONDOWN event.您必须使用MOUSEBUTTONDOWN事件。 See also Pygame mouse clicking detection .另请参阅Pygame 鼠标点击检测
pygame.mouse.get_pressed() returns a list of Boolean values that represent the state ( True or False ) of all mouse buttons. pygame.mouse.get_pressed()返回 Boolean 个值的列表,代表所有鼠标按钮的 state( TrueFalse )。 The state of a button is True as long as a button is held down.只要按下按钮,按钮的 state 就是True If you just want to detect when the mouse button is pressed or released, then you have to implement the MOUSEBUTTONDOWN and MOUSEBUTTONUP (see pygame.event module):如果您只想检测鼠标按钮何时被按下或释放,则必须实现MOUSEBUTTONDOWNMOUSEBUTTONUP (请参阅pygame.event模块):

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

        if event.type == pygame.MOUSEBUTTONDOWN:
            print("clicked", event.button)
        if event.type == pygame.MOUSEBUTTONUP:
            print("released", event.button)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM