#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Name not defined


    Hi, spent a bit too long tryin to figure this out and failing, i keep getting and name error - name 'Particle' is not defined and its doin my head in a bit any advice would be ENORMOUSLY welcome

    Code:
    import pygame
    import random
    import math
    
    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
    		self.speed = 0
    		self.angle = 0
    		
    	def display(self):
    		pygame.draw.circle(screen, self.colour, (int(self.x),int(self.y)), self.size, self.thickness)
    		
    	def move(self):
    		self.x += math.sin(self.angle) * self.speed
    		self.y -= math.cos(self.angle) * self.speed
    
    screen = pygame.display.set_mode((width,height))
    pygame.display.set_caption('first game')
    
    number_of_particles = 10
    my_particles = []
    
    
    for n in range(number_of_particles):
    	size = random.randint(10,20)
    	x = random.randint (size, width-size)
    	y = random.randint (size, height-size)
    	
    	particle = Particle((x,y), size)
    	particle.speed = random.random()
    	particle.angle = random.uniform(0, math.pi*2)
    	
    	my_particles.append(particle)
    	
    
    
    running = True
    while running:
    	for event in pygame.event.get():
    		if event.type == pygame.QUIT:
    			running = False
    			
    	screen.fill(back_ground_colour)
    	
    	for particle in my_particles:
    		particle.move()
    		particle.display()
    	pygame.display.flip()
    the problem is in the for n in range loop nearish the bottom, please help, matt
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Mind your typing. You have no class named Particle; you do however have a class named particle. Python (along with most programming languages) is case sensitive.

    Also once you change that you will have a different problem. If you have a class called particle don't create an instance called particle. It is just asking for problems. Best case you end up overwriting the class definition with an instance. Worst case (and much more common) you will just get an UnboundLocalError.

    python Code:
    class Something:
        pass
     
    def task():
        Something = Something()
     
    task()
    Result:
    Code:
    UnboundLocalError: local variable 'Something' referenced before assignment
    -Mek
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    ah thanks, the class was meant to be capitalized, couldn't see for looking, all working as it should now thankyou very very much. also is it ok then, to have a class Particle and instance particle, is this just considered bad form to stop it being unreadable?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Technically all class names and instance names should follow the CamelCase convention. This allows any person to instantly know that the variable they are looking at is a class/instance. I would call it MyParticle or SomeParticle or similar.

    Aside from the naming issue, in terms of your code, why are you doing everything in the global namespace?

    python Code:
    import pygame as pg
    import random,sys,math,os
     
    BACKGROUND_COLOR = (255,255,255)
    SIZE = (WIDTH,HEIGHT) = (300,200)
     
    class Particle:
        def __init__(self, (x, y), size):
            self.rect = pg.Rect(x,y,size,size)
            self.move = list(self.rect.topleft)
            self.colour = (0,0,255)
            self.thickness = 1
            self.speed = 0
            self.angle = 0
        def draw(self,Surf):
            pg.draw.ellipse(Surf,self.colour,self.rect,self.thickness)
        def update(self,Surf):
            self.move[0] += math.sin(self.angle) * self.speed
            self.move[1] += math.cos(self.angle) * self.speed
            self.rect.topleft = self.move
            self.draw(Surf)
     
    class Control:
        def __init__(self):
            self.screen = pg.display.get_surface()
            self.number_of_particles = 10
            self.particle_list = self.generate_particles()
            self.done = False
            self.Clock = pg.time.Clock()
        def generate_particles(self):
            part_list = []
            for n in range(self.number_of_particles):
                size = 2*random.randint(10,20)
                x = random.randint(size, WIDTH-size)
                y = random.randint(size, HEIGHT-size)
                NewParticle = Particle((x,y),size)
                NewParticle.speed = random.random()
                NewParticle.angle = random.uniform(0, math.pi*2)
                part_list.append(NewParticle)
            return part_list
        def update(self):
            self.screen.fill(BACKGROUND_COLOR)
            for Part in self.particle_list:
                Part.update(self.screen)
        def event_loop(self):
            keys = pg.key.get_pressed()
            for event in pg.event.get():
                if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
                    self.done = True
        def main(self):
            while not self.done:
                self.event_loop()
                self.update()
                pg.display.flip()
                self.Clock.tick(60)
     
     
    if __name__ == "__main__":
        os.environ['SDL_VIDEO_CENTERED'] = '1'
        pg.init()
        pg.display.set_caption('first game')
        pg.display.set_mode(SIZE)
     
        RunIt = Control()
        RunIt.main()
        pg.quit();sys.exit()

    Cheers,
    -Mek
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0
    tbh im not really sure (still very new to this), just been working through a tutorial that i found interesting. if you like i can link the full code for what i have so far?
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Just a note, Python3 does not like
    def __init__(self, (x, y), size):

    correct with
    def __init__(self, x, y, size):
    and call with
    NewParticle = Particle(x, y,size)

    Now the great code example works with both Python274 and Python331
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo