Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-AttributeError:';粒子';对象没有属性';显示';_Python_Object_Attributes_Attributeerror - Fatal编程技术网

Python-AttributeError:';粒子';对象没有属性';显示';

Python-AttributeError:';粒子';对象没有属性';显示';,python,object,attributes,attributeerror,Python,Object,Attributes,Attributeerror,这个问题被问了很多,但不幸的是,我没有找到适合我问题的答案。如果可能的话,我更喜欢一个通用的答案,因为我是一个尝试学习Python的新手。先谢谢你 以下是我通过使用pygame库学习python基础教程获得的代码: import pygame background_colour = (255, 255, 255) (width, height) = (300, 200) class Particle: def __init__(self, x, y, size):

这个问题被问了很多,但不幸的是,我没有找到适合我问题的答案。如果可能的话,我更喜欢一个通用的答案,因为我是一个尝试学习Python的新手。先谢谢你

以下是我通过使用pygame库学习python基础教程获得的代码:

import pygame

background_colour = (255, 255, 255)
(width, height) = (300, 200)


class Particle:
    def __init__(self, x, y, size):
        self.x = x
        self.y = y
        self.size = size
        self.colour = (0, 0, 255)
        self.thickness = 1


screen = pygame.display.set_mode((width, height))


def display(self):
    pygame.draw.circle(screen, self.colour, (self.x, self.y), self.size,  self.thickness)


pygame.display.set_caption('Agar')
screen.fill(background_colour)
pygame.display.flip()

running = True
my_first_particle = Particle(150, 50, 15)
my_first_particle.display()
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
它用于创建一个游戏窗口,窗口中有一个圆圈。圆被定义为以后以类似方式多次使用的类

我得到了以下错误:

Traceback (most recent call last):
  File "C:/Users/20172542/PycharmProjects/agarTryout/Agar.py", line 29, in <module>
    my_first_particle.display()
AttributeError: 'Particle' object has no attribute 'display'
回溯(最近一次呼叫最后一次):
文件“C:/Users/20172542/PycharmProjects/agarTryout/Agar.py”,第29行,在
my_first_particle.display()
AttributeError:“粒子”对象没有属性“显示”
我不理解什么原则,这个错误的具体解决方案是什么


感谢您花费的时间和精力。

定义的
显示
函数不在粒子内部,而是在脚本的
全局
(不确定此名称是否正确)级别。缩进在python中很重要,因为它没有括号。将函数移动到
\uuuu init\uuu
函数之后,使用相同的缩进


另外,我想您应该将
屏幕
移动到
粒子
定义上方

定义的
显示
函数不在粒子内部,而是在脚本的
全局
(不确定此名称是否正确)级别。缩进在python中很重要,因为它没有括号。将函数移动到
\uuuu init\uuu
函数之后,使用相同的缩进


另外,我想您应该将
屏幕
移动到
粒子
定义上方

根据粒子类的定义,“我的第一个粒子”(粒子的实例)没有显示属性

看起来显示函数的定义应该是粒子类定义的一部分


查看Python类教程。

根据您对粒子类的定义,my_first_粒子(粒子的一个实例)没有显示属性

看起来显示函数的定义应该是粒子类定义的一部分


查看Python类教程。

您的
粒子
类没有定义
显示方法。你的意思是要打电话给别的什么
display
?可能是
pygame
?不确定您到底在问什么-错误很明显。您正在调用一个不存在的方法。您的
Particle
类未定义
display
方法。你的意思是要打电话给别的什么
display
?可能是
pygame
?不确定您到底在问什么-错误很明显。您正在调用一个不存在的方法。