-1

I decided to make a simple snake game, and I've hit a simple problem already. My lines along the line of if keys[pygame.K_RIGHT], if keys[pygame.K_LEFT] etc give me a "pygame has no member "K_RIGHT/K_LEFT" error.

I tried py -m pip uninstall pygame, then reinstalled it, however the error persisted. Even when running the program, pressing left/right/up/down doesn't work, and I get the same error when doing import pygame then pygame.init().

if keys[pygame.K_RIGHT]:
    self.player.moveright

if keys[pygame.K_LEFT]:
    self.player.moveleft

if keys[pygame.K_UP]:
    self.player.moveup

if keys[pygame.K_DOWN]:
    self.player.movedown

if keys[pygame.K_ESCAPE]:
    self._running=False

The sprite doesn't move, and I get the "no member" errors. What is going wrong?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
xupaii
  • 465
  • 4
  • 15
  • 31
  • Who is giving you the error? Pylint in VS Code, the language server, or Python itself when you run the code? – Brett Cannon Jul 19 '19 at 17:38
  • 1
    @BrettCannon Pylint in VScode. – xupaii Jul 19 '19 at 17:57
  • do you get error when you run code ? if not then don't bother it. It can be IDE's warning which doesn't have to mean error in code. MAybe it may not find documentation to display more information for variables. Problem is when you get error message when you run code. – furas Jul 20 '19 at 01:12
  • 1
    maybe it can't move because your code is incorrect - ie. you may have wrong indentions and this code is never executed, or you change values `x,y` but you don't redraw image in new position. etc, and many, many other mistakes. We can't help you without rest of code. – furas Jul 20 '19 at 01:15
  • 1
    Ok, turns out it was just VScode falsely displaying errors. My game does work, thanks for your time! – xupaii Jul 20 '19 at 09:59

1 Answers1

3

It's not necessarily an error, but more of a limitation of pylint. Currently, pylint has a limitation that it cannot recognize C codes, as explained in this answer: pylint 1.4 reports E1101(no-member) on all C extensions:

Only C extensions from trusted sources (the standard library) are loaded into the examining Python process to build an AST from the live module.

K_RIGHT (and all the other constants) are imported as from pygame.constants import * but they are actually C codes defined in src_c/constants.c, which is stored in your python installation of pygame as constants.cpython-[ver]-[arch].so.

There is unfortunately no way to resolve the error. If you don't want VS Code + pylint to be showing them as errors, you can hide them, by adding this to your settings.json:

"python.linting.pylintArgs": [
    "--extension-pkg-whitelist=pygame"
]
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135