Pygame 'LEFT' is not defined

2019-08-29 06:12发布

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.

2条回答
Anthone
2楼-- · 2019-08-29 06:55

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.

查看更多
Explosion°爆炸
3楼-- · 2019-08-29 07:04

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
查看更多
登录 后发表回答