Name 'Actor' is not defined

Asked by At

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)

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

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


4 Answers

Nishant Singh On

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

samm82 On

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

mhm On

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

Lindsay Fowler On

This page on the Pygame site will help you run it from your IDE:

Essentially, you have to have these two lines of code:

import pgzrun

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


from pgzrun import *

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: