Name 'actor' Is Not Defined
Solution 1:
You are using the Python library pgzero
(indirectly via importing pgzrun
).
I had refactored my game code into multiple files (imported into the main file) and did also observe the same strange
NameError: name 'Actor' is not defined
error message.
The Actor
class seems to be "private", but can be imported with this simple code line:
from pgzero.builtinsimportActor, animate, keyboard
For background see:
https://github.com/lordmauve/pgzero/issues/61
Update Aug 18, 2019: The screen object cannot be imported since it is created as global variable during runtime (object = instance of the Screen
class) and IDE-supported code completion is not possible then. See the source code: https://github.com/lordmauve/pgzero/blob/master/pgzero/game.py (esp. the def reinit_screen
part)
Solution 2:
This page on the Pygame site will help you run it from your IDE: https://pygame-zero.readthedocs.io/en/stable/ide-mode.html
Essentially, you have to have these two lines of code:
import pgzrun
......
pgzrun.go()
But during coding, the IDE will still complain that objects and functions like screen and Actor are undefined. Pretty annoying. As far as I know, there's no way to fix it, you just have to ignore the complaints and hit Debug > Run. Provided you have no mistakes, the program will compile and run.
With regards to
importpgzrunactor= pgzrun.Actor("dot")
Or
from pgzrun import *
dot=Actor("dot")
Neither of these help integrated development environments like Spyder or Visual Studio recognise objects and functions like screen and Actor
pgzrun doesn't seem to behave like a normal python library. Pygame relies on using pgzrun to run your python script from the command line:
pgzrun mygame.py
Solution 3:
Please define the class Actor or import it from package if you have it in your pip packages or in same dir
Solution 4:
From what I could find on the Pygame Documentation website, Actor
is defined in the pgzrun package. With your current import statements, you would have to call the Actor
constructor by
actor = pgzrun.Actor("dot")
which would show the compiler that Actor
belongs to pgzrun.
Alternatively, if you wanted to just use Actor("dot")
, you could change your import statement to
from pgzrun import *
which means "import everything from pgzrun". This also keeps track of what comes from pgzrun and tells the compiler, but it could lead to issues (eg. if you define your own Actor
constructor in your code, the compiler wouldn't know which one you're trying to use).
Solution 5:
well I just found out It has no problem running with cmd, it has problem with running from the software itself.
Post a Comment for "Name 'actor' Is Not Defined"