0

I created a small game with Pygame and MU IDE. Then I wanted to convert the .py file to a .exe file with pyinstaller. It was no problem to create the exe file. But if I want to execute the created exe file I get the following error message: 'name Actor is not defined'. With MU IDE I can execute the corresponding .py file without any problems. How can I fix the problem?

The full error message is:

Hello from the pygame community. https://www.pygame.org/contribute.html

Traceback (most recent call last):   
File "test.py", line 11, in module   
 NameError: name 'Actor' is not defined   
    [1920] Failed to execute script test

Here is the code:

from random import randint
import time
import pygame

HEIGHT = 800
WIDTH = 800
score = 0
time_left = 10
banana = Actor("banana")
monkey = Actor("monkey")

pygame.mixer.init()
pygame.mixer.music.load("music\\music.mp3")
pygame.mixer.music.play(-1)

background = pygame.image.load("images\\background.png")

def draw():
    screen.blit("background",(0,0))
    banana.draw()
    monkey.draw()
    screen.draw.text("Anzahl der gesammelten Bananen: " + str(score), color = "black", topleft=(10,10))
    screen.draw.text("verbleibende Sekunden: " + str(time_left), color = "black", topleft=(10,50))

def place_graphics():
    banana.x = randint(125, 790)
    banana.y = randint(186, 790)
    monkey.x = 50
    monkey.y = 740

def on_mouse_down(pos): 
    global score
    if banana.collidepoint(pos):
        score = score + 1 
        place_graphics()
    if monkey.collidepoint(pos):
        effect = pygame.mixer.Sound("sounds\\monkey.wav")
        effect.play()

def update_time_left():
    global time_left
    if time_left:
        time_left = time_left - 1
    else:
        game_over()

place_graphics()

clock.schedule_interval(update_time_left, 1.0)

def game_over():
    global score
    screen.fill("green")
    screen.draw.text("Game Over: Du hast " + str(score) + " Bananen gesammelt!", topleft=(100,350), fontsize=40)
    screen.draw.text("Magst du nochmal spielen? ja [j] oder nein [n]", topleft=(150,450), fontsize=30)
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
siclaro
  • 137
  • 13
  • 2
    We wont be able to answer your question without your code and the full error message. :) Please see https://stackoverflow.com/help/minimal-reproducible-example – voiDnyx Oct 10 '19 at 15:00
  • 1
    i added the source code and full error message :) – siclaro Oct 10 '19 at 15:05
  • 1
    @siclaro Of course, what do you expect? The class `Actor` is missing in the file. – Rabbid76 Oct 10 '19 at 15:45
  • 2
    You probably need to import the actor class and possibly instantiate it. ```from /path/to/actor import Actor \n actor = Actor() \n banana = actor("banana")``` – DUDANF Oct 11 '19 at 15:37

2 Answers2

0

Actor is not a part of PyGame. It is a class of Pygame Zero. See Actors. Pygame Zero is based on Pygame and adds many classes to Pygame. You have to import pgzrun:

import pgzrun
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
-1

You are supposed to do the import pygame first then import randint

import pgzrun
from random import randint
Itération 122442
  • 2,644
  • 2
  • 27
  • 73