Pygame 'LEFT' is not defined

泄露秘密 提交于 2019-12-25 02:53:10

问题


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


来源:https://stackoverflow.com/questions/14873970/pygame-left-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!