为什么 MOUSEBUTTONDOWN 在 Pygame 中不起作用?

Why won't MOUSEBUTTONDOWN work in Pygame?

我遇到了一个错误 MOUSEBUTTONDOWN 使用时出现错误。

这里是错误:

if event.type == MOUSEBUTTONDOWN:
NameError: name 'MOUSEBUTTONDOWN' is not defined

这是给出错误的代码:

import pygame
import random
import sys

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

pygame.init()


# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # --- Game logic should go here



    # --- Screen-clearing code goes here

    # Here, we clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.

    # If you want a background image, replace this clear with blit'ing the
    # background image.
    screen.fill(WHITE)

    # --- Drawing code should go here

    monospace = pygame.font.SysFont("Berlin Sans FB", 169)
    label = monospace.render("test", 1, (0, 0, 0))
    screen.blit(label, (100, 100))

    button_rect = pygame.Rect(0, 0, 50, 50)  # start button rectangle

    abort = False
    start = False
    while not abort and not start:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                abort = True

            if event.type == MOUSEBUTTONDOWN:
                if button_rect.collidepoint(event.pos):
                    start = True

        # draw title screen
        # [...]

    done = abort
    while not done:

        while not done:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    done = True



    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)

# Close the window and quit.
pygame.quit()

Stack Overflow 上关于此主题的所有其他资源,我可以找到所有关于 MOUSEBUTTONDOWN 已经有效的错误的讨论。

另请注意,我使用的是 Pycharm。

找到的解决方案:

我没有导入所需的库:

要使 MOUSEBUTTONDOWN 起作用,您需要使用:

from pygame.locals import *

感谢 @Rabbid76 在评论中的回答。

pygame.MOUSEBUTTONDOWN 代替 MOUSEBUTTONDOWNfrom pygame.locals import *


您只需要一个应用程序循环:

import pygame
from pygame.locals import *
import random
import sys

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

pygame.init()

# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("game")
clock = pygame.time.Clock()
monospace = pygame.font.SysFont("Berlin Sans FB", 169)
label = monospace.render("test", 1, (0, 0, 0))

button_rect = pygame.Rect(0, 0, 50, 50)  # start button rectangle

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

        if event.type == MOUSEBUTTONDOWN:
            if not start and button_rect.collidepoint(event.pos):
                start = True

    screen.fill(WHITE)

    if not start:
        # draw title screen
        pygame.draw.rect(screen, "red", button_rect)
    else:
        # draw game 
        # [...]
        screen.blit(label, (100, 100))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

典型的 PyGame 应用程序循环必须: