5

I have a problem with python programming, when I'm trying to write a game (introduced by the book: Coding Games Python DK 3), it says: name 'Actor' is not defined.

here's my code:

import pgzrun

from random import randint

WIDTH = 400
HEIGHT = 400

dots = []
lines = []

next_dot = 0

for dot in range(0, 10):
    actor = Actor("dot")
    actor.pos = randint(20, WIDTH -20), randint (20, HEIGHT - 20)
    dots.append(actor)

def draw():
    screen.fill("black")
    number = 1
    for dot in dots:
        screen.draw.text(str(number), (dot.pos[0], dot.pos[1] + 12))
    dot.draw()
    number = number + 1

for line in lines:
    screen.draw.line(line[0], line[1], (100, 0, 0))

pgzrun.go()
R Yoda
  • 8,358
  • 2
  • 50
  • 87
mhm
  • 313
  • 1
  • 5
  • 12

11 Answers11

5

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.builtins import Actor, 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)

R Yoda
  • 8,358
  • 2
  • 50
  • 87
  • 1
    Very nice. Finally kids can code from their book without being told the whole time that they are doing it wrong :) – Benice Aug 18 '19 at 06:19
3

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

import pgzrun
actor = 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
Benice
  • 409
  • 3
  • 15
  • See my answer on how to import `Actor`. The `screen` object is still an open issue... https://stackoverflow.com/a/57512175 – R Yoda Aug 15 '19 at 15:36
  • Thanks Yoda, I just tried putting "from pgzero.builtins import Actor, animate, keyboard,screen" into my code and it silenced all the IDE complaints including "Undefined variable: screen". – Benice Aug 18 '19 at 06:19
  • For the `screen` object I have no solution so far :-( – R Yoda Aug 18 '19 at 12:56
3

In chapter 5, page 73 of Coding Games in Python, step 2 is to save the file as numbers.py. This creates a conflict with the built in module numbers. This is the cause of the error "name 'Actor' is not defined". The solution is to save the file with a different name (i.e. follow.py).

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 22 '22 at 00:18
0

Please define the class Actor or import it from package if you have it in your pip packages or in same dir

Nishant Singh
  • 3,055
  • 11
  • 36
  • 74
0

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).

samm82
  • 240
  • 4
  • 14
0

well I just found out It has no problem running with cmd, it has problem with running from the software itself.

mhm
  • 313
  • 1
  • 5
  • 12
0

So, code is correct but I might suspect something. I think you didnt upload a picture called "dot" in pgzero.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I was getting the same error message, until finally I found this topic here. The book version is missing those pgzrun first and last lines.

If you're still getting the error after putting those lines in, I bet your 'python' points to python2. Try this at the command-line:

python -V

If it shows a version of 2, you'll need to fix that. Check

which python

If it's in /usr/bin, you can do:

sudo su
cd /usr/bin
rm python
ln -s python3 python

If it's in an update-alternatives directory, you can run that program to fix it.

0

I had the exact same problem at the exact same code. After weeks of deleting and rewriting everything that has to do with python on my computer, I realised that it runs if you make the screen. ... lines comments by writing # in the start of each of them. I haven't found how to fix it yet, but I will inform when I will.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 10 '22 at 05:37
0

I found the issue. Everything on my computer was uploaded automatically to OneDrive. As a result I had a file with the same name on my computer and on OneDrive and when I tried to run it, it always ran from OneDrive where pygame and pgzero are not installed. I solved it by disconnecting auto upload and by changing the name of the file that was on my computer. Hope this works!

-1

the solution is very simple. do not give the file a name.py name because it conflicts with python numbers.py