Python 属性错误:';功能';对象没有属性';设置#u模式';
正在尝试编写游戏代码,但出现此错误 AttributeError:“函数”对象没有“设置模式”属性Python 属性错误:';功能';对象没有属性';设置#u模式';,python,pygame,attributeerror,Python,Pygame,Attributeerror,正在尝试编写游戏代码,但出现此错误 AttributeError:“函数”对象没有“设置模式”属性 from pygame import * import sys from os.path import abspath, dirname from random import randint, choice from IPython.core.display import display SCREEN = display.set_mode((800, 600)) FONT = FONT_PATH
from pygame import *
import sys
from os.path import abspath, dirname
from random import randint, choice
from IPython.core.display import display
SCREEN = display.set_mode((800, 600))
FONT = FONT_PATH + 'space_invaders.ttf'
IMG_NAMES = ['ship', 'mystery',
'enemy1_1', 'enemy1_2',
'enemy2_1', 'enemy2_2',
'enemy3_1', 'enemy3_2',
'explosionblue', 'explosiongreen', 'explosionpurple',
'laser', 'enemylaser']
IMAGES = {name: image.load(IMAGE_PATH + '{}.png'.format(name)).convert_alpha()
for name in IMG_NAMES}
而且
NameError:未定义名称“image”
知道我做错了什么吗?谢谢您可以从pygame导入*中执行
,但这将不允许您调用例如显示。设置_模式
/图像。加载因为显示
和图像
模块
您的代码应该如下所示:
import pygame
import sys
from os.path import abspath, dirname
from random import randint, choice
SCREEN = pygame.display.set_mode((800, 600))
FONT = FONT_PATH + 'space_invaders.ttf'
IMG_NAMES = ['ship', 'mystery',
'enemy1_1', 'enemy1_2',
'enemy2_1', 'enemy2_2',
'enemy3_1', 'enemy3_2',
'explosionblue', 'explosiongreen', 'explosionpurple',
'laser', 'enemylaser']
IMAGES = {name: pygame.image.load(IMAGE_PATH + '{}.png'.format(name)).convert_alpha()
for name in IMG_NAMES}
通常,您应该避免从pygame导入*
进行全局导入,如
(我猜IPython.core.display import display中的是从IDE或类似的东西自动导入的)。您没有初始化图像变量并直接调用函数。您需要初始化图像变量