Pygame 'LEFT' is not defined

2019-08-29 06:44发布

问题:

I'm working on a game. I have pygame imported. I am using Python 3.3 and Pygame 3.3. The same error message all the time "LEFT" is not defined. I did exact copies of what were on the internet as I already did a search for the problem. Here is my code. I have tried a few different ways and neither seem to work.

method 1:

import pygame
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
    ....command

method 2: (basically going all in)

from pygame import *
from pygame.locals import *
import pygame
event = pygame.event.poll()
if event.type == MOUSEBUTTONDOWN and event.button == LEFT:
    ......command

Before telling me, on both those methods I tried switching between "LEFT" and "pygame.LEFT", and so on.

Any ideas?

Hugely appreciated.

回答1:

Define LEFT yourself:

LEFT = 1

The Pygame tutorial I think you are following does exactly that. In other words, the pygame library has no constants for this value, it's just an integer.



回答2:

As Martijn Pieters pointed out, the pygame library has no constants for the possible values of event.button.

Here's what I think is a complete list of the possible values (as a class, so it won't clutter your global namespace):

class MouseButtons:
    LEFT = 1
    MIDDLE = 2
    RIGHT = 3
    WHEEL_UP = 4
    WHEEL_DOWN = 5