This question is asked a lot, but unfortunately I found no answer fitting my problem. If possible, I prefer a generic answer, since I'm a novice trying to learn Python. Thank you in advance.
This is the code I got by following a tutorial on the basics of python using the pygame library:
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
It is used to create a window for gaming, with a circle in it. The circle is defined as a class to be used multiple times in a similar way later on.
I got the following error:
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'
What principle am I not understanding, and what is the specific solution for this error?
Thank you for your time and effort.
The display
function defined is not inside Particle, instead at the global
(not sure if this name is right) level of your script. Indentation is significant in python, as it does not have brackets. Move the function after your __init__
function, with the same indentation.
Also, I guess you should move screen
above your Particle
definition.