问题
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()
来源:https://stackoverflow.com/questions/58325813/pyinstaller-error-message-name-actor-is-not-defined