Python Forum
NameError: name 'Particle' is not defined in Pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'Particle' is not defined in Pygame
#1
I have created a scenario for a bouncing ball

Firstly i have successfully created a class, and code was working wonders, later i tried to separate the coda as a Module which i will be explaining below and is giving error, Kindly help me to resolve

First i have created a Module in ..\drawing\crazyball.py, and code is as below


import pygame, sys
import random
import math
from pygame.locals import *


# In[2]:


class Particle:
    def __init__(self):
        self.x = 150
        self.y = 150
        self.size = 50
        self.colour = (255, 0, 0)
        self.speed = 0
        self.angle = 0

  

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

    def move(self):
        self.x += math.sin(self.angle) * self.speed
        self.y -= math.cos(self.angle) * self.speed
    #Here is the bounce function.
    def bounce(self):
        #This if statement is for when the circle exceeds the right wall.
        #This means the circle's x value is greater than the windows width subtract its size, meaning it exceeded the right wall.
        if self.x > width - self.size:
            #This is the reflected position represented as an x-coordinate.
            self.x = 2*(width - self.size) - self.x
            #This reflects the circle off the right wall at the same angle at which it hit the wall.
            self.angle = - self.angle

        #If the first 'if' statement is not true, then this 'elif' statement will be tested.
        #This means the circle's x value is lesser than the size of the circle, which means it has crossed the left wall/  
        elif self.x < self.size:
            self.x = 2*self.size - self.x
            self.angle = - self.angle

        #If the other statements aren't true, this statement will be tested.
        #This means the circle's y value is greater than windows height subtract the circles size.
        #Here, the circle exceeds the bottom wall.
        if self.y > height - self.size:
            self.y = 2*(height - self.size) - self.y
            #This line is a bit different because a horizontal boundary has an angle of pi, or 180 degrees.
            #Pi subtract the angle at which the circle hits the wall will give the reflected angle.
            self.angle = math.pi - self.angle

        #If the other statements aren't true, this statement will be tested.
        #This means the circle's y value is less than its size, which means it exceeded the top wall.
        elif self.y < self.size:
            self.y = 2*self.size - self.y
            self.angle = math.pi - self.angle
and the module i am calling in Main code: __init__.pynb file, the code is as below:
import pygame, sys
import random
import math
import importlib
import drawing.crazyball
importlib.reload(drawing.crazyball)

from pygame.locals import *

background_colour = ( 0,0,0)
#You should make your window bigger so the circles have more space to move.
width= 800
height= 600

screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('Circles and boundaries')
particle = Particle()
particle.speed = 0.14
particle.angle = random.uniform(3, math.pi*2)


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill(background_colour)
    particle.move()
    #This allows the circles to 'bounce' off the sides of the screen.
    particle.bounce(width,height)
    particle.display()  
    pygame.display.flip()
The code is giving me following error:
Error:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-3-05360eb658bd> in <module> 6 screen = pygame.display.set_mode((width,height)) 7 pygame.display.set_caption('Circles and boundaries') ----> 8 particle = Particle() 9 particle.speed = 0.14 10 particle.angle = random.uniform(3, math.pi*2) NameError: name 'Particle' is not defined
Kindly help, this was working perfect when was on single code, but when i tried created a module it is giving the error.

Thanks in Advance.
Reply
#2
When code is loaded in a module, the elements inside are in that module's namespace by default, although you could rename it.

So after import drawing.crazyball, you should be able to access the class as drawing.crazyball.Particle

I'm not sure what is intended with the importlib.reload line.
drunkenneo likes this post
Reply
#3
(Aug-15-2021, 07:20 AM)bowlofred Wrote: When code is loaded in a module, the elements inside are in that module's namespace by default, although you could rename it.

So after import drawing.crazyball, you should be able to access the class as drawing.crazyball.Particle

I'm not sure what is intended with the importlib.reload line.


Thanks. it worked!!! altough for importlib.reload, my module i am constantly updating and its not taking the change so for updated Module i needed to use the reload
Reply
#4
Do you have your module in the sys.path()??

I have a few home-made modules for doing my stuff. They are not normally in the sys.path, so I add them

import glob, sys

# make sure python can find my own modules
sys.path.append('/home/pedro/myPython/myModules/')

import OMR # my home-made module

# use a function from OMR called getClass()
clas = OMR.getClass()
drunkenneo likes this post
Reply
#5
(Aug-15-2021, 07:48 AM)drunkenneo Wrote: altough for importlib.reload, my module i am constantly updating and its not taking the change so for updated Module i needed to use the reload

That seems odd. reload will look for a modified module and try to load the changes, but it would have to have changed since the initial import. As you're running the reload one line after the import, there should be no changes between.

Reload should only be useful for long-running programs that need to pick up changes. If you exit and re-run, the changes should be seen. If you're not exiting and running the game a long time, then the reload line isn't executed later, and I don't see that it's doing anything useful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 261 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,092 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,874 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,302 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,507 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,911 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,335 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,940 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,873 Jul-26-2021, 04:36 PM
Last Post: snippsat
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 4,079 Jun-14-2021, 03:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020