Pygame - 未检测到鼠标点击 [英] Pygame - Mouse clicks not getting detected

查看:82
本文介绍了Pygame - 未检测到鼠标点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Pygame 以使用 Python 制作游戏.但是,我遇到了问题.我试图检测玩家当前何时点击屏幕,但我的代码不起作用.我的代码真的搞砸了,还是只是我使用的在线 Pygame 编译器?

I'm learning Pygame to make games w/ Python. However, I'm encountering a problem. I'm trying to detect when the player is currently clicking the screen, but my code isn't working. Is my code actually screwed, or is it just the online Pygame compiler that I'm using?

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

while True:
  pygame.display.update()
  mouse = pygame.mouse.get_pressed()
  if mouse:
    print("You are clicking")
  else:
    print("You released")

当我运行此代码时,输​​出控制台在一秒钟内发送了数千次您正在点击"文本.即使我没有点击屏幕,它仍然会这样说.即使我的鼠标不在屏幕上.只是同样的文字.一遍又一遍.Pygame 是否正确执行我的程序?

When I ran this code, the output console spammed the text "You are clicking", thousands of times in a second. Even when I'm not clicking the screen, it still says this. Even when my mouse isn't over the screen. Just the same text. Over, and over. Is Pygame executing my program correctly?

为了学习 Pygame,我使用了开发者的官方文档.https://www.pygame.org/docs/ 这是一种过时的学习方式吗?这就是为什么我的代码继续运行错误的原因?

To learn Pygame, I am using the official Docs from the developers. https://www.pygame.org/docs/ Is this an outdated way to learn, is this why my code continues to run errors?

推荐答案

pygame.mouse.get_pressed() 在处理事件时进行评估.您需要通过 pygame.event 处理事件.pump()<代码>pygame.event.get().

参见pygame.event.get():

对于游戏的每一帧,您都需要对事件队列进行某种调用.这可确保您的程序可以在内部与操作系统的其余部分进行交互.

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

pygame.mouse.get_pressed() 返回表示所有鼠标按钮状态的布尔值序列.母鸡你必须评估是否any按钮被按下(any(buttons))或订阅按下了一个特殊按钮(例如buttons[0]).

pygame.mouse.get_pressed() returns a sequence of booleans representing the state of all the mouse buttons. Hense you have to evaluate if any button is pressed (any(buttons)) or if a special button is pressed by subscription (e.g. buttons[0]).

例如:

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

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

    # if buttons[0]:  # for the left mouse button
    if any(buttons):  # for any mouse button
        print("You are clicking")
    else:
        print("You released")

    pygame.display.update()


如果你只是想检测鼠标按钮何时被按下和释放,那么你必须实现MOUSEBUTTONDOWNMOUSEBUTTONUP(见pygame.event 模块):


If you just want to detect when the mouse button is pressed respectively released, then you have to implement the MOUSEBUTTONDOWN and MOUSEBUTTONUP (see pygame.event module):

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 800))

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

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

    pygame.display.update()


虽然 pygame.mouse.get_pressed() 返回按钮的当前状态,MOUSEBUTTONDOWNMOUSEBUTTONUP 只发生一次按钮按下.


While pygame.mouse.get_pressed() returns the current state of the buttons, the MOUSEBUTTONDOWN and MOUSEBUTTONUP occurs only once a button is pressed.

这篇关于Pygame - 未检测到鼠标点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆